Disposing Objects (SPSite, SPWeb… etc.)

The SharePoint Foundation 2010 and SharePoint Server 2010 object models contain objects that implement the IDisposable interface. You should explicitly dispose those SharePoint objects that implement IDisposable when you are finished using them.

Why dispose?

Several of the SharePoint Foundation objects, primarily the SPSite class and SPWeb class objects, are created as managed objects. However, these objects use unmanaged code and memory to perform the majority of their work. The managed part of the object is much smaller than the unmanaged part. Because the smaller managed part does not put memory pressure on the garbage collector, the garbage collector does not release the object from memory in a timely manner. The object’s use of a large amount of unmanaged memory can cause some of the unusual behaviors described earlier. Calling applications that work with IDisposable objects in SharePoint Foundation must dispose the objects when the applications finish using them. You should not rely on the garbage collector to release them from memory automatically.

(The memory threshold should be 800 megabytes (MB) to 1.5 gigabytes (GB), assuming at least 2 GB of RAM. Setting the recycle of the application pool to occur closer to 1 GB
gives the best results.)

Each instance of SPSite and SPWeb contains a reference to an SPRequest object that, in turn, contains a reference to an unmanaged COM object that handles communications with the database server. SharePoint Foundation monitors the number of SPRequest objects that exist in each specific thread and in parallel threads, and adds useful entries to the logs under the three following scenarios:

  • The total number of SPRequest objects exceeds a configurable threshold. (most frequent) Default is 8.
  • An SPRequest object continues to exist at the end of a thread.
  • An SPRequest object was removed from the heap by garbage collection

Because the very low default threshold (8) may not apply to many sites, you can change this threshold by editing the following registry subkey:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings
LocalSPRequestWarnCount = desired threshold value
——————————————————————————————

To determine where the inproperly object was allocated, set this registry entry:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings\SPRequestStackTrace = 1

You can employ certain coding techniques to ensure object disposal. These techniques include using the following in your code:

  • The Dispose method
  • The using statement
  • try, catch, and finally blocks

———————————————————————————————————————————–
The Dispose and Close methods for the SPWeb object and SPSite object function in the same way. The Dispose method calls the object’s Close method. We recommend calling the Dispose method, instead of Close, because SPWeb and SPSite objects implement the IDisposable interface, and standard .NET Framework garbage collection calls the Dispose method to free any resources associated with the object from memory.

You can automatically dispose SharePoint objects that implement the IDisposable interface by using the Microsoft Visual C# and Visual Basic using statement. (The CLR translates using statements into try and finally blocks, and any objects that implement the IDisposable interface are disposed for you)

String str;

 

using(SPSite oSPsite = new SPSite(http://server”))

{


using(SPWeb oSPWeb = oSPSite.OpenWeb())

{

str = oSPWeb.Title;

str = oSPWeb.Url;

}

}

——————————————————————————————
DON’T USE “USING” IN THIS CASE —

If the SP object is returned by Context
SPContext objects are managed by the SharePoint framework and should not be explicitly disposed in your code. This is true also for the SPSite and SPWeb objects returned by SPContext.Site, SPContext.Current.Site, SPContext.Web, and SPContext.Current.Web.
——————————————————————————————

This example: SPWeb will not be dispoased; only the SPWeb returned by OpenWeb is disposed.


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

{


// … New SPSite will be leaked.

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

This will fix it:


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

{


using (SPWeb web = siteCollection.OpenWeb())

{


// Perform operations on site.

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

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

——————————————————————————————

If you don’t want to use Using, then use try/catch/finally (dispose the SP objects in the finally block)–

String str;

SPSite oSPSite = null;

SPWeb oSPWeb = null;

 

try

{

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

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();

}

 

——————————————————————————————

If in your “Try” block, you have to redirect to some other pages, do this: (Dispose the objects before leaving)

String str;

SPSite oSPSite = null;

SPWeb oSPWeb = null;

 

try

{

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

oSPWeb = oSPSite.OpenWeb(..);

 

str = oSPWeb.Title;


if(bDoRedirection)

{


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();

}

(TO BE CONTINUED)

 

 

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: