title: How can I fix this memory leak in my SharePoint Powershell script?
body: <p>Purpose of the script: due to our Active Directory infrastructure, we add about 6000 entries directly to the Site User Info List so that they are shown in the address book in the SharePoint people picker.
  The script creates about 6000 SPUser objects in order to add them to the Site User Info List. Most of them already exist, in this case new-SPUser should do nothing, right?</p>
  
  <p>EDIT: please leave a feedback or possible solution. I removed some of the large log for better overview and added some details. Perhaps you know another forum where I should post this question? </p>
  
  <p>I get lots of messages like this:</p>
  
  <blockquote>
    <p>Potentially excessive number of SPRequest objects (20) currently
    unreleased on thread 9.  Ensure that this object or its parent (such
    as an SPWeb or SPSite) is being properly disposed. This object is
    holding on to a separate native heap.This object will not be
    automatically disposed. Allocation Id for this object: {...snip...} Stack
    trace of current allocation: at
    Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite
    site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean
    bNotAddToContext, Byte[] UserToken, String userName, Boolean
    bIgnoreTokenTimeout, Boolean bAsAnonymous)<br>
    ...snip...</p>
  </blockquote>
  
  <p>And lots of messages like this:</p>
  
  <blockquote>
    <p>08/17/2011 19:38:35.26    PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   Performance     nask    Monitorable An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  Allocation Id: {09C61E87-2BA1-4CEA-BB3C-266A1345559A}  To determine where this object was allocated, set Microsoft.SharePoint.Administration.SPWebService.ContentService.CollectSPRequestAllocationCallStacks = true.    de663eab-8063-4381-88b5-01cc50d51b77</p>
  </blockquote>
  
  <p>This is the current script with highlighted four lines (<code># ***</code>) that we added to solve it (did not work). </p>
  
  <pre><code>Write-Host "Fill Site User Info List for "$args[0]
  add-pssnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
  
  Start-SPAssignment -Global    # ***
  
  #global properties
  $ppFilter = "(...)"
  
  get-date
  
  $ldapFilter = "(...)"
  $ldapDirectory = "LDAP://DC=..."
  Write-Host "Get all users from ..."
  $de = New-Object system.directoryservices.directoryentry($ldapDirectory)
  $de
  $newsearch = New-Object System.DirectoryServices.DirectorySearcher($de)
  write-host "Filter all active users with ..."
  $newsearch.Filter = $ldapFilter
  $newsearch.PageSize = 200
  $result = $newsearch.FindAll()
  
  write-Host "Get all domain user accounts..."
  $useraccounts = $result | % {"..."}
  
  $siteCollection = get-spsite $args[0]
  $webApp = $siteCollection.WebApplication
  
  Write-Host "clear people picker filter" $webApp.Name "..."
  $webApp.peoplepickersettings.ActiveDirectoryCustomFilter = ""
  $webApp.Update()
  
  write-Host "---------------"
  $siteUrl = $siteCollection.Url
  Write-Host "add "$useraccounts.Count" users to site collection "$siteUrl "..."
  $start =  get-date
  $site = $siteCollection.RootWeb       # ***
  foreach ($user in $useraccounts) { 
      $current = New-SPUser -UserAlias $user -Web $site   # *** was before: $siteUrl
  }
  $end = get-date
  $end - $start
  $SiteCollection.Dispose()
  Write-Host "---------------"
  
  Write-Host "set people picker filter to" $ppFilter "..."
  $webApp.peoplepickersettings.ActiveDirectoryCustomFilter = $ppFilter
  $webApp.Update()
  
  Stop-SPAssignment -Global    # ***
  
  Write-Host "Done."
  get-date
  </code></pre>
  
  <p>Is there an obvious error we made? Thank you for any suggestions.</p>
  
  <p>In answer to @x0n I post a larger portion of the log. Sorry for this unreadable formatting. Is there a better way to post logs? All line breaks and tab spaces are messed up.</p>
  
  <p>The point is, that the messages about reclaimed SPRequest object come from the powershell process. I am perfectly sure that this script was the only one running. This script used to finish within less than 10 Minutes, but now it does not finish even after 1.5 hours. I am pretty sure I did not change anything in the script that changed the behaviour. The performance view of the Windows Task Manager did show a moderate load on CPU and quite high usage of RAM (not all the way to 100%) while the script was running. </p>
  
  <blockquote>
    <p>Timestamp                  Process                     TID     Area                    Category        EventID Level       Message     Correlation
    08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   PowerShell      6tf0    Medium      Entering EndProcessing Method of New-SPUser.<br>
    08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   PowerShell      6tf0    Medium      Leaving EndProcessing Method of New-SPUser.<br>
    08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   PowerShell      6tf0    Medium      Entering BeginProcessing Method of New-SPUser.  de663eab-8063-4381-88b5-01cc50d51b77
    08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   PowerShell      6tf0    Medium      Leaving BeginProcessing Method of New-SPUser.   de663eab-8063-4381-88b5-01cc50d51b77
    08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   PowerShell      6tf0    Medium      Entering ProcessRecord Method of New-SPUser.    de663eab-8063-4381-88b5-01cc50d51b77
    08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   Performance     naqx    Monitorable Potentially excessive number of SPRequest objects (19) currently unreleased on thread 9.  Ensure that this object or its parent (such as an SPWeb or SPSite) is being properly disposed. This object is holding on to a separate native heap.This object will not be automatically disposed. Allocation Id for this object: {815F9A3D-6635-42FA-9658-B6294968136E} Stack trace of current allocation:    at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)     at Microsoft.SharePoint.SPRequestManager.GetContextRequest(SPRequestAuthenticationMode authenticationMode)     at Microsoft.SharePoint.Administration.SPFarm.g... de663eab-8063-4381-88b5-01cc50d51b77</p>
    
    <p>( .... snip ....)  </p>
    
    <p>08/17/2011 19:38:34.30     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   Performance     naqx    Monitorable Potentially excessive number of SPRequest objects (19) currently unreleased on thread 9.  Ensure that this object or its parent (such as an SPWeb or SPSite) is being properly disposed. This object is holding on to a separate native heap. Allocation Id for this object: {F9CDF3DA-41B1-485D-B78F-0266545C417A} Stack trace of current allocation:    at Microsoft.SharePoint.SPGlobal.CreateSPRequestAndSetIdentity(SPSite site, String name, Boolean bNotGlobalAdminCode, String strUrl, Boolean bNotAddToContext, Byte[] UserToken, String userName, Boolean bIgnoreTokenTimeout, Boolean bAsAnonymous)     at Microsoft.SharePoint.SPWeb.InitializeSPRequest()     at Microsoft.SharePoint.SPWeb.InitWebPublic()     at Microsoft.SharePoint.SPWeb.get_Exists()     at Microsoft.SharePoint.PowerShell.SPWebPipeBind.Rea... de663eab-8063-4381-88b5-01cc50d51b77</p>
    
    <p>(... snip ...)<br>
    (... many of those messages: )<br>
    08/17/2011 19:38:35.26     PowerShell.exe (0x1388)     0x20C0  SharePoint Foundation   Performance     nask    Monitorable An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  Allocation Id: {09C61E87-2BA1-4CEA-BB3C-266A1345559A}  To determine where this object was allocated, set Microsoft.SharePoint.Administration.SPWebService.ContentService.CollectSPRequestAllocationCallStacks = true.    de663eab-8063-4381-88b5-01cc50d51b77</p>
  </blockquote>