try / finally using() SharePoint Dispose()

Chances are that by now if you have been developing on the Microsoft SharePoint Office Server (MOSS) 2007 and Windows SharePoint Services (WSS) 3.0 Object Model (OM) for any length of time you are aware of the importance of when to (and when not to) call Dispose()  on your SharePoint Objects.  However, you may not be aware that if you do not always wrap your Dispose()  method(s) within a finally { } block either implicitly by implementing the using() { } statement or by explicitly wrapping it in your code with try/finally then you you can’t guarantee that your intended objects Dispose() will ever be called during an exception. 

SharePoint developers are required to be alert and stay on their toes when creating and releasing SPSite and SPWeb objects.  Intimately understand your applications code path(s) and object scope lifetime in order to optimize performance and good memory hygiene with the SharePoint platform is paramount.  Every .NET application (not just SharePoint) must play by the .NET Common Language Runtime (CLR) house rules to ensure that Dispose()  gets called (when appropriate) in a timely fashion if you want to avoid very serious negative consequences exacerbated in heavily utilized production SharePoint environments.  You need to make certain and take extra measures to take that code snippet you found which for brevity excluded proper dispose methods and works fine on a developers Virtual PC environment later keeps the business stakeholders up late at night wondering why their customized SharePoint site is having production problems under load.

Below I discuss some edge cases that you should be aware of when implementing using() and try/finally in your code to help mitigate the risk of memory leaks and help tighten up your production code.

Understanding the using() statement

Since you have your SharePoint OM homework you know that you can take advantage of the using() statement to increase the readability and help simplify the the amount of code you have to write to properly release the unmanaged memory held onto by the SharePoint managed code.  Behind the scenes .NET has the CLR automatically inject into your MSIL code a try/finally block (notice I did not say try/catch/finally) and the Dispose()  method is getting called on your behalf. 

public void UsingBestPractice()
{
    using (SPSite siteCollection = new SPSite("https://moss"))
    {
        using (SPWeb web = siteCollection.OpenWeb())
        {
              //...
        } // SPWeb object web.Dispose() automatically called
    }  // SPSite object siteCollection.Dispose() automatically called
}

Coding with try/finally

What might not be so commonly known when writing SharePoint code is that there are some cases where we should not implement using() .   We do have the option of manually emulating what the CLR does behind the scenes automatically for us by implementing try/finally blocks which contain the respective Dispose() method(s) which ultimately allows our SharePoint IT Administrator(s) to get their much needed beauty sleep.  By the way, there is nothing wrong with adding a try/catch/finally as long as you make certain that you are properly handling the exceptions in the catch { } block and not just eating them without some sort of logging in place.  You do not want to see any empty catch { } blocks in your production code.  Also, note the best practice of conditionally testing for null prior to disposing as shown in the following code. 

void TryFinallyBestPractice()
{
    SPSite siteCollection = null;
    SPWeb web = null;

    try
    {
        siteCollection = new SPSite("https://moss");
        web = siteCollection.OpenWeb();
        Console.WriteLine(web.Title);
    }  // optionally catch { ... make sure you handle if you use catch }
    finally
    {
        if (web != null)
            web.Dispose();

        if (siteCollection != null)
            siteCollection.Dispose();
    }
}

Don’t use SPContext with using()

The following example illustrates a common bad practice of wrapping a SPContext originated SPWeb object in a using() statement which as we now know will automatically inject a try/finally that will call web.Dispose() .  In cases where RootWeb equals the SPContext “web” as shown below this can cause production problems since SPContext objects are managed by SharePoint itself and should not be released by you (ever).

 // Bad.. Dispose() is automatically called on web which SPContext equality
using( SPWeb web = SPControl.GetContextWeb(HttpContext.Current)) { ... }
Combining OM Calls

Use caution when combining SharePoint Object Model calls into the same line as these can be some of the most tricky leaks to find.  Dissecting the following example we see:

  • SPContext.Current.Web.Url is fine no dispose needed
  • SPSite is instantiated which needs to be Disposed but is lost since we are also calling OpenWeb() which is actually what using() will Dispose()
 void CombiningCallsLeak()
{
    using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())
    {
        // ... new SPSite will be leaked
    } // SPWeb object web.Dispose() automatically called
}

You would have to split the previous sample up into nested using() statements to avoid leaking memory:

 void CombiningCallsBestPractice()
{
    using (SPSite siteCollection = new SPSite(SPContext.Current.Web.Url))
    {
        using (SPWeb web = siteCollection.OpenWeb())
        {
        } // SPWeb object web.Dispose() automatically called
    }  // SPSite object siteCollection.Dispose() automatically called
}
foreach statement and try/finally

Unfortunately foreach can’t implement using() so extra measures must be taken to make sure your code makes it to the intended Dispose by using the following try/finally pattern in your code.  Use extra caution with any code that is part of a iteration such as foreach because they have a way of turning a small problem in dev into a big problem in production especially when called with navigation or on SharePoint environments with lots of sites.

public void TryFinallyBestPractice()
{
    using (SPSite siteCollection = new SPSite("https://moss"))
    {
        using (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            foreach (SPWeb innerWeb in outerWeb.Webs)
            {
                try //should be 1st statement after foreach
{
// ... something evil occurs here
}
finally
{
if(innerWeb != null)

                        innerWeb.Dispose();
                }
            }
        } // SPWeb object outerWeb.Dispose() automatically called
    }  // SPSite object siteCollection.Dispose() automatically called
}

SharePoint Memory Internals

If you’ve made it this far without falling asleep you might be interested in learning more about the SharePoint internals of memory management and why it’s so important for you (the owner of the application code) to know when is the optimal time for performance reasons to hold and release your SharePoint objects.  As SharePoint Application developers you are familiar with the public SPSite and SPWeb objects but internally to get their work done the real managed object that holds onto the unmanaged heap is SPRequestInternalClass which is internal to a wrapper class SPRequest.

Internally we use a RCW (runtime callable wrapper) which is essentially a finalizable object (there are subtle differences but they don’t really come into play here).    If that object is no longer rooted, the finalizer (teardown of the RCW) will release the native memory.   However, like normal finalizers the RCW teardown is performed by a single finalizer thread which generally can’t keep up with cleaning these objects if they’re being leaked many times per second.

Not releasing the SharePoint objects in a timely fashion can lead to poor memory hygiene including excessive fragmentation, pinning, and OOM exceptions building very quickly.  Problematic code which fails to properly dispose objects in a timely fashion becomes exacerbated especially in x32bit environments with a large number of sites.  We encourage all Enterprise SharePoint farms to use x64bit versions of the OS and MOSS to take advantage of the additional addressable memory.

Resources other than just memory are effected by not properly disposing memory.  For example, SQL Server establishes a 1:1 connection with each SPRequest object and it lives up to the release of the internal SPRequest object (which is used by your SPSite and SPWeb object).

If you’d like to read more perspective on SharePoint internals and also discover ways to look for evidence of dispose related memory leaks in the ULS logs I’d encourage you to read Stefan Goßner’s excellent blog on Disposing SPWeb and SPSite objects .

Special thanks to my colleagues at Microsoft Dave Aknai, Paul Andrew, Todd Carter, Shawn Cicoria, Stefan Goßner, Scott Harris, Vishwas Kulkarni, Zach Kramer, Randy Thomson, and Sean Thompson.