Automate SharePoint Dispose() code reviews with SPDisposeCheck

Today the Microsoft SharePoint Product Team announced the SPDisposeCheck utilityhere and also at Paul Andrew’s blog here. You can download the free SPDisposeCheck utility in the MSDN Code Gallery https://code.msdn.microsoft.com/SPDisposeCheck .

The SPDisposeCheck utility will assist you dig through your custom SharePoint MSIL assemblies (DLL’s and EXE’s) looking for areas in your code that may require “closer examination” and might lead to Dispose() related memory leaks.  It’s important to note that this tool is not a replacement for developers to learn and understand the ins and outs of the SharePoint Dispose() guidance and how it is implemented in their custom code.  A manual code review is still required to cast out ‘false positives’ that the tool may produce in the output report.  During internal field tests we received feedback that the tool can help identify areas in the code that may have been overlooked even by the most experienced SharePoint developers. As a final step in development we encourage you to frequently review your ULS logs for Dispose() related leaks that have occurred while the code is executing.  Note SharePoint’s instrumentation writes to the ULS logs when it finds SPSite and SPWeb objects that were not disposed as expected.

The original MSDN article Best Practices: Using Disposable Windows SharePoint Services Objects was updated 1/30/09 and the Dispose() guidance is in sync with the content on this blog and the SPDisposeCheck tool.  If you have not done so in a while check out my original blog post “SharePoint 2007 and WSS 3.0 Dispose Patterns by Example” which has several updated Dispose() guidance and edge cases including RootWeb and ParentWeb changes.  You can find all the source code examples on this blog in the Visual Studio 2008 solution SPDisposeExamples packaged with the SPDisposeCheck utility installer.

Understanding the latest Dispose() guidance from Microsoft, scanning with SPDisposeCheck frequently throughout the Software Development Life Cycle (SDLC), and examining the SharePoint ULS logs for evidence of Dispose() related leaks during runtime can help you reduce the probability of unnecessary memory pressure resulting in poor performing SharePoint applications.