Udostępnij za pośrednictwem


SharePoint + PowerShell Memory Leak

It’s very important that anyone working with SharePoint object model be concerned with the appropriate management of memory.  Most developers are aware of the need to dispose objects properly, but we some times get sloppy when it comes to things like PowerShell.

Zach Rosenfield just posted a very important note for anyone using PowerShell with the SharePoint object model.  A memory leak can occur on EVERY object if the objects are referenced across several “sentences” or pipelines.

e.g.   

PS>   $site = new-object Microsoft.SharePoint.SPSite(“https://contoso”)
PS>   $site.Title
PS>   $site.Dispose()

The example above would still cause some memory to leak because the SPSite object is referenced on several pipelines (threads) and lost along the way.

Zach’s post can be found here with a good work around:  https://blogs.msdn.com/sharepoint/archive/2009/02/11/sharepoint-and-powershell-knowledge.aspx

IMPORTANT:

I did confirm today that the issue only pertains to scripts executed across multiple threads/pipelines in the shell.  This means that writing your commands in functions, script files, modules, etc. are all still recommended best practices as they are executed from the shell in a single sentence….thus resulting in a single pipeline (thread).

e.g. 

PS>   Get-SPSiteTitle(“https://contoso”)