<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Chris Forbes blogs on SharePoint &#38; .NET &#187; Dispose Checker</title>
	<atom:link href="http://chrisforbesblogs.net/tag/dispose-checker/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrisforbesblogs.net</link>
	<description>Developers chat on SharePoint and .NET</description>
	<lastBuildDate>Tue, 08 May 2012 10:58:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='chrisforbesblogs.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/3aa4775025bad14a027d0ade34632a33?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Chris Forbes blogs on SharePoint &#38; .NET &#187; Dispose Checker</title>
		<link>http://chrisforbesblogs.net</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://chrisforbesblogs.net/osd.xml" title="Chris Forbes blogs on SharePoint &#38; .NET" />
	<atom:link rel='hub' href='http://chrisforbesblogs.net/?pushpress=hub'/>
		<item>
		<title>Using the SharePoint dispose checker tool</title>
		<link>http://chrisforbesblogs.net/2009/06/08/using-the-sharepoint-dispose-checker-tool/</link>
		<comments>http://chrisforbesblogs.net/2009/06/08/using-the-sharepoint-dispose-checker-tool/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 19:12:57 +0000</pubDate>
		<dc:creator>Chris Forbes</dc:creator>
				<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Dispose Checker]]></category>
		<category><![CDATA[IDispose]]></category>

		<guid isPermaLink="false">http://chrisforbesblogs.wordpress.com/2009/06/08/using-the-sharepoint-dispose-checker-tool/</guid>
		<description><![CDATA[Objects using IDisposable A number of objects within the SharePoint object model implement the IDisposable interface. For the on-going performance of your SharePoint server you need to ensure you remember to correctly dispose of these objects within your code.  I guess this has always been important for any application you are building but its a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisforbesblogs.net&#038;blog=6742676&#038;post=69&#038;subd=chrisforbesblogs&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2><strong>Objects using IDisposable</strong></h2>
<p><a href="http://chrisforbesblogs.files.wordpress.com/2009/06/rubbish.jpg"><img style="border-right:0;border-top:0;display:inline;margin-left:0;border-left:0;margin-right:0;border-bottom:0;" title="rubbish" src="http://chrisforbesblogs.files.wordpress.com/2009/06/rubbish_thumb.jpg?w=170&h=128" border="0" alt="rubbish" width="170" height="128" align="right" /></a> A number of objects within the SharePoint object model implement the <strong>IDisposable</strong> interface. For the on-going performance of your SharePoint server you need to ensure you remember to correctly dispose of these objects within your code.  I guess this has always been important for any application you are building but its a bit more crucial in SharePoint as your building your solutions on a platform that isn&#8217;t just servicing your application but could be many.</p>
<p> </p>
<h2><strong>So what’s the problem?</strong></h2>
<p>So what if you don’t dispose of your objects? Well your just using up memory that could have been released. So without recycling your application pool you run the risk of slowly degrading the performance of your SharePoint farm. <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' />  In fact your application pools will automatically recycle when they hit a particular memory usage threshold, which could then result in slow page loads as pages are re-compiled by the JIT when requested.</p>
<p><strong> </strong></p>
<h2><strong>How to spot the problem?</strong></h2>
<p>If your spotting your SharePoint server application pools recycling frequently or your servers performance degrades during heavy usage then this could be an indication that your custom\3rd party code\web parts are not correctly disposing their objects.</p>
<p>You can check the ULS logs at <a href="http://chrisforbesblogs.co.uk/2009/04/27/12-hive-quick-file-path-reference/">12 hive</a> \LOGS. Your looking for entries related to <strong>SPRequest</strong>. It might be complaining about the number of SPRequests objects exceeding a configurable threshold and so on. You can increase this threhold setting through the registry. But first, try and use this as an indicator that some of your code or indeed a 3rd partys code such as some web parts etc. are not correctly disposing the SPSite and SPWeb objects. So look at this first before cranking this level up.</p>
<p>If you do want to change the threshold then look for the following sub key in your registry using regedit…</p>
<p> <em>HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Shared Tools\Web Server Extensions\HeapSettings</em><br />
 <br />
<em>LocalSPRequestWarnCount = &lt;set to the level you want&gt;</em></p>
<h2>How to dispose your objects</h2>
<p>Ok so that the problem, and how to determine if your getting the problem covered.  So how do you ensure your code isn’t leaving your SPSite and SPWeb objects ‘indisposed’. Well, as mentioned previously, both of these objects implement the <strong>IDisposable</strong> interface so you can go ahead and call the .<strong>Dispose</strong> method when your done with the object. Good practice would be to put this in a try, catch finally block…</p>
<p> </p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">SPSite site = <span style="color:#0000ff;">null</span>;

<span style="color:#0000ff;">try</span>{   site = <span style="color:#0000ff;">new</span> SPSite(<span style="color:#006080;">"http://litwaredemo"</span>);

   Console.WriteLine(<span style="color:#006080;">"got a handle on the site {0} : {1}"</span>, 

   site.Title. site.Url);

}

<span style="color:#0000ff;">catch</span>(Exception ex)

{ 

  <span style="color:#008000;">//Catch exceptions here</span>

}

<span style="color:#0000ff;">finally</span>

{  

 <span style="color:#0000ff;">if</span> (site != <span style="color:#0000ff;">null</span>) site.Dispose();

}</pre>
<p> </p>
<p>Another approach, which I prefer, is to use the using statement. This will ensure the dispose is taken care of without you worrying about it. Like so…</p>
<p> </p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;"><span style="color:#0000ff;">using</span> (SPSite site = <span style="color:#0000ff;">new</span> SPSite(<span style="color:#006080;"><a href="http://litwaredemo">http://litwaredemo</a></span>)

{

    Console.WriteLine("got a handle on the site {0} : {1},

    site.Title, site.Url);

}</pre>
<p> </p>
<p>The one catch (if you’ll pardon the pun) to dispose, is to only take care of the objects that you have instantiated, if your grabbing a handle from things like SPContext then you don’t need to worry about disposing this, let SPContext take care of it.</p>
<p> </p>
<div id="codeSnippetWrapper">
<pre style="font-size:8pt;overflow:visible;width:100%;color:black;direction:ltr;line-height:12pt;">SPSite site = SPContext.Site;Console.WriteLine(<span style="color:#006080;">"got a handle on {0} : {1}"</span>,

site.Title, site.Url);

//Don’t dispose site, leave this to SPContext to worry about.</pre>
<p> </p>
<h2>Using the SharePoint Dispose Checker Tool</h2>
<p>Microsoft have released a neat console application which you can add into Visual Studio and have check your code once your all done. It will output any suspect methods which may not be disposing these objects correctly. You need to look at the output as it may be reporting some false positives, for example you may have some factory methods which instantiate these objects and return them. But I find this tool invaluable!</p>
<p>You can download the link from MSDN here <a href="http://code.msdn.microsoft.com/SPDisposeCheck" target="_blank">Dispose Checker Tool</a> To add this into Visual Studio just…</p>
<p>1. select<em> Tools\External Tools</em> from the menu option in VS. Then click add.</p>
<p>2. Go ahead and give the tool a friendly name (include an &amp; next to a one of the letters if you want to use a keyboard shortcut).</p>
<p>3. in the command box add the correct path to the checker executable</p>
<p>  eg C:\Program Files\Microsoft\SharePoint Dispose Check\SPDisposeCheck.exe</p>
<p>4.In the Arguments box select the menu icon and select ‘Project Directory</p>
<p>5. Check the ‘Use output window’ to have the results kept within VS.</p>
<p>6. And finally, select ‘prompt for arguments’ this is useful if you need to add additional arguments such as –debug etc.</p>
<p> </p>
<p>Then once your done writing your code, just compile as normal then run the tool. Below is an example output which is telling me I haven’t disposed of the xxSite object. I have highlighted the important bits in red…</p>
<p> </p>
<table border="1" cellspacing="0" cellpadding="2" width="472">
<tbody>
<tr>
<td width="470" valign="top">Line: 121Notes:      Disposable type not disposed: Microsoft.SharePoint.SPSite     ***This may be a false positive depending on how the type was created or if it is disposed outside the current scope</p>
<p>More Information: <a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110">http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>ID: SPDisposeCheckID_110</p>
<p>Module: LookingAtWebFields.exe</p>
<p>Method: ConsoleApplication1.Program.Main(System.String[])</p>
<p><strong><span style="color:#ff0000;">Statement: XXsite := new Microsoft.SharePoint.SPSite(&#8220;</span></strong><a href="http://litwaredemo&quot;)"><strong><span style="color:#ff0000;">http://litwaredemo&#8221;)</span></strong></a></p>
<p>Source: C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\Lab Work\LookingAtWebFields\LookingAtWebFields\Program.cs</p>
<p>Line: 121</p>
<p>Notes:      <strong><span style="color:#ff0000;">Disposable type not disposed: Microsoft.SharePoint.SPSite</span></strong></p>
<p>     ***This may be a false positive depending on how the type was created or if it is disposed outside the current scope</p>
<p>More Information: <a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110">http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx#SPDisposeCheckID_110</a></p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Total Found: 2</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Modules Checked: 3</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>LookingAtWebFields.exe</p>
<p>LookingAtWebFields.vshost.exe</p>
<p>LookingAtWebFields.exe</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Modules Ignored: 0</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-</p>
<p>Methods Ignored: 0</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</td>
</tr>
</tbody>
</table>
<p> </p>
<p>So there you go, hope this document has been of some help. Happy SharePoint coding <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p> </p>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:f11bdee3-5313-4af7-9ff7-e83768bf680e" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">Technorati Tags: <a rel="tag" href="http://technorati.com/tags/SharePoint">SharePoint</a>,<a rel="tag" href="http://technorati.com/tags/IDispose">IDispose</a>,<a rel="tag" href="http://technorati.com/tags/SharePoint+Dispose+Checker+Tool">SharePoint Dispose Checker Tool</a></div>
<div id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:5b196733-5dfa-44c1-a367-4a5d5ce1b6f0" class="wlWriterEditableSmartContent" style="display:inline;float:none;margin:0;padding:0;">Flickr Tags: <a rel="tag" href="http://flickr.com/photos/tags/SharePoint">SharePoint</a>,<a rel="tag" href="http://flickr.com/photos/tags/IDispose">IDispose</a>,<a rel="tag" href="http://flickr.com/photos/tags/SharePoint+Dispose+Checker+Tool">SharePoint Dispose Checker Tool</a></div>
</div>
</div>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/chrisforbesblogs.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/chrisforbesblogs.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/chrisforbesblogs.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/chrisforbesblogs.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/chrisforbesblogs.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/chrisforbesblogs.wordpress.com/69/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/chrisforbesblogs.wordpress.com/69/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/chrisforbesblogs.wordpress.com/69/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=chrisforbesblogs.net&#038;blog=6742676&#038;post=69&#038;subd=chrisforbesblogs&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://chrisforbesblogs.net/2009/06/08/using-the-sharepoint-dispose-checker-tool/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/ed3e25ef8a88f8fff819a7722f1805a9?s=96&#38;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">chrisforbesblogs</media:title>
		</media:content>

		<media:content url="http://chrisforbesblogs.files.wordpress.com/2009/06/rubbish_thumb.jpg" medium="image">
			<media:title type="html">rubbish</media:title>
		</media:content>
	</item>
	</channel>
</rss>
