In this article we can get awareness over the Dispose Checker Tool which is a useful tool in finding non-disposed objects inside SharePoint projects.
What is the Dispose Checker Tool?
Dispose Checker Tool checks your project for unreleased SharePoint objects and reports them as errors. You can integrate this tool with Visual Studio 2010 and there
is automatic error reporting on the build event.
Following are the objects that are checked for disposal:
Why we need this tool?
You might be thinking that why we need this tool although we have .Net Garbage Collector in place. Yes! It is true that there is .Net automatic garbage collection.
But there is a risk involved of unreleased objects occupying too much memory. This may lead to application pool restart occasionally creating delays during responses.
For example the following code induces non-disposed object:
Plus there are unmanaged memory occupied by the managed server objects resulting in very high memory consumption. To be sleek with memory it is then advised to release memory.
So the best way of using SharePoint objects is to release the objects after code exit. The
using statement is the recommended solution.
The other solution is to manually call the Dispose() method.
Where to download the tool?
You can download the tool from CodePlex and as there are many versions choose the right version appropriate for your SharePoint binaries.
Please note that sometimes the latest version may not work well with your binaries. The following link contains a valid download tested with SharePoint 2010
and Visual Studio 2010: http://archive.msdn.microsoft.com/SPDisposeCheck/.
How to use the tool?
Install the setup from downloaded from the above link and please remember to check the following options:
On selecting the above options you will get the convenience of:
- Using it from Tools menu as a Visual Studio add-in
- Automatic execution of tool on each build
Testing the tool
You can create a new SharePoint 2010 Console Application. Add the following code inside the main method.
static void Main(string args)
SPSite site = new SPSite("http://localhost");
SPWeb web = site.OpenWeb();
Build the project to continue.
Accessing the tool
You can access the tool from Tools menu of Visual Studio.
On accessing the tool it will show the current project as Dispose Check enabled.
Click on the Save and Analyze button to start analyzing your project. You should be getting the following results in the Error List.
The error denotes the need for disposing object and you can see there is an MSDN link to learn more about disposing objects.
Solving the error
You can use the Dispose method of each objects or use the using statement to get rid of errors.
Try building the project and you should see that the errors from Error List disappeared. (If automatic dispose check was selected while installation.)
You can practice this by opening your existing project and running the tool. If there are dispose check errors you should correct them.
Note: The whole idea behind using this tool is to make the code efficient by disposing unwanted objects after scope.
The examples over MSDN are having using clause to support the dispose pattern. You can refer the associated MSDN link for more information.
In this article we have explored the Dispose Checker Tool for SharePoint 2010. Following are the points to be summarized about Dispose Checker Tool.
- Helps in finding non-disposed objects
- Integrates well with Visual Studio 2010
- The tool is free
The attached source code contains valid and invalid scenarios. You can check the errors running the Dispose Checker tool after building.
Jean Paul is a Microsoft MVP and Architect with 12+ years of experience. He is very much passionate in programming and his core skills are SharePoint, ASP.NET & C#.
In the academic side he do hold a BS in Computer Science & MBA. In the certification side he holds MCPD & MCTS spanning from .Net Fundamentals to SQL Server.
Most of the free time he will be doing technical activities like researching solutions, writing articles, resolving forum problems etc. He believes quality & satisfaction goes hand in hand.
You can find some of his work over here. He blogs at http://jeanpaulva.com