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.

28 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.

digital certificate said...

THANK-YOU so much for taking the time to create this easy-to-understand post!

Anonymous said...

Very nice post.

Louis Vuitton iPhone 6 Cases said...

Multiple reports suggest that the iPhone 6s camera will feature a new 12MP sensor. Megapixels aren’t everything but a jump from the usual 8MP could mean that Apple has something big in store.

Raj Joshi said...

Happy Promise Day 2016 Images Pics Wishes,
Promise Day 2016 SMS Wishes Quotes,
Happy Promise Day Wishes Quotes Messages SMS
When is Promise Day 2016 Date
Promise Day Wishes Quotes SMS
2016 Promise Day Wishes SMS

Hug Day said...

happy hug day Images
hug day 2016 Wishes cards greetings
Hug day Gift Ideas For Him/Her
hug day sms for bf

dongdong said...

stephen curry shoes
canada goose jackets
michael kors outlet
michael kors uk
nike tn
babyliss flat iron
michael kors outlet
michael kors watches
coach outlet
polo ralph lauren outlet
michael kors outlet online
oakley sunglasses
reebok
nike air max
coach outlet online
coach outlet
burberry outlet
christian louboutin uk
reebok uk
michael kors outlet
coach factory outlet
christian louboutin uk
hermes belt
hollister co
versace
cartier glasses
pandora jewelry
fitflops shoes
louis vuitton outlet online
oakley store
valentino store
hollister sale
canada goose uk
coach outlet online
louis vuitton outlet
jordans shoes
coach outlet
kate spade outlet
toms shoes
armani exchange
20163.22wengdongdong

风骚达哥 said...

20160423 junda
air jordans
cheap oakley sunglasses
kate spade outlet
adidas stan smith
ray ban sunglasses
michael kors outlet
nike air max shoes
cheap omega watches
yeezy boost 350
michael kors outlet online
michael kors outlet online
nike free runs
air jordan uk
michael kors outlet online
pandora charms
ray ban outlet
calvin klein outlet
fitflops sale clearance
toms outlet
nike huarache white
sac longchamp
toms outlet
michael kors outlet clearance
bottega veneta outlet
armani watches
gucci handbags
nike blazer
oakley sunglasses
true religion jeans
cheap jordans
ray bans
hollister
bottega veneta handbags
jimmy choo outlet
oakley sunglasses
converse
prada outlet
reebok shoes
true religion
burberry outlet

Libin Huang said...

20160426libinferragamo outlet
pandora outlet
ferragamo shoes
nike air max
ferragamo shoes
hermes belt for sale
michael kors factory outlet
swarovski crystal
ferragamo shoes
michael kors outlet
louis vuitton handbags
louis vuitton handbags outlet
swarovski outlet
beats by dre
cazal outlet
mont blanc pens
longchamp solde
fitflops outlet
cartier sunglasses for men
michael kors outlet
louis vuitton outlet
swarovski crystal
cheap nfl jerseys
adidas outlet store
tory burch outlet
cheap oakley sunglasses
mulberry bags
rolex watches,rolex watches,swiss watches,watches for men,watches for women,omega watches,replica watches,rolex watches for sale,rolex replica,rolex watch,cartier watches,rolex submariner,fake rolex,rolex replica watches,replica rolex
oakley sunglasses
tory burch outlet
ray ban sunglasses
louis vuitton outlet
polo ralph lauren
michael kors uk
tiffany outlet