SharePoint 2010 Recommended Practices for disposing objects–Part1

Home | SharePoint Development | SharePoint 2010 Recommended Practices for disposing objects–Part1

I thought of collating set of best practices around disposing the SharePoint objects. This three part series of articles primarily focuses around that.

1. Disposal of SharePoint Objects

All the SharePoint Foundation 2010 and SharePoint Server 2010 object models implement IDisposable interface. It is recommended to explicitly dispose SharePoint 2010 objects that implement this interface. Because the large part of SharePoint Foundation 2010 object model has unmanaged objects, only a small set of it is managed objects. Do not rely on Garbage collector to release unused SP Foundation objects. It is recommended to explicitly dispose them.

Using Statement for disposing objects

a)The Using Statement can be leveraged to automate the disposal of SharePoint objects that implements IDisposable interface

string test;

using(SPSite oSPsite = new SPSite("http://yoursitecollection"))

{

using(SPWeb oSPWeb = oSPSite.OpenWeb())

{

test = oSPWeb.Title;

test = oSPWeb.Url;

}

}

b)The common language runtime translate ‘Using’ statement into a finally block and disposes the object. Using statements should be used

with Caution for the objects instantiated through SPContext.

c)Do not leverage ‘Using’ statements for SPSite or SPWeb objects that get instantiated through SPContext like the following.

using( SPWeb web = SPControl.GetContextWeb(HttpContext.Current)) { … }

d)In this case the ‘SPContext’ object is managed by SharePoint 2010 framework. This should not be used in ‘Using’ clause.

e)Leverage ‘Using’ statements for every new instance of SPSite or SPWeb created explicitly by the user (not created through SPContext). The following statement is problematic. Because the ‘Using’ statement takes care of only SPWeb, not the SPSite object. In this case, the SPSite object is created, but never disposed.

using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())

{

} // SPWeb object web.Dispose() automatically called.

f)This problem can be fixed by nesting a ‘Using’ statement for SPSite within another ‘Using’ statement for SPWeb.

void NestingUsingStatements()

{

using (SPSite oSiteCollection = new SPSite(SPContext.Current.Web.Url))

{

using (SPWeb oSPWeb = siteCollection.OpenWeb())

{

// 

} // SPWeb object web.Dispose() automatically called.

} // SPSite object siteCollection.Dispose() automatically called.

}

2. Try-Catch-Finally statement for disposing objects

The alternative approach is to leverage Try-Catch-Finally block for disposing unused objects.

a)Leverage catch block to handle exceptions and finally block to dispose unused objects. Do not leave ‘Catch’ block empty.

try

{

oSPSite = new SPSite("http://yourserver");

oSPWeb = oSPSite.OpenWeb(..);

str = oSPWeb.Title;

}

catch(Exception e)

{

// Handle exception, log exception, etc.

}

finally

{

if (oSPWeb != null)

oSPWeb.Dispose();

if (oSPSite != null)

oSPSite.Dispose();

}

b) Always check for ‘Null’ in the finally block before disposing objects.

c)Try-Catch-Finally block can be leveraged to dispose objects created inside for-each loop.

d)Whenever the SharePoint code performs a Re-direct using Response. Redirect, it is recommended to dispose the objects explicitly before redirect. Usually finally block never gets executed during the re-direct.

try

{

oSPSite = new SPSite("http://server");

oSPWeb = oSPSite.OpenWeb(..);

str = oSPWeb.Title;

if (oSPWeb != null)

oSPWeb.Dispose();

if (oSPSite != null)

oSPSite.Dispose();

Response.Redirect("newpage.aspx");

}

catch(Exception e)

{

}

finally

{

if (oSPWeb != null)

oSPWeb.Dispose();

if (oSPSite != null)

oSPSite.Dispose();

}

e)If an object is created using ‘new’ operator , it is recommended to dispose the object

void ExplicitDisposeMethod()

{

SPSite oSPSiteCollection = null;

try

{

oSPSiteCollection = new SPSite("http://moss");

}

finally

{

if (oSPSiteCollection != null)

oSPSiteCollection.Dispose();

}

}

f)Do not share SPRequest objects across threads

g)Do not store SharePoint objects (SPSite and SPWeb) in static variables

Please refer part2 of this article for continuation …

 Subscribe to my blog

2 thoughts on “SharePoint 2010 Recommended Practices for disposing objects–Part1

Leave a Reply