Thursday, January 14, 2010

Dispose SPSite and SPWeb Check Tool

I read something pretty interesting. Scot Hillier in a presentation said some of the things that drive a SharePoint consultant crazy are:

  1. Running in Full trust. (Drives me nuts, read here.)
  2. Not automating web.config settings. (Very true, read here.)
  3. Deploying non-Release code to production. (Doh – do not forget!)
  4. Deploying to the GAC. (Drives me nuts, read here.)
  5. Not using Feature and Solutions properly to do deployments. (My addition)
  6. Forgetting to run SPDisposeCheck. (???)

Given I had recently seen some issues in SharePoint production environments where connections were not being closed, I wonder what was SPDisposeCheck? I had never heard of it and sounded useful.

SPDisposeCheck was developed by Microsoft and provided to the community to avoid this issue. It will check all of your assemblies to see if SPSite and SPWeb objects are not being disposed of correctly. Here is information for using it:

If you are seeing performance problems on your SharePoint sites, check your logs! If you see the following errors objects are not being disposed correctly and memory is being consumed:

ERROR: request not found in the TrackedRequests. We might be creating and closing webs on different threads. ThreadId = 21, Free call stack = at Microsoft.SharePoint.SPRequestManager.Release(SPRequest request) at Microsoft.SharePoint.SPSite.Close() ......

An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. This object will now be disposed. Allocation Id: {B274FEBF-1D42-463C-B0C3-7A9005371494} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.

An SPRequest object was reclaimed by the garbage collector instead of being explicitly freed. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it. Allocation Id: {617E63C9-32A3-42AE-AEEB-BC41CB798C88} To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings. Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.

Here is a good blog that goes into the details of the error. The error is associated to not disposing SPSite and SPWeb objects correctly in custom code. This is extremely important and it can take down a SharePoint site.

No comments: