Andrew Connell [MVP SharePoint]
1579 Posts |  42 Articles |  4864 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
SharePoint Quick Links
Article Categories
Archives
Post Categories


Add to Technorati Favorites

[via Paul Andrew]

Today Microsoft released a tool on MSDN Code Gallery called SPDisposeCheck. It will look at your code in a SharePoint to make sure you are properly disposing objects to ensure you aren't consuming more memory than you need to be. This tool is incredibly helpful as this one one issue that many customers run into unknowingly. IMHO, all your projects should be run though SPDisposeCheck before deployment.

Microsoft is also updating the dispose best practices article in the SDK that will walk you through how to use this tool as well as updated info along disposing of SharePoint objects.

Run, don't walk, and download this tool today!

» MSDN Code Gallery: SharePoint Dispose Checker

posted on Thursday, January 29, 2009 1:40 PM

Feedback

# re: SPDisposeCheck - This should be in EVERY SharePoint developer's toolbox 1/30/2009 8:21 AM Kristian Kjær
Gravatar A little warning (beware that the observations below are from a pre-release version of the tool):

Don’t use the tool without proper considerations. Especially you should notice:
1. If the tool says you have 0 (zero) errors (memory leaks) you should NOT be happy. The tool tells you for example to dispose uses of the RootWeb property. This is not always right. You should NOT dispose SPContext.Current.Site.RootWeb. If you do this and SPContext.Current.Site.RootWeb == SPContext.Current.Web you get an exception.
2. It does NOT find all memory leaks. In fact it only finds the most obvious leaks. Consider the following pattern (and yes, it is not good code, but I have seen it A LOT in various "live" systems):

public List<SPListItem> GetItems(SPQuery query) {
using (SPSite site = new SPSite(“http://localhost”) {
using (SPWeb web = site.RootWeb) {
var items = web.Lists[“MyList”].GetItems(query);
return items.Cast<SPListItem>().ToList();
}
}
}

public string DoSomething(SPQuery query) {
var sb = new StringBuilder();
var items = GetItems(query);
foreach(var item in items) {
sb.AppendLine(item[“Title”]);
}
return sb.ToString();
}


In this case you have a memory leak and the tool won’t find it. For those who don’t see where the leak is, it arises when the method, DoSomething, uses the SPListItem. Internally MOSS will recreate the owner SPWeb object, but won’t dispose it. The user has to do this (or better, write better code!) by calling item.ParentList.ParentWeb.Dispose()!!

# re: SPDisposeCheck - This should be in EVERY SharePoint developer's toolbox 1/30/2009 5:59 PM AC [MVP MOSS]
Gravatar Kristan-
First, you should test against the live tool as #1 was fixed in the released version. As for #2, read the article I linked to... it provides guidance around this. It was never meant to find ALL leaks and cases of dispose... just as the article says.

# re: SPDisposeCheck - This should be in EVERY SharePoint developer's toolbox 1/31/2009 1:15 AM Michhes
Gravatar Heads up, the dispose best practices article is now live.

Also worth noting Roger Lamb's dedicated blog post on this subject has been at the forefront of this topic for a long time now, communicating the latest revisions prior to their incorporation into the guidance.

http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx

# re: SPDisposeCheck - This should be in EVERY SharePoint developer's toolbox 1/31/2009 8:32 AM AC [MVP MOSS]
Gravatar Michhes-
Agreed... hence why it's linked on the SPDisposeCheck download page. I suspect you haven't looked at the new download page or the new release before commenting.

# re: SPDisposeCheck - This should be in EVERY SharePoint developer's toolbox 2/2/2009 5:38 PM Matt
Gravatar Definitely cool stuff! I've added an AfterBuild target to my projects so that I can get this to run each time I build, without having to mess with the command line, a la:

<Target Name="AfterBuild">
<Exec Command='"C:\Program Files\Microsoft\SharePoint Dispose Check\SPDisposeCheck.exe" "$(TargetPath)"' />
</Target>

Thanks for sharing the links.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 8 and 6 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2010 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 5-day hands-on and online WCM classes for developers I offer through my company: Critical Path Training.

Get more information on the WCM courses!