In this article we can get awareness over the Dispose Checker Tool which is a useful tool in finding undisposed objects inside SharePoint projects.
What is 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 undisposed 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.
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 AddIn
· 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.
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.
SharePoint Dispose Checker Tool
Dispose Patterns by Example
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 undisposed 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.