Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How bad is it to not dispose() in Powershell?

Sometimes we need to perform small administrative tasks in SharePoint. A simple PowerShell script is a really good tool for that. For instance, such script can enumerate event handlers of a list:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") $site = new-object Microsoft.SharePoint.SPSite($args[0])    $site.RootWeb.Lists["MyList"].EventReceivers > C:\MyListHandlers.txt 

It's known that objects like SPSite and SPWeb have to be Dispose()-d after a call, otherwise memory leaks occur. The best would be to call

$site.RootWeb.dispose() $site.dispose() 

at the end of this script. But if this is a Powershell script which will only be run once, and we know that PowerShell cleans up after execution - is it so bad to not call dispose()?

So, my question is - is there some danger if sometimes I run scripts like this; will it affect the overall stability of SharePoint farm (or of the server on which I'm running the script)?

like image 533
naivists Avatar asked Jan 04 '10 14:01

naivists


People also ask

Does closing PowerShell stop script?

The PowerShell console will immediately close. This keyword can also exit a script rather than the console session. Including the exit keyword in a script and exit terminates only the script and not the entire console session from where the script runs.

Why we use $_ in PowerShell?

The $_ is a variable or also referred to as an operator in PowerShell that is used to retrieve only specific values from the field. It is piped with various cmdlets and used in the “Where” , “Where-Object“, and “ForEach-Object” clauses of the PowerShell.

What should I save a PowerShell script as?

To save and name a script In the Save as type box, select a file type. For example, in the Save as type box, select 'PowerShell Scripts ( *. ps1 )'. Click Save.


1 Answers

This has been edited to include a safe, nonspecific answer.

IN GENERAL: dispose everything, because Dispose is the .NET framework's way to free up external resources (such as file handles, TCP ports, database connections, etc). Resources are not guaranteed to be released unless you call Dispose(). So beware. This is the general, non-SharePoint answer.

SPECIFICALLY WHEN DEALING WITH SharePoint: 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.

I've built scripts for working with SharePoint, and most of the time I don't bother disposing.

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

like image 96
Peter Seale Avatar answered Sep 21 '22 11:09

Peter Seale