SIGN UP MEMBER LOGIN:    
ARTICLE

Object Disposal in Power Shell scripting for SharePoint 2010

Posted by Destin joy Articles | SharePoint October 22, 2010
In this article I am trying to explain object disposal in Power Shell.
Reader Level:

In this article I am trying to explain object disposal in Power Shell. Most of us are aware of object disposal in SharePoint object model. In scenarios in which we use SharePoint objects extensively-for example, in SharePoint sites that use custom Web Parts or event handler etc.it can cause the following unusual behaviors if we are not disposing of SharePoint objects once we finished with the operation. The following are the main causes if you don't dispose SharePoint objects properly in custom coding.

  • Frequent recycles of the Windows SharePoint Services application pool, especially during peak usage

  • Application crashes that appear as heap corruption in the debugger

  • High memory use for Microsoft Internet Information Services (IIS) worker processes

  • Poor system and application performance

Similarly in Power shell scripting also we have to adopt disposal practices to avoid memory leakage resulting in performance issues on your servers.

We have two methods for disposal practice

  1. Explicitly dispose every object you create

  2. Use SpAssignmentGlo

Explicitly dispose every object you create

Please find one example for explicit dispose

$template = Get-SPWebTemplate "STS#0"
New-SPSite -Url "your Site collection URL" -OwnerAlias "domain\user" -Template $template
$template.dispose()

SpAssignment

Any objects defined between the Start-SPAssignment –Global and Stop-SPAssignment –Global commands will be automatically disposed of by PowerShell.You don't have to explicitly dispose each object you created

Start-SPAssignment –Global

$template = Get-SPWebTemplate "STS#0"
New-SPSite -Url "your Site collection URL" -OwnerAlias "domain\user" -Template $template

Stop-SPAssignment –Global

Login to add your contents and source code to this article
share this article :
post comment
 

Hi Destin
The second method of disposal in powershell ie SpAssignmentGlobal is good.Really powershell in 2010 brings a lot of help  to developers

Posted by jibin koshy Nov 03, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor