A memory leak in SharePoint code?

I'm just curious about how to detect a memory leak in code. I am working on code written by someone else and they told him that it has a memory leak. I look through the code to see if it has a memory leak.

In this case, the code has a memory leak. I need to close SPWEB objects here.

     private bool isSubSite()
    {

        SPWeb currWeb = SPContext.Current.Web;
        SPWeb rootWeb = currWeb.Site.RootWeb;

        if (currWeb.ID != rootWeb.ID)
            return true;
        return false;
    }
+4
source share
3 answers

Edit: Sorry, I just noticed what you get SpContext.Current.Web. This is a shared resource, and you should not call it dispose, as @Servy pointed out. Let the structure take care of this for you.


SPWeb. Dispose , , using, :

  private bool isSubSite()
{

    using (SpWeb currWeb = SpContext.Current.Web){
        using (SPWeb rootWeb = currWeb.Site.RootWeb){

            if (currWeb.ID != rootWeb.ID)
                return true;
            return false;
        }
    }
}

, using, try...finally finally, , .

, Dispose, Close SPWeb . , .

+1

, .

, SPWeb. , , SPWeb , . , SPWeb , .

+5

Simplify the code:

private bool isSubSite()
    { return SPContext.Current.Web.ID != currWeb.Site.RootWeb.ID;}

It is not possible to take a look at your code to determine if it could be a source of memory leak (but only the temporary references are used in the code above).

-2
source

All Articles