Geeks With Blogs
Dennis Bottjer | MVP

The .NET Framework allows developers to write managed code.  The term managed refers to how memory is handled by a computer system.  For example, C/C++ code is commonly known as unmanaged because developers must allocate and de-allocate memory.  In contrast, the .NET Framework provide a mechanism known as the Garbage Collector (GC) to de-allocate or collect object no longer in use.

I have found it particularly helpful to consider SharePoint a very large ASP.NET Application.  If you can visualize SharePoint as an ASP.NET application it becomes familiar and relatable.  If you begin to deconstruct SharePoint you will notice that it is comprised of Active Server Pages (ASPX), Master Pages, Style Sheets (CSS), User Controls (ASCX), Server Controls, Web Services (ASMX), Handlers, etc.  All these artifacts are common to ASP.NET developers.

SharePoint provides a rich API for developers to program against and extend this large "ASP.NET Application."  Two object commonly used in SharePoint development are SPSite and SPWeb.  Despite the names of these objects it must be understood that an SPSite object represents a site collection.  While an SPWeb object represent a site beneath a site collection.  Both of these object are managed wrappers of unmanaged code.  Furthermore, both of these object implement IDisposible.  So now for the million dollar question... When should you as a developer explicitly dispose of these object?  Well, the answer is, it depends.    

   1: String title;
   3: using(SPSite siteCollection = new SPSite("http://mossdev"))
   4: {
   5:   using(SPWeb site = siteCollection.OpenWeb())
   6:    {
   7:        title = site.Title;
   8:    }
   9: }  


In the example above we created a new SPSite and SPWeb object.  We explicitly created a new SPSite object while we called the OpenWeb() method of SPSite to return a new SPWeb Object.  Since in effect we created new instances of both object we should dispose of both objects.   Note that, wrapping the objects within a using statement will automatically dispose of that object when it is no longer needed.   A good example of when not to dispose of an SPSite or SPWeb object is when a reference to an existing object is returned from a method call.  For example, the method call SPControl.GetContextSite() will return a reference to an SPSite object we did not create therefore we should not dispose of this particular SPSite object.  Disposing of this particular instance will crash current page and possibly the site.

Disposing of SharePoint objects such as SPSite and SPWeb will free both managed and unmanaged resources as soon as possible which will increase system performance and scalability.

Posted on Wednesday, June 18, 2008 4:09 AM SharePoint , ASP.NET | Back to top

Comments on this post: Disposing of SharePoint Objects

# re: Disposing of SharePoint Objects
Requesting Gravatar...
See some guidelines here
Left by Bijay on Aug 24, 2011 1:02 AM

Your comment:
 (will show your gravatar)

Copyright © DennisBottjer | Powered by: