I am just wondering how to find out a memory leak in a code. I am working on a code written by someone else and been told that it has a memory leak. I am going through the code to see if it has a memory leak.
does following code has memory leak. do i need to close the 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;
}
Edit: Sorry, I've just noticed you're getting
SpContext.Current.Web
. That is a shared resource, and thus you should not call dispose on it, as pointed out by @Servy. Let the framework take care of that for you.You need to dispose of the
SPWeb
objects. You can either callDispose
yourself after you no longer need them, or you can use theusing
statement, which automatically disposes the object for you:Note that there is no problem returning inside the
using
, as this code will be translated into atry...finally
code block later with the call to dispose inside thefinally
block, thus making sure it will be executed regardless of function returning or exceptions thrown.Also, note that you should not call neither
Dispose
orClose
on sharedSpWeb
objects. As this is not the case, you can call them safely.