Nov 10, 2009

SharePoint 2010, PowerShell and Disposing

When you are already familiar with the SharePoint object model, you might know that SPSite and SPWeb allocate large amounts of memory and adequate memory management and disposing is essential. To make life easier the SharePoint team introduced two Cmdlets dealing with this in SharePoint 2010:
  • Start-SPAssignment
  • Stop-SPAssignment
By default, all Get commands dispose of these objects immediately after the pipeline finishes, but using SPAssignment, you can assign the list of objects to a variable and dispose of the objects after they are no longer needed. You can also ensure that the objects will remain as long as you need them, even throughout multiple iterations of commands
There are three levels of assignment:

No Assignment

The object is not assigned to a variable and is disposed after each iteration of the command.
Get-SPWeb http://server/sites/* |
     foreach { $_.Title | out-host } 

Simple Assignment

All objects are assigned to the global assignment store. This is done by using the Global parameter. When using this level, all objects are assigned to a global store and are disposed of when the Stop-SPAssignment command is called.

Start-SPAssignment -GlobalGlobal
$web = Get-SPWeb "http://server/sites/web"
$web.Title = "Greetings from PowerShell"
$web.Update()
Stop-SPAssignment -Global
# $web will be disposed

Note: Use global assignment with caution! For example, if you use Start-SPAssignment – Global with and then call Get-SPSite –Limit ALL, every site collection object will be loaded, every site collection object will be loaded into memory. In a live server farm, this is likely to cause to cause serious performance issues

Advanced Assignment

Objects are assigned to named stores for disposal. You can dispose of objects by using the -Identity parameter with the Stop-SPAssignment command.

$siteScope = Start-SPAssignment
foreach($site in ($siteScope | Get-SPSite "http://server/*"))
{
   $webScope = Start-SPAssignment
   $web = $webScope | Get-SPWeb $site.RootWeb.Url
   $web.Title = "Greetings from PowerShell"
   $web.Update()
   Stop-SPAssignment $webScope
}
Stop-SPAssignment $siteScope

Regardless of the level used, all objects are disposed of when the PowerShell runspace is closed.

20 comments:

Christian said...

Don't understand anything, but hope it will be something good ;)

aa said...
This comment has been removed by a blog administrator.
aa said...
This comment has been removed by a blog administrator.
Volker from Germany said...
This comment has been removed by a blog administrator.
Send Flowers to USA said...

I suggest this site to my friends so it could be useful & informative for them also. Great effort.

Fewlines4Biju said...

See some guidelines here

http://www.fewlines4biju.com/2011/08/disposing-sharepoint-objects.html

Alexandre said...

thank you SO MUCH !

Anonymous said...

Awesome explanation. Thanks so much for the effort of writing it all down for the community.

mardukes said...

I don't get it. What's the difference between no assignment and advanced assignment?

Christian said...

If you run a command that only consist of one line and when you don't assign the result to a variable the objects get disposed automatically e.g. get-spweb http://blabla
When you assign the result to a variable you have to dispose yourself or you can use the advcanced assignment approach.
e.g. $web = get-spweb http://blabla
$web.Dispose()

Prank Calls said...

Nice blog and nice post, The topic here i found is really effective.

Show Plates said...

Nice blog, I really appreciate the way you are sharing your experiences.

Tina said...

Some posts really matters because they are valueable, I have found your post very valueable.

small business voip said...

I read and walked for miles at night along the beach, writing bad blank verse and searching endlessly for someone wonderful who would step out of the darkness and change my life. It never crossed my mind that that person could be me.

small business voip

voip service providers said...

I read and walked for miles at night along the beach, writing bad blank verse and searching endlessly for someone wonderful 1who would step out of the darkness and change my life. It never crossed my mind that that person could be me.

voip service providers

Superman said...

Glad to read your post :). It is very informative!

Christina said...

Very value able post, I read the whole story when I start reading it.

Raj Jumar said...

Face Fitness Formula Ebook: How To Get Rid Of Chubby Cheeks And Double Chin Using Step-By-Step Facial Exercises That Help You Lose Face Fat And Give You A Sexier, More Sculpted Facial Appearance. This site is a leading resource for information about the John Socratous Face Fitness Formula PDF book and face rejuvenation system that teaches you how to lose face fat and build the attractive jawline that turns heads and gets noticed.
silk sarees

eHaze said...

Typo.
Start-SPAssignment -GlobalGlobal

Should be
Start-SPAssignment -Global

GlobalGlobal doesn't work.

moona said...

Nice post realy good post for all, that you have updated us with all of nice information that can be very useful for future.