locked
Memory Leaks - Best Practices RRS feed

  • Question

  • Hello,

    i've a question about an example in the article Disposing Objects (found here: http://msdn.microsoft.com/en-us/library/ee557362.aspx):

    void SPSiteCollectionAddNoLeak()
    {
        SPWebApplication webApp = new SPSite("http://moss").WebApplication;
        SPSiteCollection siteCollections = webApp.Sites;
        using (SPSite siteCollection = siteCollections.Add("sites/myNewSiteCollection", "DOMAIN\\User",
          "roger.lamb@litwareinc.com"))
        {
        } // SPSite object siteCollection.Dispose() automatically called.
    }

    In the example above a new SPSite Object is created (new SPSite(...).WebApplication) but only the returned WebApplication is used. Is this correct, because the created SPSite is not disposed. Maybe it is not necessary to call Dispose on the SPSite-Object, because the Object is not assigned to a variable. But I'm not sure ...

    Thanks for clarification.


    -
    • Edited by Marc A. Zhou Saturday, September 24, 2011 10:01 AM
    Saturday, September 24, 2011 10:00 AM

Answers

  • Hi Marc.
               
                One of the case when memory leak happens is due to SPSite objects, which are not disposed correctly.  So code present in msdn article will retrieve SPSite object first and then it will return WebApplication. And SPSite object has to disposed, which is not mention in above code. Potential memory leak.
    In article it is also mention that "This best practice addresses the issue identified by the SharePoint Dispose Checker Tool"  
    I verified in that tool and it is also suggesting that as memory leaks. 
    Regards
    Milan Chauhan.
    Saturday, September 24, 2011 11:29 AM
  • Hi Milan,

    thanks for the reply. I know, I did the same test and the SDCT reported me the error SPDisposeCheckID_110

    The correct code should be:

    public static void AddWithNoMemLeak()
    {

       using (SPSite targetSite = new SPSite("..."))
       {
          SPSiteCollection sites = targetSite.WebApplication.Sites;
          using (SPSite site = sites.Add("...", "...", ""))
          { }
       }
    }

    - OR -

    public static void AddWithNoMemLeak()
    {
        SPSite targetSite = null;
        try
        {
            targetSite = new SPSite("...");
            SPSiteCollection sites = targetSite.WebApplication.Sites;
            using (SPSite site = sites.Add("...", "...", "..."))
            {

            }
        }
        catch { }
        finally
        {
            if (targetSite != null)
                targetSite.Dispose();
        }
    }

    I prefer the first version.


    -
    • Marked as answer by Zizhuoye Chen Wednesday, October 5, 2011 11:57 AM
    Saturday, September 24, 2011 3:06 PM

All replies

  • Hi Marc.
               
                One of the case when memory leak happens is due to SPSite objects, which are not disposed correctly.  So code present in msdn article will retrieve SPSite object first and then it will return WebApplication. And SPSite object has to disposed, which is not mention in above code. Potential memory leak.
    In article it is also mention that "This best practice addresses the issue identified by the SharePoint Dispose Checker Tool"  
    I verified in that tool and it is also suggesting that as memory leaks. 
    Regards
    Milan Chauhan.
    Saturday, September 24, 2011 11:29 AM
  • Hi Milan,

    thanks for the reply. I know, I did the same test and the SDCT reported me the error SPDisposeCheckID_110

    The correct code should be:

    public static void AddWithNoMemLeak()
    {

       using (SPSite targetSite = new SPSite("..."))
       {
          SPSiteCollection sites = targetSite.WebApplication.Sites;
          using (SPSite site = sites.Add("...", "...", ""))
          { }
       }
    }

    - OR -

    public static void AddWithNoMemLeak()
    {
        SPSite targetSite = null;
        try
        {
            targetSite = new SPSite("...");
            SPSiteCollection sites = targetSite.WebApplication.Sites;
            using (SPSite site = sites.Add("...", "...", "..."))
            {

            }
        }
        catch { }
        finally
        {
            if (targetSite != null)
                targetSite.Dispose();
        }
    }

    I prefer the first version.


    -
    • Marked as answer by Zizhuoye Chen Wednesday, October 5, 2011 11:57 AM
    Saturday, September 24, 2011 3:06 PM
  • Hi,

    Basically, as a rule of thumb, to avoid memory leaks, just remember this:

    - Dispose either calling the Dispose() method either with using blocks any object that you have instantiated or that has been instanciated indirectly

    When recalling this rule, you should be safe for most of the memory leaks.

    The other rule of thumb is:

    - Never dispose a SPSite/SPWeb object that you did not instantiate (example SPContext...properties.web etc...) because this can be even worst than leaving a memory leak.

    Best Regards


    http://www.silver-it.com - My blog http://sptoolbasket2010.codeplex.com - Rating for 2007 http://sptoolbasket.codeplex.com - Tools for 2010
    Saturday, September 24, 2011 6:22 PM
  • Hi Marc,

    You can refer to the below msdn link which gives a description on disposing SharePoint objects.

    http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx

    Hope this is useful !

    Monday, September 26, 2011 7:48 AM