7

Title of the question is pretty simple, I have 18000 site collections and this script is showing 1GB of ram used, so I wonder how can I dispose correctly the spweb and spsite object

$webapplication =  Get-SPWebApplication  http://xxx
foreach($site in $webapplication.Sites)
{         

    foreach($subWeb in $site.AllWebs)
    {
        if($subWeb.AllProperties["WebTemplate"] -eq "Client" -or $subWeb.AllProperties["WebTemplate"] -eq "Job")
        {

           if($subWeb.AllProperties["WebTemplate"] -eq "Client")
           {
                $infoList  = $subWeb.Lists["Client Info"]
           }

           if($subWeb.AllProperties["WebTemplate"] -eq "Job")
           {
                $infoList  = $subWeb.Lists["Job Info"]
           }

           if($infoList -ne $null)
           {              
               $InfoItem = $infoList.Items[0]
               if($InfoItem -ne $null)
               {
                  $clientCode = $InfoItem["Client Code"]

                  $quicklaunchnavigationnodes= $subWeb.Navigation.QuickLaunch
                  foreach($navNode in $quicklaunchnavigationnodes)
                  {
                      if($navNode.Title.Contains("Tax Invoices"))
                      {
                        $clientcodefromurl = $navNode.Url.Substring($navNode.Url.IndexOf("=")+1,8)

                        if($clientCode -ne $clientcodefromurl)
                        {
                            Write-Host 'Processing  site ' $subWeb.Url 
                            Write-Host '  Info list found for ' $subWeb.Url 
                            Write-Host '  Info item found '
                            Write-Host '    Client code from url: ' $clientcodefromurl                          
                            Write-Host '    There is a difference' -ForegroundColor Red 
                            Write-Host '    Incorrect Navigation Url: ' $navNode.Url                           
                            Write-Host '    New Url: ' $navNode.url.Replace($clientcodefromurl, $clientCode)
                            #$navNode.url.Replace($clientcodefromurl, $clientCode)
                            #$navNode.Update()
                            Write-Host '    Url updated' -ForegroundColor Green 
                        }
                      }                
                  }
               }
               else
               {
                     Write-Host '  Info item not found '$spWeb.Url 
               }           
           } 
           else
           {
               Write-Host 'Info list not found for ' $subWeb.Url 
           }  
        }    
    }
}
0

1 Answer 1

14

When you close the PowerShell.exe process, the memory is freed. If you need to dispose objects to keep memory pressure down (important in production environments or if you're looping over all sites/webs), make sure to dispose. If not, you don't need to worry about disposing.

The reason why we're so crazy about disposing in the first place is because most SharePoint code runs in long-running processes (either in an ASP.NET worker process or OWSTimer.exe) and failing to dispose can cause difficult-to-troubleshoot, sudden catastrophes (i.e., web server go boom). These catastrophic performance issues/OutOfMemoryExceptions don't affect me most of the time when working in PowerShell. I run ad-hoc scripts, I waste ~3-50MB of RAM because I fail to dispose my objects, I close the PowerShell window and the memory is freed. Most of the time it's a nonissue.

You can built scripts for working with SharePoint using the below links,

Here is a script wherein I dispose SPSite and SPWeb objects

Here is a script in which I don't bother to dispose an SPSite object

How can I dispose SharePoint objects safely in PowerShell functions?

Source

I strongly recommend you to have a look at the below link

Best Practices: Using Disposable Windows SharePoint Services Objects

You can also form your script as per the below script,

foreach ($site in $wa.Sites) {
 foreach ($web in $site.AllWebs) {
  #Put your functionality
  $web.Dispose()
 }
 $site.Dispose()
} 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.