3

I have been doing a lot of scripting lately and came across memory considerations recently as we are in the phase of running scripts in production environments.

Basically what I do throughout my scripts is:

Start-SPAssignment -Global
Get-SPWebApplication $webApplicationUrl | Get-SPSite -Limit All | foreach {
    $_.AllWebs | foreach {
        $_.Lists | ? { $_.BaseType -eq "DocumentLibrary" } | foreach { 
            #Logic goes here like writing info about the list into CSV
        }
    }
}
Start-SPAssignment -Global

What we noticed after running above script in an environments with 3000+ site collections and 10000+ lists is that PowerShell process eats up to 1,5+ GB of memory, I guess I'm doing the disposal the wrong way! Or is that normal?

Should I try something like below instead?

Get-SPWebApplication $webApplicationUrl | Get-SPSite -Limit All | foreach {
    $siteAssignment = Start-SPAssignment
    $_.AllWebs | foreach {
        $webAssignment = Start-SPAssignment
        $_.Lists | ? { $_.BaseType -eq "DocumentLibrary" } | foreach {
            #Logic goes here like writing info about the list into CSV
        }
        Stop-SPAssignment $webAssignment
    }
    Stop-SPAssignment $siteAssignment
}

Or should I try a whole different approach? Thankful for you ideas over this case!

0

I would definitely dispose the SPSite and SPWeb you get from the Get-SPSite and $_.AllWebs at the end of each respective foreach loop.

Get-SPWebApplication $webApplicationUrl | Get-SPSite -Limit All | foreach {
    $siteAssignment = Start-SPAssignment
    $_.AllWebs | foreach {
        $webAssignment = Start-SPAssignment
        $_.Lists | ? { $_.BaseType -eq "DocumentLibrary" } | foreach {
            #Logic goes here like writing info about the list into CSV
        }        
        Stop-SPAssignment $webAssignment
        $_.Dispose() #<<<<<
    }    
    Stop-SPAssignment $siteAssignment
    $_.Dispose() #<<<<<
}
  • Did like you suggested, noticed significant difference in memory consumed by PowerShell process, it sticks to 120+ MB. Wonderful. – Eliya Amanoeel Oct 12 '16 at 14:16
2

1,5+ GB with 3000+ site collections? That's nothing. While it's the best practice to dispose of SPWeb objects that you get from SPSite.AllWebs, it's only true when working with large amount of SPWebs, because when your SPSite is being disposed, all the child objects are disposed as well. And then again, when you close PowerShell, the memory is freed anyway. It's not like it's an IIS process which would free the leaked memory only when an iisreset is called. I would say you're safe with only SPSite.Dispose(), as the SPWeb.Dispose() will only introduce unnecessary overhead in your case.

Get-SPWebApplication $webApplicationUrl | Get-SPSite -Limit All | foreach {
    $siteAssignment = Start-SPAssignment
    $_.AllWebs | foreach {
        $_.Lists | ? { $_.BaseType -eq "DocumentLibrary" } | foreach { 
            #Logic goes here like writing info about the list into CSV
        }
    }
    Stop-SPAssignment $siteAssignment
    $_.Dispose()
} 
  • That sounds very logical and acceptable! Thanks for the contribution. – Eliya Amanoeel Oct 12 '16 at 14:16
  • 1
    Just because you have lots of RAM doesn't mean you should get sloppy. Do you really want to get back to this code sometime later when amount of data grows and add the disposals (that also closes(!) the objects) when you could do it now? Btw. am I reading your code correctly that you're not disposing even the SPSites as you have it outside the SPSite foreach? – Jussi Palo Oct 13 '16 at 7:38
  • There was a mistype, $_.Dispose() ought to be in the loop, sorry. The code depends if that is a reusable solution or designed for a specific environment. I would stick to this, as I still can't see any benefits of disposing SPWeb objects in a loop versus disposing them in batches. Especially in PowerShell. Even if I had to dispose of them, I would do it in a try/catch/final instead of $_.Dispose after Stop-SPAssignment $webAssignment. But then again, if it's a 100mb we are trying to save for a short period of time, we might as well introduce even more overhead. – Paul Strupeikis Oct 13 '16 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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