When you doing custom development in SharePoint like
webparts, event receiver, etc.. it is important to consider memory leaks. Creating
the component in safe way lead to improve the performance. There are some points we can consider to
avoid memory leaks.
1.
Use Dispose() method.
2.
Use SPDisposeCheck utility to identify issues.
Dispose() is not related to SharePoint, it is provided in
.NET. As a .NET developer we have to know how to correctly release the
resources while working with the objects. .NET framework has a concept of
garbage collection, provided by Common Language Runtime. Whenever you create an
instance of managed type, when it is no longer used, the Garbage Collector will
automatically release the allocated memory. But this is non deterministics
(unpredictable).
.NET framework provide IDisposable interface which expose a
Dispose() method that you can call explicitly to release the unmanaged
resources.
To use Dispose technique you can follow one of the following
methods.
1. Use the using keyword
Ex:
using (SPSite site = new SPSite(“http://address”))
{
//your code
}
2. Use try/finally code block
Ex:
SPSite site = null;
try
{
site = new SPSite(“http://address”);
//your code
}
finally
{
if (site != null)
site.Dispose();
}
3. Explicitly call Dispose() method.
Ex:
SPSite site = new SPSite(“http://address”);
//your code
site.Dispose();
SPDisposeCheck is a tool that helps the developers and
administrators to check custom SharePoint solutions that use the SharePoint
Object Model. It measures against dispose best practices. Following is the way
how to use SPDisposeCheck utility.
SPDisposeCheck is a command line tool normally installed to
this location “C:\Program Files (x86)\Microsoft\SharePoint Dispose Check”.
Go to any dll that you have developed using Visual Studio. My
dll path is: “C:\Users\t-ictdev05\Documents\Visual Studio
2010\Projects\TestSPProjects\BlogPost\BlogPost\bin\Debug”.
Now open the command prompt and enter the following command:
“C:\Program Files (x86)\Microsoft\SharePoint Dispose Check\ SPDisposeCheck.exe”
“C:\Users\t-ictdev05\Documents\Visual Studio
2010\Projects\TestSPProjects\BlogPost\BlogPost\bin\Debug”
It will show the problems in
the specified dll.
If you want you can write
this result to a txt file by using following command.
“C:\Program Files (x86)\Microsoft\SharePoint Dispose Check\ SPDisposeCheck.exe”
“C:\Users\t-ictdev05\Documents\Visual Studio
2010\Projects\TestSPProjects\BlogPost\BlogPost\bin\Debug” >result.txt
By using this utility you can identify what are the object
we can dispose to develop a safe code.
No comments:
Post a Comment