Finding out if SharePoint Objects are Disposed Correctly

27 February, 2009 · 1 minute to read

Finding out if SharePoint Objects are Disposed Correctly

Everyone should know by now that certain SharePoint objects, including SPSite and SPWeb need to be disposed of when you have finished with them or you will get a memory leak. If you want to refresh your memory have a look at the MSDN white papers Best Practices: Using Disposable Windows SharePoint Services Objects on MSDN and Best Practices: Common Coding Issues When using the SharePoint object model.

What you may not know is that there are a couple of tools/techniques which can help you find areas in your code where you are not disposing of objects correctly.
 

  1. SPDisposeCheck. This is a tool which will analyze your assemblies to search for coding patterns which lead to objects not being disposed correctly. It's a great tool and worth running regularly on your assemblies.
     
  2. SPRequestStackTrace registry value. Creating the registry key

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings
SPRequestStackTrace: DWORD = 1 

Will cause the SharePoint runtime to log a stack trace everytime a SPRequest object is not disposed of correctly. This will actually pick up all the types of objects not being disposed of as internally the problem with not disposing of them is that they contain a reference to SPRequest.

 

At this point I have to say thank you to Jared Kahlil who pointed out a memory leak in the SLK by using the second method above and started my research into both of these methods. Unfortunately we can't use SPDisposeCheck on SLK as it checks to see if the file name begins with Microsoft and if there are any namespaces beginning with Microsoft, and if so ignores the assembly.
 

 

Richard Willis headshot

Written by Richard Willis

Managing Director / Founder

Richard started SalamanderSoft in 2007 after a successful career as a software developer. Wanting to start his own company and with experience in integrating school systems he set out to build the best integration system for schools and to exceed customer expectations. He starting out on his own, doing all the coding, support and sales until finally the growing number of customers meant he needed to start growing the team. He is still heavily involved in coding the core Integration Suite product in addition to running the company and being the first point of contact for prospective customers.

Copyright © 2020 SalamanderSoft Limited