Welcome to MSDN Blogs Sign in | Join | Help

SharePoint + PowerShell

[Cross Posted from Zach Rosenfield's Blog] 

 

Today I’m posting about a rather dense technology issue—so please bear with me!  Those of you who read my blog know that I am a huge fan of PowerShell and on occasion talk about various ways to use PowerShell for managing SharePoint deployments.  Additionally I’ve seen projects and people around the web discuss it.  However, it wasn’t until learning about some of the most in-depth inner-workings of PowerShell did an important realization come about.  In short: There was a potential for PowerShell users to constantly leak memory without knowing it!  While this is not a common situation and it would be hard to do so in such large volume to really cause real harm—it is possible, and thus, everyone should know about it.

 

So first, what’s the issue?  Well, first of all it is important to know that SharePoint does an amazing amount of work in relation to the SPSite and SPWeb objects to make sure that they are running for optimal performance—which in terms of native code means working to improve memory allocation.  Thus there is some special code in these objects, but this also comes with some unique requirements—one that should not be new to SharePoint developers: the need to Dispose of these objects.  So what’s changed you ask?  Well—did you know that SPSite and SPWeb objects should never be used across threads?

 

Usually this is not important.  Multiple threads can safely access the same SPSite or SPWeb simultaneously, so there’s no reason for a developer to open a reference to one of these objects and pass it to another thread.  However, if you did try this, the unmanaged Heap (which is thread specific) will be “lost” and a new Heap will be allocated in the new thread.  So if you did this endlessly your machine will eventually run out of memory (though this memory is freed if you close the PowerShell process).

 

So on to the specific PowerShell concern.  I only recently learned that every time a new ‘pipe’ in PowerShell is executed (essentially every time you press ‘enter’), that command is run in a unique thread.  So what does this mean for SharePoint?  Well, it means that saving an SPSite or an SPWeb to a variable will cause memory leaks—no matter what you do. 

 

As an example, let’s take the following syntax:

 

PS>  $S = New-Object Microsoft.SharePoint.SPSite(“http://sharepoint”)

PS>  $S.Title

My SharePoint Site

PS> $S.Dispose()

 

Looks fine, right?  Unfortunately, since each “PS>” line is executed in a new thread, the final $S.Dispose() does not dispose of the unmanaged heap (before you go try to prove me wrong, realize that the managed code is properly disposed—so you will recover some memory).

 

Well, as it turns out this is a simple problem to mitigate.  Since each PowerShell ‘sentence’ is run in a single thread, any loaded function or script is run as a single thread!  So while the syntax above was not healthy, the exact same lines of code run in script work fine.  Or even this is line works great:

 

PS>  $S = New-Object Microsoft.SharePoint.SPSite(“http://sharepoint”);  $S.Title; $S.Dispose()

 

Keep your eye out on this blog for other potential workarounds in the future. 

 

With that in mind, happy scripting!

 

Zach Rosenfield
Program Manager, Microsoft Office SharePoint Server

Published Wednesday, February 11, 2009 1:01 PM by sptblog
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Renaming Root “Title” Site Column - Powershell Example « SharePoint Knowledge Base

# SharePoint + PowerShell Memory Leak

It’s very important that anyone working with SharePoint object model be concerned with the appropriate

Thursday, February 12, 2009 8:05 PM by Eric Kraus' SharePoint/.NET Blog

# re: SharePoint + PowerShell

Friday, February 13, 2009 5:04 AM by Hosty

# re: SharePoint + PowerShell

Friday, February 13, 2009 5:06 AM by Hosty

# re: SharePoint + PowerShell

The memory footprint when examined of the powershell environment, at least seems to nicely de-allocate when you exit the actual prompt.

If you were to do spsite.allwebs | foreach-object {do whatever here but dont dispose}

for thousands of webs, you would see gigs of memory go down the drain.

However exiting the powershell prompt seems to de-allocate that memory.

Whether or not that effectively leaks anything, is of course hard to tell, however testing has so far not revealed any problems (I have severely strained our test environment like this), over several days with no reboots or the like.

Indeed, the memory allocated for holding the spwebs is effectively out of hands for the script environment, so if not disposed immediately = handle gone forever (I like to think of it as a handle to each spweb, the $_).

But for actual leaks to occur, I really haven't been able to reproduce leaks for sure.

Monday, February 23, 2009 1:43 AM by AllTheGoodNamesAreInUse

# Microsoft SharePoint Team Blog : SharePoint + PowerShell

Keep this in mind if you are Using PowerShell to work with MOSS or WSS like I do. I always recommend

Thursday, March 05, 2009 10:20 AM by It's my life... And I live it...

# web tasarım

Thursday, July 30, 2009 7:15 AM by web tasarım

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker