<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Roger Lamb's SharePoint Developer Blog</title><link>http://blogs.msdn.com/rogerla/default.aspx</link><description /><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>SharePoint 2007/2010 “Do Not Dispose Guidance” + SPDisposeCheck</title><link>http://blogs.msdn.com/rogerla/archive/2009/11/30/sharepoint-2007-2010-do-not-dispose-guidance-spdisposecheck.aspx</link><pubDate>Mon, 30 Nov 2009 05:25:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9930021</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9930021.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9930021</wfw:commentRss><description>&lt;p&gt;Below I put together a quick reference of the known SharePoint properties and methods that you &lt;strong&gt;do not&lt;/strong&gt; want to call Dispose against when developing SharePoint applications (AKA “&lt;strong&gt;&lt;em&gt;Do Not Dispose Guidance&lt;/em&gt;&lt;/strong&gt;”).&amp;nbsp; Please check back on this blog post as I will be keeping it updated as we move closer to SPS 2010 (SharePoint Server 2010) RTM (Release To Manufacturing).&amp;nbsp; I am communicating directly with the SharePoint Product Team and plan on validating the existing guidance for MOSS2007/WSS 3.0 against the new SPS2010/MSF2010 (Microsoft SharePoint Foundation 2010) OM (Object Model).&amp;nbsp; If you have additional questions on items you don’t see here please feel free to leave comments on this blog with code samples.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;SPDisposeCheck: &lt;/strong&gt;The current version v1.3.1 (January 2009) of &lt;a href="http://code.msdn.microsoft.com/SPDisposeCheck" target="_blank"&gt;SPDisposeCheck&lt;/a&gt; utility does &lt;strong&gt;not &lt;/strong&gt;scan for “&lt;strong&gt;&lt;em&gt;Do Not Dispose Guidance”&lt;/em&gt;&lt;/strong&gt;.&amp;nbsp; SPDisposeCheck does a great job drawing your attention to areas of your source code that potentially do not conform with our (Microsoft’s) guidance on Disposing SharePoint objects but you might not be fully aware that it’s not going to report any of the &lt;strong&gt;&lt;em&gt;“Do Not Dispose Guidance”&lt;/em&gt; &lt;/strong&gt;listed below.&amp;nbsp; Until we release the refreshed version of SPDisposeCheck that targets &lt;em&gt;“&lt;strong&gt;Do Not Dispose Guidance”&lt;/strong&gt;&lt;/em&gt; you will need to manually review the source code for the guidance on my &lt;a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx" target="_blank"&gt;blog&lt;/a&gt; and our &lt;a href="http://msdn.microsoft.com/en-us/library/aa973248.aspx" target="_blank"&gt;Dispose WhitePaper&lt;/a&gt;.&amp;nbsp; The current version of SPDisposeCheck was targeted and tested against MOSS2007/WSS 3.0 but we are working on a update now that SharePoint 2010 Beta 2 is now released to the public.&lt;/p&gt; &lt;p&gt;Remember it’s best practice to regularly review your ULS logs for Dispose() related leaks that have occurred during runtime.. SharePoint is instrumented to report on Dispose related leaks.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;em&gt;“Do Not Dispose Guidance”&lt;/em&gt; as of 11/30/2009:&lt;/strong&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;Applies to both MOSS2007/WSS 3.0 and SPS2010/MSF2010:&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;SPContext.Current.Site  &lt;li&gt;SPContext.Current.Web  &lt;li&gt;SPContext.Site  &lt;li&gt;SPContext.Web  &lt;li&gt;SPControl.GetContextWeb(..)  &lt;li&gt;SPControl.GetContextSite(..)  &lt;li&gt;SPFeatureReceiverProperties.Feature.Parent  &lt;li&gt;SPItemEventProperties.ListItem.Web  &lt;li&gt;SPList.BreakRoleInheritance()  &lt;ul&gt; &lt;li&gt;Do not call list.ParentWeb.Dispose()&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;SPListEventProperties.Web  &lt;li&gt;SPListEventProperties.List.Web  &lt;li&gt;SPSite.RootWeb  &lt;ul&gt; &lt;li&gt;Problems may occur when SPContext.Web has equality to the SPContext.Web.. make sure you dispose of SPSite and it will cleanup sub webs automatically&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;SPSite.LockIssue  &lt;li&gt;SPSite.Owner  &lt;li&gt;SPSite.SecondaryContact  &lt;li&gt;SPWeb.ParentWeb  &lt;li&gt;SPWebEventProperties.Web&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;&lt;strong&gt;Dispose Delta’s between WSS 3.0 and MSF2010:&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.siteadministrationselector_properties.aspx" target="_blank"&gt;Microsoft.SharePoint.WebControls.SiteAdminsitrationSelector.CurrentItem&lt;/a&gt; ** &lt;strong&gt;MSF2010&lt;/strong&gt; no Dispose() required  &lt;ul&gt; &lt;li&gt;When used with WSS 3.0 this property needs to call Dispose()&lt;/li&gt;&lt;/ul&gt; &lt;li&gt;SPItemEventProperties.OpenWeb() ** &lt;strong&gt;MSF2010 &lt;/strong&gt;no Dispose() required  &lt;ul&gt; &lt;li&gt;When using WSS 3.0 the SPWeb object returned by SPItemEventProperties.OpenWeb() does require a Dispose() (known edge case)  &lt;li&gt;MSF 2010 Best Practice: Use the newly introduced &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spitemeventproperties.web(office.14).aspx" target="_blank"&gt;SPItemEventProperties.Web&lt;/a&gt; property instead for better performance.. and just to be clear .. no explicit Dispose required&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9930021" width="1" height="1"&gt;</description></item><item><title>SharePoint Conference 2009 Developer Hands On Labs (HOLs)</title><link>http://blogs.msdn.com/rogerla/archive/2009/10/27/sharepoint-conference-2009-developer-hands-on-labs-hols.aspx</link><pubDate>Tue, 27 Oct 2009 14:27:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9913510</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9913510.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9913510</wfw:commentRss><description>&lt;p&gt;Whether or not you were one of the lucky ones to attend the sold out SPC ‘09 conference in Las Vegas last week you should download and review the 10 developer Hands On Labs (HOL’s) in anticipation to the public beta of SharePoint Server 2010 due out in November 2009.&lt;/p&gt; &lt;p&gt;You can download the 10 Developer HOL’s from &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=C010FC68-B47F-4DB6-B8A8-AD4BA33A35C5&amp;amp;displaylang=en#filelist"&gt;here&lt;/a&gt; (C# and VB.NET).&lt;/p&gt; &lt;p&gt;HOL01 - Developing a Visual Web Part in Visual Studio 2010&lt;br&gt;This hands-on lab introduces the Visual Studio 2010 SharePoint development environment. It shows how to build a Visual Web Part using LINQ to SharePoint, and how to connect one Web Part to another Web Part on the page. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL02 - Developing a List Definition and Event Receiver in Visual Studio 2010 &lt;br&gt;This hands-on lab walks you through building a list definition for SharePoint 2010 in Visual Studio 2010. It also shows how to build an event receiver for the list in Visual Studio 2010 and deploy it to SharePoint. After the list and event receiver are deployed, you can use the developer dashboard to evaluate the performance of the event receiver. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL03 - Developing Advanced Web Parts for SharePoint 2010 with Visual Studio 2010 &lt;br&gt;This hands-on lab shows how to build a Web Part using several SharePoint-specific controls in Visual Studio 2010. Investigate advanced built-in Web Parts, including the Data View Web Part. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL04 - Developing with LINQ to SharePoint in Visual Studio 2010 &lt;br&gt;This hands-on lab explores a variety of LINQ queries on SharePoint 2010, going into more depth than the introductory hands-on lab. It also walks you through an exercise of creating a custom content type in Visual Studio 2010. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL05 - Developing for SharePoint 2010 with the Client OM and REST in Visual Studio 2010 &lt;br&gt;This hands-on lab introduces the Client object model for use in calling SharePoint 2010 APIs from a client machine. It also shows the use of ADO.NET Data Services to call REST services in SharePoint 2010. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL06 - Developing a BCS External Content Type with Visual Studio 2010 &lt;br&gt;This hands-on lab walks you through building an external content type for Business Connectivity Services using Visual Studio 2010. It also builds a form for Microsoft Outlook and shows the data being edited offline in Outlook. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL07 - Developing a SharePoint 2010 Workflow with Initiation Form in Visual Studio 2010 &lt;br&gt;This hands-on lab walks you through building a workflow in Visual Studio 2010 for SharePoint 2010. You add an initiation form to the workflow and use an external data exchange activity in the workflow. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL08 - Developing SharePoint 2010 User Interface with Silverlight in Visual Studio 2010&lt;br&gt;This hands-on lab walks you through building Microsoft Silverlight applications for use in SharePoint 2010. You will access SharePoint 2010 data in Silverlight using the Client object model. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL09 - Developing SharePoint 2010 Sandboxed Solutions in Visual Studio 2010 &lt;br&gt;This hands-on lab walks you through building a Sandboxed Solution Web Part for SharePoint 2010. It will also add code to the Web Part that overloads the limits placed by the sandboxed solution, and you will review how the solution is shut down. &lt;br&gt;&lt;/p&gt; &lt;p&gt;HOL10 - Developing SharePoint 2010 User Interface Ribbon and Dialog Customizations&lt;br&gt;This hands-on lab walks you through adding a custom action to the SharePoint 2010 ribbon, and creating a Web Part that uses the Dialog Framework.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9913510" width="1" height="1"&gt;</description></item><item><title>Updated Patterns &amp; Practices Developing SharePoint Applications Guidance</title><link>http://blogs.msdn.com/rogerla/archive/2009/09/04/updated-patterns-practices-developing-sharepoint-applications-guidance.aspx</link><pubDate>Fri, 04 Sep 2009 22:33:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9891575</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9891575.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9891575</wfw:commentRss><description>&lt;p&gt;Congrats to Chris Keyser and all the authors/contributors for shipping the updated Version 2 of the SharePoint Developers Guidance.. must have free resource if you are doing development with SharePoint.  &lt;p&gt;&lt;strong&gt;Guidance for building collaborative applications that extend your LOB systems&lt;/strong&gt;  &lt;blockquote&gt; &lt;p&gt;The guidance provides value for experienced developers just starting in SharePoint development as well as experienced SharePoint developers looking to expand their skills.  &lt;p&gt;If you are new to SharePoint development, the first step is to study the Training Management application, which is based on Windows SharePoint Services. The documentation and the application can help developers understand the fundamentals of SharePoint development, and compliments other training resources and publications.  &lt;p&gt;For those that already are experienced in developing SharePoint applications, or who have gone through the Training Management application, the Partner Portal application and SharePoint Guidance library demonstrate these advanced areas. You can explore the guidance and Partner Portal reference implementation based upon your areas of interest. The general guidance refers into areas of the reference implementation that illustrate the covered concepts.  &lt;p&gt;This guide enhances product documentation by applying the information to a realistic business situation illustrated in the reference implementations. In many cases, the guidance refers to the product documentation. You can use the guidance to gain initial understanding. You can then use the product documentation for deeper understanding. &lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;strong&gt;Quick Links:&lt;/strong&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;a href="http://www.microsoft.com/spg"&gt;http://www.microsoft.com/spg&lt;/a&gt;&lt;br&gt;&lt;a href="http://www.codeplex.com/spg"&gt;http://www.codeplex.com/spg&lt;/a&gt;&lt;br&gt;Download &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=91f3c22c-8be7-4721-9449-84f699337d55&amp;amp;displaylang=en"&gt;here&lt;/a&gt;&lt;br&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;strong&gt;What’s included:&lt;/strong&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;table border="1" cellpadding="0"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="bottom" width="118"&gt; &lt;p&gt;&lt;b&gt;Component&lt;/b&gt;&lt;/p&gt;&lt;/td&gt; &lt;td valign="bottom" width="391"&gt; &lt;p&gt;&lt;b&gt;Description&lt;/b&gt;&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="118"&gt; &lt;p&gt;SharePoint Guidance Library&lt;/p&gt;&lt;/td&gt; &lt;td valign="top" width="391"&gt; &lt;p&gt;A set of reusable components that helps developers manage configuration, build repositories for SharePoint lists, log traces and events, and use service location.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="118"&gt; &lt;p&gt;Guide&lt;/p&gt;&lt;/td&gt; &lt;td valign="top" width="391"&gt; &lt;p&gt;The documentation includes a variety of topics, such as how to use design and application patterns, how to integrate LOB systems with SharePoint applications, building scalable applications, upgrading SharePoint applications, and using SharePoint capabilities to create, and deploy content. It also includes the design decisions made for the Partner Portal and Training Management applications and explanations of their implementations.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="118"&gt; &lt;p&gt;Contoso Partner Portal Reference Implementation&lt;/p&gt;&lt;/td&gt; &lt;td valign="top" width="391"&gt; &lt;p&gt;This SharePoint application shows how Contoso created an extranet where it can interact with its partners. Among the items demonstrated are techniques for building manageable and scalable enterprise applications, and how to incorporate publishing and page composition features, flexible navigation, collaboration sites, and LOB integration. It includes more advanced techniques than the Training Management reference implementation and requires Microsoft Office SharePoint Server 2007 with Service Pack 1 or Service Pack 2.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td valign="top" width="118"&gt; &lt;p&gt;Contoso Training Management Reference Implementation&lt;/p&gt;&lt;/td&gt; &lt;td valign="top" width="391"&gt; &lt;p&gt;This SharePoint application illustrates how the Contoso Human Resources department manages its training course offerings. It shows how to solve many basic SharePoint challenges that you might encounter when you develop your own applications. Windows SharePoint Services 3.0 is required.&lt;/p&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;a href="http://channel9.msdn.com/posts/akMSFT/Announcing-the-release-of-patterns-and-practices-Developing-SharePoint-Applications-guidance/"&gt;&lt;b&gt;Channel 9 videos&lt;/b&gt;&lt;/a&gt;&lt;b&gt;&lt;/b&gt;  &lt;blockquote&gt; &lt;p&gt;· &lt;a href="http://channel9.msdn.com/posts/akMSFT/Setting-up-the-Contoso-RI-p--p-Developing-SharePoint-Applications-guidance/"&gt;Setting up the Contoso RI&lt;/a&gt; &lt;b&gt;&lt;/b&gt; &lt;p&gt;· &lt;a href="http://channel9.msdn.com/posts/akMSFT/Walkthrough-of-the-Contoso-Reference-Implementation-p--p-Developing-SharePoint-Applications-guidance/"&gt;Walkthrough of the Contoso Reference Implementation&lt;/a&gt; &lt;b&gt;&lt;/b&gt; &lt;p&gt;· &lt;a href="http://channel9.msdn.com/posts/akMSFT/How-to-use-the-configuration-component-p--p-Developing-SharePoint-Applications-guidance/"&gt;How to use the configuration component?&lt;/a&gt; &lt;b&gt;&lt;/b&gt; &lt;p&gt;· &lt;a href="http://channel9.msdn.com/posts/akMSFT/How-to-use-the-logging-components-p--p-Developing-SharePoint-Applications-guidance/"&gt;How to use the logging components?&lt;/a&gt;&lt;b&gt;&lt;/b&gt;  &lt;p&gt;· &lt;a href="http://channel9.msdn.com/posts/akMSFT/How-to-use-the-SharePoint-Service-Locator-p--p-Developing-SharePoint-Applications-guidance/"&gt;How to use the SharePoint Service Locator? &lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;strong&gt;Authors and Contributors&lt;/strong&gt;  &lt;blockquote&gt; &lt;p&gt;The &lt;em&gt;Developing SharePoint Applications&lt;/em&gt; guidance was produced by the following individuals:  &lt;ul&gt; &lt;li&gt;Larry Brader, Francis Cheung, RoAnn Corbisier, Nelly Delgado, Chris Keyser, Erwin van der Valk, Blaine Wastell, Hanz Zhang (Microsoft Corporation)  &lt;li&gt;Charles Cho, Mike Chorey (Avanade)  &lt;li&gt;Kishore Kadiveti, Allvin Muthunayagam, VenkataAppaji Sirangi (Tata Consultancy Services)  &lt;li&gt;Colin Campbell, Roberta Leibovitz (Modeled Computation LLC)  &lt;li&gt;Robert L. Bogue (Thor Projects LLC)  &lt;li&gt;Todd Baginski (Advaiya, Inc)  &lt;li&gt;Andrew Connell (Critical Path Training)  &lt;li&gt;John Daniels (Analysts International)  &lt;li&gt;Tina Burden, Sharon Smith (TinaTech Inc)  &lt;li&gt;Veronica Ruiz (CXR Design)  &lt;li&gt;Richard Burte (ChannelCatalyst.com, Inc.) &lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Many thanks to the following advisors who provided invaluable assistance:  &lt;ul&gt; &lt;li&gt;Microsoft Contributors and Reviewers: Mike Ammerlaan, Paul Andrew, Luca Bandinelli, Eric Charran, Todd Haugen, Rob Howard, Roger Lamb, Steve Peschka, Jim Sturms, Simon Skaria, Aaron Weiker, Mike Wise  &lt;li&gt;External Contributors and Reviewers: Reza Alirezaei, Darrin Bishop, Jackson Chackungal, Spencer Harbar, Faraz Khan, Matthew McDermott, Maurice Prather, Eric Shupps, Ethan Wilansky, Andrew Woodward &lt;/li&gt;&lt;/ul&gt;&lt;/blockquote&gt; &lt;p&gt;&lt;img alt="Dd203468.practices(en-us,MSDN.10).png" src="http://i.msdn.microsoft.com/Dd203468.practices(en-us,MSDN.10).png"&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9891575" width="1" height="1"&gt;</description></item><item><title>SharePoint STSADM Silverlight application on TechNet</title><link>http://blogs.msdn.com/rogerla/archive/2009/06/03/sharepoint-stsadm-silverlight-application-on-technet.aspx</link><pubDate>Thu, 04 Jun 2009 05:17:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9697881</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9697881.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9697881</wfw:commentRss><description>&lt;p&gt;Go check out the cool new TechNet interactive online Silverlight application which allows you to quickly browse and filter STSADM commands with links to the documentation.&lt;/p&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="http://technet.microsoft.com/en-us/windowsserver/sharepoint/dd418924.aspx" target="_blank"&gt;Stsadm Technical Reference for Windows SharePoint Services 3.0&lt;/a&gt;  &lt;li&gt;&lt;a href="http://technet.microsoft.com/en-us/office/sharepointserver/cc948709.aspx" target="_blank"&gt;Stsadm Technical Reference for SharePoint Server 2007&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt; &lt;blockquote&gt; &lt;p&gt;&lt;a href="http://technet.microsoft.com/en-us/office/sharepointserver/cc948709.aspx" target="_blank"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointSTSADMSilverlightapplicationon_13981/image_3.png" width="638" height="350"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9697881" width="1" height="1"&gt;</description></item><item><title>Announcing Developer Best Practices Resource Center for SharePoint Server 2007</title><link>http://blogs.msdn.com/rogerla/archive/2009/04/03/announcing-developer-best-practices-resource-center-for-sharepoint-server-2007.aspx</link><pubDate>Sat, 04 Apr 2009 04:52:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9531147</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9531147.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9531147</wfw:commentRss><description>&lt;p&gt;Find up-to-date guidance about how to write Microsoft Office SharePoint Server 2007 applications and customizations that perform well, avoid common pitfalls, and best use the features of the SharePoint object model. &lt;p&gt;&lt;a title="http://msdn.microsoft.com/en-us/office/dd638301.aspx" href="http://msdn.microsoft.com/en-us/office/dd638301.aspx"&gt;http://msdn.microsoft.com/en-us/office/dd638301.aspx&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9531147" width="1" height="1"&gt;</description></item><item><title>SharePoint Diagnostics Tool v1.0 (SPDiag) Released</title><link>http://blogs.msdn.com/rogerla/archive/2009/02/05/sharepoint-diagnostics-tool-v1-0-spdiag-released.aspx</link><pubDate>Thu, 05 Feb 2009 21:55:32 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9399527</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9399527.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9399527</wfw:commentRss><description>&lt;p&gt;There is a new GUI utility published today to Microsoft.com/downloads for &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=412a9ef1-3358-4420-b820-0ca3f4641651&amp;amp;displaylang=en"&gt;x32&lt;/a&gt; and &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=BE58D769-2516-43CB-9890-3F79304528FF&amp;amp;displaylang=en"&gt;x64&lt;/a&gt; environments which should help cut down the ping pong performance related troubleshooting discussions between IT staff, support, and developers regarding the SharePoint farm itself. Use the tool to collect data from performance counters, ULS log files, IIS log files, event logs, and WMI (Windows Management Instrumentation), and then display and analyze the data in snapshots and custom reports.&amp;nbsp; You can read more about the SPDiag tool in the &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=1c222804-51c7-4bb5-ae3d-89c68ad27a78&amp;amp;displaylang=en&amp;amp;tm"&gt;SPDiag User Guide&lt;/a&gt; .&amp;nbsp; &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Description of the tool directly from the SPDiag User guide:&lt;/strong&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;The SharePoint Diagnostic tool (SPDiag) version 1.0, included with the latest release of the SharePoint Administration Toolkit, was created to simplify and standardize troubleshooting of SharePoint Products and Technologies, and to provide a unified view of collected data. SharePoint Products and Technologies administrators can use SPDiag to gather relevant information from a farm, display the results in a meaningful way, identify performance issues, and export the collected data and reports for analysis by Microsoft support personnel.  &lt;p&gt;The SharePoint Products and Technologies platform is highly complex and can be used for a wide variety of uses. Deploying, managing, and troubleshooting SharePoint Products and Technologies requires extensive knowledge spanning multiple technology areas, including security, networking, such Web technologies as ASPX, and SQL Server.  &lt;p&gt;Traditionally, SharePoint Server troubleshooting involves manually collecting a wide array of data from servers in the affected farm, and then manually analyzing the data to determine the source of the problem. This process can be complex and time-consuming, and data collection itself can place a significant load on the servers.  &lt;p&gt;SPDiag is designed to collect and review data from SharePoint Products and Technologies Web servers, application servers, and SQL servers, and store the collected data for each project in a SQL Server database for retrieval and analysis. SPDiag can collect performance data from IIS logs, ULS logs, and performance counters, and can also collect live data from the servers using Windows Management Instrumentation (WMI). Data can then be displayed in the Trends pane of the SPDiag interface and filtered to reveal trends, bottlenecks, and other performance issues that would otherwise require significant manual data processing to uncover. You can also view the individual components and the logical structure of the farm in the Snapshot pane.  &lt;p&gt;SPDiag operates in the context of a &lt;i&gt;project&lt;/i&gt;, which is the container used to store collected data for a specific farm. Each project has its own database, and you can create many projects for a single farm, subject only to database server resource limitations. Projects can be saved and reopened again at a later time, and new data can be added to a project between SPDiag sessions. You cannot move data between projects, and you cannot collect data from more than one farm in a single project. Because all SPDiag project data is stored in a SQL Server database, you can back up a project database or move it to another database server.  &lt;p&gt;SPDiag can be used in online or offline modes. In online mode, SPDiag is installed on a Web server belonging to the farm you want to troubleshoot. This allows SPDiag to connect to the farm and collect data. In offline mode, SPDiag is installed on a computer that is not a part of a farm. It can only be used to review existing SPDiag projects, and cannot collect data.  &lt;p&gt;You can export collected data and reports as data files, which can then be sent to Microsoft support technicians for analysis. This can help to facilitate remote troubleshooting by ensuring that the required data is captured on-site, and by consolidating the data in a standardized format.&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9399527" width="1" height="1"&gt;</description></item><item><title>Automate SharePoint Dispose() code reviews with SPDisposeCheck</title><link>http://blogs.msdn.com/rogerla/archive/2009/01/29/automate-sharepoint-dispose-code-reviews-with-spdisposecheck.aspx</link><pubDate>Fri, 30 Jan 2009 05:23:54 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9384093</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9384093.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9384093</wfw:commentRss><description>&lt;p&gt;Today the Microsoft SharePoint Product Team announced the &lt;strong&gt;SPDisposeCheck&lt;/strong&gt; utility&lt;strong&gt; &lt;/strong&gt;&lt;a href="http://blogs.msdn.com/sharepoint/archive/2009/01/29/spdisposecheck-released.aspx"&gt;here&lt;/a&gt; and also at Paul Andrew’s blog &lt;a href="http://blogs.msdn.com/pandrew/archive/2009/01/29/spdisposecheck-v1-3-1-is-released.aspx"&gt;here&lt;/a&gt;. You can download the free &lt;strong&gt;SPDisposeCheck&lt;/strong&gt; utility in the MSDN Code Gallery &lt;a href="http://code.msdn.microsoft.com/SPDisposeCheck"&gt;http://code.msdn.microsoft.com/SPDisposeCheck&lt;/a&gt; .&lt;/p&gt; &lt;p&gt;The &lt;strong&gt;SPDisposeCheck&lt;/strong&gt; 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.&amp;nbsp; 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.&amp;nbsp; A manual code review is still required to cast out ‘false positives’ that the tool may produce in the output report.&amp;nbsp; 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.&amp;nbsp; Note SharePoint’s instrumentation writes to the ULS logs when it finds SPSite and SPWeb objects that were not disposed as expected.&lt;/p&gt; &lt;p&gt;The original MSDN article &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; was updated 1/30/09 and the Dispose() guidance is in sync with the content on this blog and the SPDisposeCheck tool.&amp;nbsp; If you have not done so in a while check out my original blog post “&lt;a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx"&gt;SharePoint 2007 and WSS 3.0 Dispose Patterns by Example&lt;/a&gt;” which has several updated Dispose() guidance and edge cases including &lt;strong&gt;RootWeb&lt;/strong&gt; and &lt;strong&gt;ParentWeb&lt;/strong&gt; changes.&amp;nbsp; You can find all the source code examples on this blog in the Visual Studio 2008 solution &lt;strong&gt;SPDisposeExamples&lt;/strong&gt; packaged with the &lt;strong&gt;SPDisposeCheck&lt;/strong&gt; utility installer.&lt;/p&gt; &lt;p&gt;Understanding the latest Dispose() guidance from Microsoft, scanning with &lt;strong&gt;SPDisposeCheck&lt;/strong&gt; 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.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9384093" width="1" height="1"&gt;</description></item><item><title>SharePoint 2007 PublishingWeb.GetVariation() Method Leak</title><link>http://blogs.msdn.com/rogerla/archive/2009/01/28/updated-publishingweb-getvariation-dispose-guidance.aspx</link><pubDate>Thu, 29 Jan 2009 07:27:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9382363</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9382363.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9382363</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.getvariation.aspx"&gt;&lt;strong&gt;PublishingWeb.GetVariation()&lt;/strong&gt;&lt;/a&gt; method returns a &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.aspx"&gt;&lt;strong&gt;PublishingWeb&lt;/strong&gt;&lt;/a&gt; object which which needs to be explicitly Closed() as shown in the following code sample.&amp;nbsp; For a complete list of all the updated SharePoint Dispose() guidance please see &lt;a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx" target="_blank"&gt;SharePoint 2007 and WSS 3.0 Dispose Patterns by Example&lt;/a&gt; .&lt;/p&gt; &lt;blockquote&gt; &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 100.89%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 598px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 98.4%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 581px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; GetVariationLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);  &lt;span style="color: #008000"&gt;// Passing in web so no Close() needed&lt;/span&gt;
            VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
            PublishingWeb variationPublishingWeb = publishingWeb.GetVariation(variationLabel);  &lt;span style="color: #008000"&gt;// must be Closed()&lt;/span&gt;
            &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; GetVariationNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb variationPublishingWeb = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
            &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);  &lt;span style="color: #008000"&gt;// Passing in web so no Close() needed&lt;/span&gt;
                VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
                variationPublishingWeb = publishingWeb.GetVariation(variationLabel);  &lt;span style="color: #008000"&gt;// must be Closed()&lt;/span&gt;
                &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
            }
            &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
            {
                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(variationPublishingWeb != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                    variationPublishingWeb.Close();
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Special thanks to Keith Dahlby, Stefan Goßner (Microsoft), and Robert Orleth (Microsoft).&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9382363" width="1" height="1"&gt;</description></item><item><title>SharePoint 2007 UserProfiles.PersonalSite Property Leak</title><link>http://blogs.msdn.com/rogerla/archive/2009/01/14/sharepoint-2007-userprofiles-personalsite-property-leak.aspx</link><pubDate>Thu, 15 Jan 2009 02:31:43 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9319640</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9319640.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9319640</wfw:commentRss><description>&lt;p&gt;The following edge case applies to MOSS 2007 (and not WSS 3.0).&amp;nbsp; When using the following pattern the property &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofile.personalsite.aspx" target="_blank"&gt;&lt;strong&gt;Microsoft.Office.Server.UserProfiles.PersonalSite&lt;/strong&gt;&lt;/a&gt; requires a Dispose() call before leaving scope if you use the property in your code.&amp;nbsp; Below is an example of a common usage of PersonalSite property.&lt;/p&gt; &lt;blockquote&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 717px; cursor: text; max-height: 800px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 170px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 98.36%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 152px; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PersonalSiteLeak()
{
    &lt;span style="color: #008000"&gt;// open a site collection&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        UserProfileManager profileManager = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; UserProfileManager(ServerContext.GetContext(siteCollection));
        UserProfile profile = profileManager.GetUserProfile(&lt;span style="color: #006080"&gt;"domain\\username"&lt;/span&gt;);
        SPSite personalSite = &lt;font color="#ff0000"&gt;profile.PersonalSite;&lt;/font&gt;    &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// will leak&lt;/font&gt;&lt;/span&gt;
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 725px; cursor: text; max-height: 800px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 214px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 97.09%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 206px; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PersonalSiteNoLeak()
{
    &lt;span style="color: #008000"&gt;// open a site collection&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        UserProfileManager profileManager = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; UserProfileManager(ServerContext.GetContext(siteCollection));
        UserProfile profile = profileManager.GetUserProfile(&lt;span style="color: #006080"&gt;"domain\\username"&lt;/span&gt;);
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite personalSite = profile.PersonalSite)
        {
            &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
        }
    }
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;From a web context performance perspective it is more efficient to get your UserProfile object in the following example: 
&lt;blockquote&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 600px; cursor: text; max-height: 600px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 84px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 97.36%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 70px; background-color: #f4f4f4; border-bottom-style: none"&gt;UserProfile myProfile = ProfileLoader.GetProfileLoader().GetUserProfile();
&lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite personalSite = myProfile.PersonalSite)
{
     &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;Note the use of ProfileLoader to get the shared UserProfile instance for my own profile.&amp;nbsp; There is no need to open a fresh SPSite in the case where you’re browsing a page.&amp;nbsp; Additionally, if your web part is designed to be placed on a My Site, there is even a shared PersonalSite instance that will be disposed for you and does not require you to call Dispose() with this technique: 
&lt;blockquote&gt;
&lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 600px; cursor: text; max-height: 200px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 105px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 98.56%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 91px; background-color: #f4f4f4; border-bottom-style: none"&gt;IPersonalPage currentMySitePage = &lt;span style="color: #0000ff"&gt;this&lt;/span&gt;.Page &lt;span style="color: #0000ff"&gt;as&lt;/span&gt; IPersonalPage;
&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (currentMySitePage != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; !currentMySitePage.IsProfileError)
{
     SPSite personalSite = currentMySitePage.PersonalSite; &lt;span style="color: #008000"&gt;// will not leak&lt;/span&gt;
     &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;Thanks to Microsoft’s Anant Dimiri and Bryant Fong for their contributions!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9319640" width="1" height="1"&gt;</description></item><item><title>try / finally using() SharePoint Dispose()</title><link>http://blogs.msdn.com/rogerla/archive/2009/01/14/try-finally-using-sharepoint-dispose.aspx</link><pubDate>Wed, 14 Jan 2009 17:13:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318753</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9318753.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9318753</wfw:commentRss><description>&lt;p&gt;Chances are that by now if you have been developing on the Microsoft SharePoint Office Server (MOSS) 2007 and Windows SharePoint Services (WSS) 3.0 Object Model (OM) for any length of time you are aware of the importance of when to (and when not to) call &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx"&gt;Dispose()&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;on your SharePoint Objects.&amp;nbsp; However, you may not be aware that if you &lt;strong&gt;do not always &lt;/strong&gt;wrap your &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx"&gt;Dispose()&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;method(s) within a finally { } block either implicitly by implementing the using() { } statement or by explicitly wrapping it in your code with &lt;a href="http://msdn.microsoft.com/en-us/library/dszsf989.aspx" target="_blank"&gt;&lt;strong&gt;try/finally&lt;/strong&gt;&lt;/a&gt; then you you can’t guarantee that your intended objects &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx"&gt;Dispose()&lt;/a&gt;&lt;/strong&gt; will ever be called during an exception.&amp;nbsp; &lt;/p&gt; &lt;p&gt;SharePoint developers are required to be alert and stay on their toes when creating and releasing SPSite and SPWeb objects.&amp;nbsp; Intimately understand your applications code path(s) and object scope lifetime in order to optimize performance and good memory hygiene with the SharePoint platform is paramount.&amp;nbsp; Every .NET application (not just SharePoint) must play by the .NET Common Language Runtime (CLR) house rules to ensure that &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx"&gt;Dispose()&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;gets called (when appropriate) in a timely fashion if you want to avoid very serious negative consequences exacerbated in heavily utilized production SharePoint environments.&amp;nbsp; You need to make certain and take extra measures to take that code snippet you found which for brevity excluded proper dispose methods and works fine on a developers Virtual PC environment later keeps the business stakeholders up late at night wondering why their customized SharePoint site is having production problems under load.&lt;/p&gt; &lt;p&gt;Below I discuss some edge cases that you should be aware of when implementing using() and try/finally in your code to help mitigate the risk of memory leaks and help tighten up your production code.&lt;/p&gt; &lt;h5&gt;Understanding the using() statement&lt;/h5&gt; &lt;p&gt;Since you have your SharePoint OM homework you know that you can take advantage of the &lt;a href="http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx" target="_blank"&gt;&lt;strong&gt;using() statement&lt;/strong&gt;&lt;/a&gt; to increase the readability and help simplify the the amount of code you have to write to properly release the unmanaged memory held onto by the SharePoint managed code.&amp;nbsp; Behind the scenes .NET has the CLR automatically inject into your MSIL code a &lt;a href="http://msdn.microsoft.com/en-us/library/dszsf989.aspx" target="_blank"&gt;&lt;strong&gt;try/finally&lt;/strong&gt;&lt;/a&gt; block (notice I did not say try/catch/finally) and the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx"&gt;Dispose()&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;method is getting called on your behalf.&amp;nbsp; &lt;/p&gt; &lt;blockquote&gt; &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 500px; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 170px; max-height: 600px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt; &lt;p&gt;public void UsingBestPractice()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SPSite siteCollection = new SPSite("&lt;a href="http://moss&amp;quot;))"&gt;http://moss"))&lt;/a&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SPWeb web = siteCollection.OpenWeb())&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; //...&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } // SPWeb object web.Dispose() automatically called&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; // SPSite object siteCollection.Dispose() automatically called &lt;br&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/blockquote&gt; &lt;h5&gt;Coding with try/finally&lt;/h5&gt; &lt;p&gt;What might not be so commonly known when writing SharePoint code is that there are some cases where we should not implement &lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx" target="_blank"&gt;using()&lt;/a&gt;&lt;/strong&gt;&lt;strong&gt;.&lt;/strong&gt;&amp;nbsp; We do have the option of manually emulating what the CLR does behind the scenes automatically for us by implementing &lt;a href="http://msdn.microsoft.com/en-us/library/dszsf989.aspx" target="_blank"&gt;&lt;strong&gt;try/finally&lt;/strong&gt;&lt;/a&gt; blocks which contain the respective &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx"&gt;Dispose()&lt;/a&gt;&lt;/strong&gt; method(s) which ultimately allows our SharePoint IT Administrator(s) to get their much needed beauty sleep.&amp;nbsp; By the way, there is nothing wrong with adding a &lt;a href="http://msdn.microsoft.com/en-us/library/dszsf989.aspx" target="_blank"&gt;&lt;strong&gt;try/catch/finally&lt;/strong&gt;&lt;/a&gt; as long as you make certain that you are properly handling the exceptions in the catch { } block and not just eating them without some sort of logging in place.&amp;nbsp; You do not want to see any empty catch { } blocks in your production code.&amp;nbsp; Also, note the best practice of conditionally testing for null prior to disposing as shown in the following code.&amp;nbsp; &lt;/p&gt; &lt;blockquote&gt; &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 520px; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 329px; max-height: 600px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt; &lt;p&gt;void TryFinallyBestPractice()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SPSite siteCollection = null;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SPWeb web = null;  &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; try&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; siteCollection = new SPSite("&lt;a href="http://moss&amp;quot;);"&gt;http://moss");&lt;/a&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; web = siteCollection.OpenWeb();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(web.Title);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; // optionally catch { ... make sure you handle if you use catch }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; finally&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (web != null)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; web.Dispose();  &lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if (siteCollection != null)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; siteCollection.Dispose();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;} &lt;/p&gt;&lt;/div&gt;&lt;/blockquote&gt; &lt;h5&gt;Don’t use SPContext with using()&lt;/h5&gt; &lt;p&gt;The following example illustrates a common &lt;strong&gt;&lt;font color="#ff0000"&gt;bad&lt;/font&gt;&lt;/strong&gt; practice of wrapping a SPContext originated SPWeb object in a &lt;a href="http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx" target="_blank"&gt;&lt;strong&gt;using() statement&lt;/strong&gt;&lt;/a&gt; which as we now know will automatically inject a &lt;a href="http://msdn.microsoft.com/en-us/library/dszsf989.aspx" target="_blank"&gt;&lt;strong&gt;try/finally&lt;/strong&gt;&lt;/a&gt; that will call web.Dispose() .&amp;nbsp; In cases where RootWeb equals the SPContext “web” as shown below this can cause production problems since SPContext objects are managed by SharePoint itself and should not be released by you (ever).&lt;/p&gt; &lt;blockquote&gt; &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 500px; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 45px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 99.34%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 32px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// Bad.. Dispose() is automatically called on web which SPContext equality&lt;/font&gt;&lt;/span&gt;
&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;( SPWeb web = SPControl.GetContextWeb(HttpContext.Current)) { ... }&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h5&gt;Combining OM Calls&lt;/h5&gt;
&lt;p&gt;Use caution when combining SharePoint Object Model calls into the same line as these can be some of the most tricky leaks to find.&amp;nbsp; Dissecting the following example we see:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;SPContext.Current.Web.Url is fine no dispose needed 
&lt;li&gt;SPSite is instantiated which needs to be Disposed but is lost since we are also calling OpenWeb() which is actually what using() will Dispose()&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 500px; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 115px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 96.51%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 99px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CombiningCallsLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(SPContext.Current.Web.Url).OpenWeb())
    {
        &lt;span style="color: #008000"&gt;// ... new SPSite will be leaked&lt;/span&gt;
    } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;You would have to split the previous sample up into nested using() statements to avoid leaking memory:&lt;/p&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 500px; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 153px; max-height: 200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 97.74%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 134px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CombiningCallsBestPractice()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(SPContext.Current.Web.Url))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h5&gt;foreach statement and try/finally&lt;/h5&gt;
&lt;p&gt;Unfortunately &lt;strong&gt;foreach&lt;/strong&gt; can’t implement &lt;a href="http://msdn.microsoft.com/en-us/library/yh598w02(VS.80).aspx" target="_blank"&gt;&lt;strong&gt;using()&lt;/strong&gt;&lt;/a&gt; so extra measures must be taken to make sure your code makes it to the intended Dispose by using the following &lt;a href="http://msdn.microsoft.com/en-us/library/dszsf989.aspx" target="_blank"&gt;&lt;strong&gt;try/finally&lt;/strong&gt;&lt;/a&gt; pattern in your code.&amp;nbsp; Use extra caution with any code that is part of a iteration such as &lt;strong&gt;foreach&lt;/strong&gt; because they have a way of turning a small problem in dev into a big problem in production especially when called with navigation or on SharePoint environments with lots of sites.&lt;/p&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 500px; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 365px; max-height: 600px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;
&lt;p&gt;public void TryFinallyBestPractice()&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SPSite siteCollection = new SPSite("&lt;a href="http://moss&amp;quot;))"&gt;http://moss"))&lt;/a&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; using (SPWeb outerWeb = siteCollection.OpenWeb())&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (SPWeb innerWeb in outerWeb.Webs)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try //should be 1st statement after foreach&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // ... something evil occurs here&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finally&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(innerWeb != null)&lt;/strong&gt;&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; innerWeb.Dispose();&lt;br&gt;&lt;font color="#ff0000"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;}&lt;/strong&gt;&lt;br&gt;&lt;/font&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } // SPWeb object outerWeb.Dispose() automatically called&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&amp;nbsp; // SPSite object siteCollection.Dispose() automatically called &lt;br&gt;}&lt;/p&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h5&gt;SharePoint Memory Internals&lt;/h5&gt;
&lt;p&gt;If you’ve made it this far without falling asleep you might be interested in learning more about the SharePoint internals of memory management and why it’s so important for you (the owner of the application code) to know when is the optimal time for performance reasons to hold and release your SharePoint objects.&amp;nbsp; As SharePoint Application developers you are familiar with the public SPSite and SPWeb objects but internally to get their work done the real managed object that holds onto the unmanaged heap is SPRequestInternalClass which is internal to a wrapper class SPRequest.&lt;/p&gt;
&lt;p&gt;Internally we use a RCW (runtime callable wrapper) which is essentially a finalizable object (there are subtle differences but they don’t really come into play here).&amp;nbsp;&amp;nbsp;&amp;nbsp; If that object is no longer rooted, the finalizer (teardown of the RCW) will release the native memory.&amp;nbsp;&amp;nbsp; However, like normal finalizers the RCW teardown is performed by a single finalizer thread which generally can’t keep up with cleaning these objects if they’re being leaked many times per second.&lt;/p&gt;
&lt;p&gt;Not releasing the SharePoint objects in a timely fashion can lead to poor memory hygiene including excessive fragmentation, pinning, and OOM exceptions building very quickly.&amp;nbsp; Problematic code which fails to properly dispose objects in a timely fashion becomes exacerbated especially in x32bit environments with a large number of sites.&amp;nbsp; We encourage all Enterprise SharePoint farms to use x64bit versions of the OS and MOSS to take advantage of the additional addressable memory.&lt;/p&gt;
&lt;p&gt;Resources other than just memory are effected by not properly disposing memory.&amp;nbsp; For example, SQL Server establishes a 1:1 connection with &lt;strong&gt;each &lt;/strong&gt;SPRequest object and it lives up to the release of the internal SPRequest object (which is used by your SPSite and SPWeb object).&lt;/p&gt;
&lt;p&gt;If you’d like to read more perspective on SharePoint internals and also discover ways to look for evidence of dispose related memory leaks in the ULS logs I’d encourage you to read Stefan Goßner’s excellent blog on &lt;a href="http://blogs.technet.com/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx" target="_blank"&gt;Disposing SPWeb and SPSite objects&lt;/a&gt; .&lt;/p&gt;
&lt;p&gt;Special thanks to my colleagues at Microsoft Dave Aknai, Paul Andrew, Todd Carter, Shawn Cicoria, Stefan Goßner, Scott Harris, Vishwas Kulkarni, Zach Kramer, Randy Thomson, and Sean Thompson.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318753" width="1" height="1"&gt;</description></item><item><title>CTP of VSeWSS 1.3 Announced – Now installs on 64bit OS</title><link>http://blogs.msdn.com/rogerla/archive/2009/01/12/ctp-of-vsewss-1-3-now-installs-on-64bit-os.aspx</link><pubDate>Mon, 12 Jan 2009 17:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9307879</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9307879.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9307879</wfw:commentRss><description>&lt;p&gt;Paul Andrew just announced on the &lt;a href="http://blogs.msdn.com/sharepoint/archive/2009/01/12/announcing-community-technology-preview-of-visual-studio-2008-extensions-for-sharepoint-v1-3.aspx" target="_blank"&gt;Microsoft SharePoint Team Blog&lt;/a&gt; that a new Community Technology Preview (CTP) version of the Visual Studio extensions for SharePoint 1.3 (VSeWSS 1.3) is now publically available on &lt;a href="https://connect.microsoft.com/site/sitehome.aspx?SiteID=428" target="_blank"&gt;Microsoft Connect&lt;/a&gt; site.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;New Features in VSeWSS 1.3&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;The extensions now install on x64 bit OS. Visual Studio 2008 and SharePoint must be already installed. &lt;li&gt;Command Line Build option for TFS and MSBuild integration &lt;li&gt;Separate WSP Package and Retract commands. You can now build the WSP without deploying it &lt;li&gt;SPSolGen to Support Exporting from Content Management Publishing Sites &lt;li&gt;New Item Template for RootFiles Deployment &lt;li&gt;Automatically Remove conflicting existing features on development SharePoint server &lt;li&gt;WSP View New Feature Dialog Improvements: scope, receiver checkbox, element checkbox &lt;li&gt;WSP View can now be used to merge features and it blocks site features being merged into web features &lt;li&gt;Allow adding separate binary files such as Workflow assemblies &lt;li&gt;Some refactoring allowing for Web Part renaming and removing lines from feature.xml Item Removed &lt;li&gt;Allow selection of GAC or BIN deployment for Web Part Project not including CAS generation &lt;li&gt;Increase visibility of hidden features that VSeWSS creates &lt;li&gt;Add fast update deploy for DLL only or file only changes to solutions &lt;li&gt;Numerous Bug Fixes and improvements to error messages&lt;/li&gt;&lt;/ul&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9307879" width="1" height="1"&gt;</description></item><item><title>SharePoint Automated Setup, Product Software Updates, and “Single-Click” Deployment</title><link>http://blogs.msdn.com/rogerla/archive/2008/12/16/sharepoint-automated-setup-product-software-updates-and-single-click-deployment.aspx</link><pubDate>Tue, 16 Dec 2008 19:16:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9226418</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9226418.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9226418</wfw:commentRss><description>&lt;h3&gt;MOSS 2007 and WSS 3.0 December 2008 Cumulative Update (CU)&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font color="#ff0000"&gt;&lt;strong&gt;Blog Updated 12/17/08&lt;/strong&gt;&lt;/font&gt;&lt;/p&gt; &lt;p&gt;The &lt;a href="http://blogs.msdn.com/sharepoint/archive/2008/12/17/announcing-december-cumulative-update-for-office-sharepoint-server-2007-and-windows-sharepoint-services-3-0.aspx" target="_blank"&gt;Microsoft SharePoint Team Blog Announced the December Cumulative Update (CU) for Office SharePoint Server 2007 and Windows SharePoint Services 3.0.&lt;/a&gt;&amp;nbsp; From this point (December 2008) forward each Cumulative Update will consist of a package that contains every hotfix patch that we have shipped including the Infrastructure Update.&amp;nbsp; This should greatly simplify building MOSS servers and farms going forward.&amp;nbsp; In summary you can start with WSS 3.0 + SP1 and MOSS 2007 + SP1 plus the Dec CU for both WSS 3.0 and MOSS 2007!&lt;/p&gt; &lt;p&gt;After applying the December CU the version should be &lt;b&gt;12.0.6335&lt;/b&gt;&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointAutomatedSetupProductSoftwareU_9E82/image_2.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="134" alt="image" src="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointAutomatedSetupProductSoftwareU_9E82/image_thumb.png" width="244" border="0"&gt;&lt;/a&gt; &lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;MOSS 2007 and WSS 3.0 Product Software Updates&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;As developers you want to ensure you are working with your I.T. departments for building, testing, and deploying your SharePoint software on the latest updated bits from Microsoft.&amp;nbsp; The best practice going forward is to use the Cumulative Update to build your MOSS 2007 and WSS 3.0 servers but if you want more details on the specific updates prior to the Dec CU check out the following links which will be maintained by the product group.&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;Deploy software updates for Windows SharePoint Services 3.0&lt;/strong&gt;&lt;br&gt;&lt;a href="http://technet.microsoft.com/en-us/library/cc288269.aspx"&gt;http://technet.microsoft.com/en-us/library/cc288269.aspx&lt;/a&gt;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Deploy software updates for Office SharePoint Server 2007&lt;br&gt;&lt;/strong&gt;&lt;a href="http://technet.microsoft.com/en-us/library/cc263467.aspx"&gt;http://technet.microsoft.com/en-us/library/cc263467.aspx&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;MOSS 2007 Automated Setup White Paper&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;Emmanuel Bergerat (Microsoft Consulting Services) published the following White Paper: “&lt;a href="http://technet.microsoft.com/en-us/library/dd335964.aspx" target="_blank"&gt;Using scripts to automate SharePoint Server 2007 installations&lt;/a&gt;” to industrialize MOSS 2007 environments setup in a single resource.&amp;nbsp; This will help SharePoint Developers with deployment and Administrators for repeatability and improve consistency.&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;“Single-Click” Deployment Example – Adventure Works Demo Site Installation&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointAutomatedSetupProductSoftwareU_9E82/image_8.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="244" alt="image" src="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointAutomatedSetupProductSoftwareU_9E82/image_thumb_3.png" width="226" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;The MOSS MVP’s have a free webcast series ongoing using a new soon to be published Adventure Works Web Application Demo Site.&amp;nbsp; Ryan Duguid (Microsoft PM SharePoint) and Steve Fox (Microsoft DPE) have been leading this effort and it is very impressive &lt;a href="http://silverlight.net/Default.aspx" target="_blank"&gt;Silverlight&lt;/a&gt; 2.0 enabled SharePoint Web Application that hardly looks like a SharePoint WCM site!&amp;nbsp; One thing I’m most impressed with is what is called the “Single-Click” Web Application Deployment example which allows you to modify a config file with your MOSS Farm environment settings and it will do a complete uninterrupted installation of the entire Adventure Works Demo Site in about 20 minutes (based on your Virtual Environment efficiency)!&amp;nbsp; What’s very cool for developers and administrators is that you can examine how this is being done since it’s a combination of custom stsadm extensions with batch and config files.&amp;nbsp; It is very comprehensive setup process and closely resembles real world Internet farms including dual authentication (FBA and Windows Integrated), AD user setup, IIS host headers, SSP, Web application(s), SharePoint Solutions and Features.. everything!&amp;nbsp; Until the Adventure Works Demo Site is released soon you can begin ramping up by viewing the MOSS MVP’s free ongoing MS Events Webcast presentations (past sessions are recorded) here:&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;Date (GMT -0500)&lt;/strong&gt;&lt;br&gt;&lt;strong&gt;Topic &amp;amp; Registration URL&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Tues, Dec 2, 2008 : 2p-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396509&amp;amp;Culture=en-US"&gt;Getting Started&lt;/a&gt;  &lt;p&gt;Thurs, Dec 4, 2008 : 2p-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396669&amp;amp;Culture=en-US"&gt;.COM Branding&lt;/a&gt;  &lt;p&gt;Tues, Dec 9, 2008 : 2p-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396671&amp;amp;Culture=en-US"&gt;Custom Fields, Web Parts &amp;amp; Lists&lt;/a&gt;  &lt;p&gt;Thurs, Dec 11, 2008 : 2p-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396941&amp;amp;Culture=en-US"&gt;Authentication&lt;/a&gt;  &lt;p&gt;Tues, Dec 16, 2008 : 2p-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396806&amp;amp;Culture=en-US"&gt;Web Interoperability&lt;/a&gt;  &lt;p&gt;Thurs, Dec 18, 2008 : 2-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396824&amp;amp;Culture=en-US"&gt;Search&lt;/a&gt;  &lt;p&gt;Tues, Jan 6, 2009 : 2-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032396926&amp;amp;Culture=en-US"&gt;Content Deployment&lt;/a&gt;  &lt;p&gt;Thurs, Jan 8, 2009 : 2-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032397228&amp;amp;Culture=en-US"&gt;Variations &amp;amp; Multilingual Sites&lt;/a&gt;  &lt;p&gt;Tues, Jan 13, 2009 : 2-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032397230&amp;amp;Culture=en-US"&gt;Enabling Social Networking&lt;/a&gt;  &lt;p&gt;Thurs, Jan 15, 2009 : 2-3p&lt;br&gt;&lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032397232&amp;amp;Culture=en-US"&gt;Site Customization with Silverlight 2.0&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;SharePoint Guidance (SPG) on Deploying and Upgrading SharePoint Applications&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;The folks in the Patterns and Practices team have published many topics in the SharePoint Guidance (SPG) which is downloadable from &lt;a href="http://www.microsoft.com/spg" target="_blank"&gt;here&lt;/a&gt;.&amp;nbsp; It includes a fictitious Contoso Pharmaceuticals Training Site which can be manually deployed from Visual Studio Solutions and there is some great guidance you can take offline in the chm help file which installs with the SPG guidance download.&amp;nbsp; In particular take a look at the &lt;a href="http://msdn.microsoft.com/en-us/library/dd206931.aspx" target="_blank"&gt;Deploying and Upgrading SharePoint Applications&lt;/a&gt; .&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointAutomatedSetupProductSoftwareU_9E82/image_10.png"&gt;&lt;img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="138" alt="image" src="http://blogs.msdn.com/blogfiles/rogerla/WindowsLiveWriter/SharePointAutomatedSetupProductSoftwareU_9E82/image_thumb_4.png" width="244" border="0"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9226418" width="1" height="1"&gt;</description></item><item><title>SPDisposeCheck SPSite and SPWeb Dispose() Best Practice Utility Announced</title><link>http://blogs.msdn.com/rogerla/archive/2008/11/12/spdisposecheck-spsite-and-spweb-dispose-best-practice-utility-announced.aspx</link><pubDate>Thu, 13 Nov 2008 01:38:15 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9064521</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/9064521.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=9064521</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/pandrew" target="_blank"&gt;Paul Andrew&lt;/a&gt; (Microsoft SharePoint Product Group) &lt;a href="http://blogs.msdn.com/sharepoint/archive/2008/11/12/announcing-spdisposecheck-tool-for-sharepoint-developers.aspx" target="_blank"&gt;announced the SPDisposeCheck utility&lt;/a&gt; at the &lt;a href="http://www.microsoft.com/emea/teched2008/developer/default.aspx" target="_blank"&gt;Tech·Ed EMEA Developers Conference&lt;/a&gt; today.&amp;nbsp; The SPDisposeCheck command line utility will be released soon (North American Winter) and will recursively scan any destination folder that you specify containing custom WSS 3.0 and Microsoft SharePoint Server 2007 .NET assemblies (scanning MSIL and not the source code itself) looking for SharePoint specific Dispose() best practices (or lack thereof) as listed in the guidance on MSDN &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; , &lt;em&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/bb687949.aspx"&gt;Best Practices: Common Coding Issues When Using the SharePoint Object Model&lt;/a&gt;&lt;/em&gt; , and my blog &lt;a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx"&gt;SharePoint 2007 and WSS 3.0 Dispose Patterns by Example&lt;/a&gt; .&amp;nbsp; SPDisposeCheck utility is intended to help quickly scan through a large amount of custom SharePoint code in order to direct an experienced SharePoint developer to areas of concern with regards to proper usage of Dispose().&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Additional SPDisposeCheck information:&lt;/strong&gt;&lt;/p&gt; &lt;ul&gt; &lt;li&gt;For developer workstations, not to be run on production  &lt;li&gt;Built on FX Cop technology the tool opens assemblies and validates them against the Microsoft published guidance (however, not compatible with FX Cop)  &lt;li&gt;Output will contain warnings messages for Dispose() method  &lt;li&gt;May include “false positives” so expert analysis is required  &lt;li&gt;Contact Microsoft support if you need assistance now&lt;/li&gt;&lt;/ul&gt; &lt;p&gt;Special thanks to Microsoft’s Sean Thompson (Microsoft PFE), Scott Harris, Paul Andrew, and &lt;a href="http://blogs.technet.com/stefan_gossner/default.aspx"&gt;Stefan Goßner&lt;/a&gt;.&amp;nbsp; In addition, I’d like to thank the folks in the field including the MOSS MVP’s and Microsoft Services and Support (MCS/Premier/PFE/CSS) for their feedback and testing.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9064521" width="1" height="1"&gt;</description></item><item><title>Updated SPSite.RootWeb Dispose Guidance</title><link>http://blogs.msdn.com/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx</link><pubDate>Sun, 05 Oct 2008 02:21:34 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8976769</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/8976769.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=8976769</wfw:commentRss><description>&lt;blockquote&gt; &lt;p&gt;I have recently been working directly with the Microsoft Office SharePoint Server 2007 (MOSS 2007) Product Group as well as several Subject Matter Experts in the Microsoft Services field to help clarify some Dispose edge cases which have been causing confusion which have surfaced in the MOSS Developer community.&amp;nbsp; Taking a closer look at the RootWeb Dispose Best Practice listed in the MSDN white paper &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; there is an new exception to the &lt;strong&gt;SPSite.RootWeb&lt;/strong&gt; Dispose guidance.&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;Do not explicitly call Dispose() on the &lt;strong&gt;SPSite.RootWeb&lt;/strong&gt; property.&amp;nbsp; The dispose cleanup will be handled automatically by the SharePoint and the .NET framework.&amp;nbsp; For existing SharePoint customizations removal of explicit RootWeb Dispose is recommended to avoid an edge case condition where the &lt;strong&gt;SPContext.Current.Web&lt;/strong&gt; has equality to the &lt;strong&gt;SPSite.RootWeb&lt;/strong&gt;.&amp;nbsp; Problems can occur when disposing RootWeb when obtained from any variation of &lt;strong&gt;SPContext &lt;/strong&gt;(For Example:&lt;strong&gt; SPContext.Site.RootWeb&lt;/strong&gt;, &lt;strong&gt;SPContext.Current.Site.RootWeb&lt;/strong&gt; and &lt;strong&gt;GetContextSite(Context).RootWeb &lt;/strong&gt;).&amp;nbsp; Note the owning &lt;strong&gt;SPSite&lt;/strong&gt; object must be properly Disposed (or not Disposed in the case of SPContext) as described here - &lt;a href="http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx" target="_blank"&gt;SharePoint 2007 and WSS 3.0 Dispose Patterns by Example&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;In addition, SPSite properties &lt;strong&gt;LockIssue&lt;/strong&gt;, &lt;strong&gt;Owner&lt;/strong&gt;, and &lt;strong&gt;SecondaryContact &lt;/strong&gt;internally used the RootWeb property.&amp;nbsp; Based on the updated guidance for RootWeb no explicit Dispose on any of these properties are required.&amp;nbsp; Again, note the same dependency on properly Disposing the owning SPSite object is in effect.&lt;/p&gt;&lt;/blockquote&gt; &lt;blockquote&gt; &lt;p&gt;&lt;/p&gt; &lt;div style="border-right: gray 1px solid; padding-right: 4px; border-top: gray 1px solid; padding-left: 4px; font-size: 8pt; padding-bottom: 4px; margin: 20px 0px 10px; overflow: auto; border-left: gray 1px solid; width: 97.5%; cursor: text; max-height: 600px; line-height: 12pt; padding-top: 4px; border-bottom: gray 1px solid; font-family: consolas, 'Courier New', courier, monospace; height: 290px; background-color: #f4f4f4"&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 99.31%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; height: 274px; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; RootWebBestPractice()
{
    &lt;span style="color: #008000"&gt;// Exception to "Best Practices: Using Disposable Windows SharePoint Services Objects" White paper&lt;/span&gt;

    &lt;span style="color: #008000"&gt;// Example 1 - new SPSite&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPWeb rootWeb1 = siteCollection.RootWeb;
        &lt;span style="color: #008000"&gt;// No explicit rootWeb1 dispose required&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// siteCollection automatically disposed by implementing using()&lt;/span&gt;
    &lt;span style="color: #008000"&gt;// rootWeb1 will be Disposed by SPSite&lt;/span&gt;

    &lt;span style="color: #008000"&gt;// Example 2 - SPContext and SPControl&lt;/span&gt;
    SPWeb rootWeb2 = SPContext.Current.Site.RootWeb;
    &lt;span style="color: #008000"&gt;// Also would apply to SPControl.GetContextSite(Context);&lt;/span&gt;
    &lt;span style="color: #008000"&gt;// No explicit rootWeb2 dispose required because it's obtained from SPContext.Current.Site&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8976769" width="1" height="1"&gt;</description></item><item><title>SharePoint 2007 and WSS 3.0 Dispose Patterns by Example</title><link>http://blogs.msdn.com/rogerla/archive/2008/02/12/sharepoint-2007-and-wss-3-0-dispose-patterns-by-example.aspx</link><pubDate>Wed, 13 Feb 2008 05:42:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7660792</guid><dc:creator>Roger Lamb</dc:creator><slash:comments>67</slash:comments><comments>http://blogs.msdn.com/rogerla/comments/7660792.aspx</comments><wfw:commentRss>http://blogs.msdn.com/rogerla/commentrss.aspx?PostID=7660792</wfw:commentRss><description>&lt;p&gt;Last Updated: &lt;strong&gt;January 30, 2009&lt;/strong&gt;&lt;/p&gt; &lt;blockquote&gt; &lt;p&gt;&lt;font color="#ff0000"&gt;[New]&lt;/font&gt; – The original MSDN article &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; was updated 1/30/2009 and the Dispose() guidance is now in sync with the content on this blog and the new &lt;a href="http://blogs.msdn.com/rogerla/archive/2009/01/29/automate-sharepoint-dispose-code-reviews-with-spdisposecheck.aspx"&gt;SPDisposeCheck&lt;/a&gt; automated Dispose() code review tool.&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;Overview&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;Windows SharePoint Services (WSS 3.0) and Microsoft Office SharePoint Server (MOSS 2007) have some gotchas with serious implications that every SharePoint application developer needs to be intimately familiar with before deploying into production MOSS 2007 farms.&amp;nbsp; In particular, &lt;strong&gt;&lt;/strong&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;Microsoft.SharePoint.SPSite&lt;/a&gt;&lt;/strong&gt; , &lt;strong&gt;&lt;/strong&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;Microsoft.SharePoint.SPWeb&lt;/a&gt;&lt;/strong&gt; , and the often overlooked (MOSS 2007) &lt;strong&gt;&lt;/strong&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.aspx" target="_blank"&gt;Microsoft.SharePoint.Publishing&lt;/a&gt;&lt;/strong&gt; objects need to be carefully examined and disposed of properly in order to avoid potential memory leaks. My original objective with this blog post was to consolidate several different reliable sources compiled by Microsoft's top Subject Matter Experts (SME’s) into a Uber-Dispose quick reference which could be used to increase developer awareness of the current Microsoft Guidance.&amp;nbsp; I’ve been working closely with the SharePoint Product Group as well as the top Microsoft Support and Services SME’s to help carve out the information you see here.&amp;nbsp; I plan to keep this blog post updated with the latest official guidance from Microsoft and while there are more verbose resources available elsewhere the primary objective is to provide lots of samples in a quick reference format.&amp;nbsp; Pre-requisites: The intended audience for this guidance is a seasoned .NET developer who currently is or will be developing custom SharePoint solutions with Visual Studio 2005/2008.&lt;/p&gt;&lt;/blockquote&gt;&lt;a name="SPDisposeCheckID_Index"&gt;&lt;/a&gt; &lt;h3&gt;Quick Reference - SharePoint Example Dispose() Patterns&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;&lt;strong&gt;WSS 3.0&lt;/strong&gt;  &lt;ul&gt; &lt;li&gt;&lt;a href="#SPDisposeCheckID_100"&gt;Microsoft.SharePoint.SPList.BreakRoleInheritance()&lt;/a&gt; method &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;  &lt;li&gt;&lt;a href="#SPDisposeCheckID_110"&gt;Microsoft.SharePoint.SPSite new()&lt;/a&gt; operator  &lt;li&gt;&lt;a href="#SPDisposeCheckID_120"&gt;Microsoft.SharePoint.SPSite.OpenWeb()&lt;/a&gt; method  &lt;li&gt;&lt;a href="#SPDisposeCheckID_130"&gt;Microsoft.SharePoint.SPSite.AllWebs[]&lt;/a&gt; indexer  &lt;li&gt;&lt;a href="#SPDisposeCheckID_140"&gt;Microsoft.SharePoint.SPSite.RootWeb, LockIssue, Owner, and SecondaryContact&lt;/a&gt; properties &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;  &lt;li&gt;&lt;a href="#SPDisposeCheckID_150"&gt;Microsoft.SharePoint.SPSite.AllWebs.Add()&lt;/a&gt; method  &lt;li&gt;&lt;a href="#SPDisposeCheckID_160"&gt;Microsoft.SharePoint.SPWeb.GetLimitedWebPartManager()&lt;/a&gt; method  &lt;li&gt;&lt;a href="#SPDisposeCheckID_170"&gt;Microsoft.SharePoint.SPWeb.ParentWeb&lt;/a&gt; property &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;  &lt;li&gt;&lt;a href="#SPDisposeCheckID_180"&gt;Microsoft.SharePoint.SPWeb.Webs&lt;/a&gt; property  &lt;li&gt;&lt;a href="#SPDisposeCheckID_190"&gt;Microsoft.SharePoint.SPWeb.Webs.Add()&lt;/a&gt; method &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;  &lt;li&gt;&lt;a href="#SPDisposeCheckID_200"&gt;Microsoft.SharePoint.SPWebCollection.Add()&lt;/a&gt; method &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;  &lt;li&gt;&lt;a href="#SPDisposeCheckID_210"&gt;Microsoft.SharePoint.WebControls.SPControl GetContextSite() and GetContextWeb()&lt;/a&gt; methods  &lt;li&gt;&lt;a href="#SPDisposeCheckID_220"&gt;Microsoft.SharePoint.SPContext Current.Site / SPContext.Site and SPContext.Current.Web / SPContext.Web&lt;/a&gt; properties  &lt;li&gt;&lt;a href="#SPDisposeCheckID_230"&gt;Microsoft.SharePoint.Administration.SPSiteCollection[]&lt;/a&gt; indexer  &lt;li&gt;&lt;a href="#SPDisposeCheckID_240"&gt;Microsoft.SharePoint.Administration.Add()&lt;/a&gt; method &lt;/li&gt;&lt;/ul&gt; &lt;li&gt;&lt;strong&gt;MOSS 2007&lt;/strong&gt;  &lt;ul&gt; &lt;li&gt;&lt;a href="#SPDisposeCheckID_300"&gt;Microsoft.SharePoint.Publishing.GetPublishingWebs()&lt;/a&gt; method  &lt;li&gt;&lt;a href="#SPDisposeCheckID_310"&gt;Microsoft.SharePoint.Publishing.PublishingWebCollection.Add()&lt;/a&gt; method  &lt;li&gt;&lt;a href="#SPDisposeCheckID_320"&gt;Microsoft.SharePoint.Publishing.PublishingWeb.GetVariation()&lt;/a&gt; method &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;  &lt;li&gt;&lt;a href="#SPDisposeCheckID_400"&gt;Microsoft.Office.Server.UserProfiles.PersonalSite&lt;/a&gt; property &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt; &lt;/li&gt;&lt;/ul&gt; &lt;li&gt;&lt;strong&gt;Obsolete&lt;/strong&gt;  &lt;ul&gt; &lt;li&gt;&lt;a href="#SPDisposeCheckID_500"&gt;Microsoft.SharePoint.Portal.SiteData.Area.Web&lt;/a&gt; property &lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Why Dispose?&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; classes both implement the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.idisposable_members(VS.80).aspx" target="_blank"&gt;IDisposable&lt;/a&gt;&lt;/strong&gt; interface.&amp;nbsp; Microsoft .NET requires objects that implement the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/system.idisposable_members(VS.80).aspx" target="_blank"&gt;IDisposable&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;interface to properly cleanup the unmanaged resources by explicitly calling the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx" target="_blank"&gt;Dispose()&lt;/a&gt;&lt;/strong&gt; method when you are finished using them.&amp;nbsp; Internally, &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; both hold references to an "&lt;strong&gt;internal class Microsoft.SharePoint.Library.SPRequest"&lt;/strong&gt; which holds on to unmanaged COM resources.&amp;nbsp; The consequence of not explicitly disposing unmanaged resources in a timely fashion can lead to not having enough memory for further allocations and quickly consumes memory. Under the hood, when reviewing dump files we see the that the managed objects used by &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; are relatively small and it's the unmanaged resources that are most concerning and account for approximately 1MB to 2MB for each object instance!&amp;nbsp; Omitting to explicitly call &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx" target="_blank"&gt;Dispose()&lt;/a&gt;&lt;/strong&gt; means the .NET (non-deterministic) garbage collector gets out of sync with the finalizer and the unmanaged memory does not get reclaimed in a timely manner possibly blocking future memory allocations. For further reading I recommend reviewing Stefan Goßner's blog &lt;em&gt;&lt;a href="http://blogs.technet.com/stefan_gossner/archive/2007/11/26/dealing-with-memory-pressure-problems-in-moss-wss.aspx" target="_blank"&gt;Dealing with Memory Pressure problems in MOSS/WSS&lt;/a&gt;&lt;/em&gt; .&lt;/p&gt; &lt;p&gt;The unmanaged memory leaks can grow very quickly especially when traversing through frequently called areas like site navigation code and item event receivers.&amp;nbsp; The lack of proper &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx" target="_blank"&gt;Dispose()&lt;/a&gt;&lt;/strong&gt; hygiene can increase your risk of frequent IIS Application Domain recycles (see Steve Sheppard's blog &lt;a href="http://blogs.msdn.com/steveshe/archive/2007/12/17/overlapped-recycling-and-sharepoint-why-sharepoint-requires-it.aspx" target="_blank"&gt;Overlapped Recycling And SharePoint: Why SharePoint Requires It&lt;/a&gt;), Out Of Memory (OOM) exceptions, high memory consumption, and poor performing SharePoint production environments.&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;To Dispose or not Dispose?! That is the question...&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;To make matters more confusing for SharePoint developers there are times when &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; objects &lt;strong&gt;should not be&lt;/strong&gt; disposed and are cleaned up by SharePoint and ASP.NET after page processing is completed&lt;strong&gt;.&lt;/strong&gt;&amp;nbsp; In addition, there are cases when developers indirectly call a property on a object that creates and holds an internal reference to a &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; object (for example &lt;strong&gt;SPSite.ParentWeb&lt;/strong&gt; property).&amp;nbsp; Understanding the origin and the scope the object was created is paramount when determining whether or not to explicitly call dispose.&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;Dispose Patterns&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;When writing customized SharePoint code you need to be aware of the scope and context of each &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt;,&lt;strong&gt; &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; objects lifetime.&amp;nbsp; When objects are created and destroyed in the same method or iteration scope (foreach or do/while loop) they are the easiest to clean handle.&amp;nbsp; Things become more complex to review when developers create objects in one method and dispose in another.&amp;nbsp; Areas to be aware of are assigning objects to class variables or static/global variables which may hold on to the object reference across method calls.&amp;nbsp; The content in this blog post is a combination of Product Group Guidance along with any edge cases that have been discovered in the field through support incidents.&lt;/p&gt;&lt;/blockquote&gt;&lt;a name="index"&gt;&lt;/a&gt; &lt;h3&gt;Try / finally using() Dispose()&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;Make certain that the objects listed here which need to be properly Disposed by your application code is wrapped within a using(), try/finally, or try/catch/finally block.&amp;nbsp; Failure to do so can lead to unexpected memory leaks as there is no other way to guarantee that your Dispose will be reached in the event of an exception.&amp;nbsp; See my updated guidance &lt;a href="http://blogs.msdn.com/rogerla/archive/2009/01/14/try-finally-using-sharepoint-dispose.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;Troubleshooting Dispose Related Leaks&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;Recently the &lt;a href="http://blogs.msdn.com/rogerla/archive/2008/11/12/spdisposecheck-spsite-and-spweb-dispose-best-practice-utility-announced.aspx" target="_blank"&gt;SPDisposeCheck utility&lt;/a&gt; was announced by Paul Andrew from the Microsoft SharePoint Product Group which was published 1/29/09 &lt;a href="http://code.msdn.microsoft.com/SPDisposeCheck"&gt;here&lt;/a&gt;.&amp;nbsp; This command line utility will help scan your custom MOSS and WSS 3.0 .NET assemblies (MSIL not your original source code) automatically scanning for the guidance listed on this blog post.&amp;nbsp; SPDisposeCheck will produce a summary report calling your attention to areas in your code you that you need to closely examine for possible Dispose() related leaks.&amp;nbsp; After you have completed your initial scan with SPDisposeCheck you should examine the SharePoint ULS logs to help further verify that no Dispose() edge cases will be introduced into your shared development, test, and production environments.&amp;nbsp; Stefan Goßner (Microsoft Support Escalation Engineer) has created guidance on using the ULS logs to &lt;a href="http://blogs.technet.com/stefan_gossner/archive/2008/05/07/troubleshooting-spsite-spweb-leaks-in-wss-v3-and-moss-2007.aspx" target="_blank"&gt;Troubleshooting SPSite/SPWeb leaks in WSS v3 and MOSS 2007&lt;/a&gt; .&amp;nbsp; See also Stefan’s &lt;a href="http://blogs.technet.com/stefan_gossner/archive/2008/12/05/disposing-spweb-and-spsite-objects.aspx"&gt;Disposing SPWeb and SPSite objects&lt;/a&gt; for additional info.&amp;nbsp; For advanced troubleshooting you can ($) contact Microsoft Support or if you have a Premier Contract you can engage Microsoft Premier Support’s Developer Advisory Services (DAS) or Premier Field Engineering (PFE) resources for advanced troubleshooting, debugging, and proactive advisory services including custom code reviews.&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;SharePoint Memory Internals&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;400 Level – (Optional read)&lt;/em&gt; It’s very important for the developer of the application code to understand when is the optimal time for code optimization and performance reasons to hold on to and release the proper SharePoint objects in a timely fashion.&amp;nbsp; As SharePoint Application developers you should be familiar with the public SPSite and SPWeb objects but internally to get their work done the real managed object that holds onto the unmanaged heap is SPRequestInternalClass which is internal to a wrapper class SPRequest.  &lt;p&gt;Internally we use a RCW (runtime callable wrapper) which is essentially a finalizable object (there are subtle differences but they don’t really come into play here).&amp;nbsp;&amp;nbsp;&amp;nbsp; If that object is no longer rooted, the finalizer (teardown of the RCW) will release the native memory.&amp;nbsp;&amp;nbsp; However, like normal finalizers the RCW teardown is performed by a single finalizer thread which generally can’t keep up with cleaning these objects if they’re being leaked many times per second.  &lt;p&gt;Not releasing the SharePoint objects in a timely fashion can lead to poor memory hygiene including excessive fragmentation, pinning, and OOM exceptions building very quickly.&amp;nbsp; Problematic code which fails to properly dispose objects in a timely fashion becomes exacerbated especially in x32bit environments with a large number of sites.&amp;nbsp; We encourage all Enterprise SharePoint farms to use x64bit versions of the OS and MOSS to take advantage of the additional addressable memory.  &lt;p&gt;Note resources other than just memory are effected by not properly disposing your object in a timely fashion.&amp;nbsp; For example, SQL Server establishes a 1:1 connection with &lt;strong&gt;each &lt;/strong&gt;SPRequest object and it lives up to the release of the internal SPRequest object (which is used by your SPSite and SPWeb object).&lt;/p&gt;&lt;/blockquote&gt; &lt;h3&gt;Microsoft.SharePoint.SPList&lt;/h3&gt; &lt;blockquote&gt; &lt;p&gt;&lt;/p&gt;&lt;/blockquote&gt; &lt;ul&gt; &lt;li&gt;&lt;a name="SPDisposeCheckID_100"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated] -&lt;/font&gt; &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.splist.breakroleinheritance.aspx" target="_blank"&gt;SPList.BreakRoleInheritance()&lt;/a&gt;&lt;/strong&gt; method no longer requires list.ParentWeb.Dispose() to be executed after calling BreakRoleInheritance() due to the updated ParentWeb guidance (see &lt;a href="#SPDisposeCheckID_170"&gt;ParentWeb&lt;/a&gt;) later in this blog post.&lt;/li&gt;&lt;/ul&gt; &lt;h3&gt;Microsoft.SharePoint.SPSite&lt;/h3&gt; &lt;ul&gt; &lt;li&gt;&lt;a name="SPDisposeCheckID_140"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated] &lt;strong&gt;-&lt;/strong&gt;&lt;/font&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.rootweb.aspx" target="_blank"&gt;&lt;strong&gt;RootWeb&lt;/strong&gt;&lt;/a&gt; property no longer requires Dispose() to be called on itself as previously indicated in the Whitepaper &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx" target="_blank"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; .&amp;nbsp; See my blog post &lt;a href="http://blogs.msdn.com/rogerla/archive/2008/10/04/updated-spsite-rootweb-dispose-guidance.aspx" target="_blank"&gt;here&lt;/a&gt; for more detail.&amp;nbsp; In addition, properties &lt;strong&gt;LockIssue&lt;/strong&gt;, &lt;strong&gt;Owner&lt;/strong&gt;, and &lt;strong&gt;SecondaryContact &lt;/strong&gt;internally used the RootWeb property.&amp;nbsp; Based on the updated Microsoft guidance for &lt;strong&gt;RootWeb&lt;/strong&gt; there is no longer a requirement to explicitly Dispose on any of these properties.&amp;nbsp; Note that &lt;strong&gt;the owning SPSite object must be properly Disposed (or not Disposed in the case of SPContext)&lt;/strong&gt; as described by the rules listed elsewhere in this blog.&lt;br&gt;&lt;br&gt; &lt;li&gt;&lt;a name="SPDisposeCheckID_110"&gt;&lt;/a&gt;&lt;strong&gt;new &lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite()&lt;/a&gt;&lt;/strong&gt; operator - Instantiating &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; objects with the &lt;strong&gt;new&lt;/strong&gt; operator needs to be disposed.&lt;/li&gt;&lt;/ul&gt; &lt;blockquote&gt; &lt;p&gt;&lt;em&gt;Note: With C# you can automatically have the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx" target="_blank"&gt;Dispose()&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;called for you when the object leaves the scope by wrapping the code with the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/yh598w02.aspx" target="_blank"&gt;using() { }&lt;/a&gt;&lt;/strong&gt; statement.&lt;/em&gt;&lt;/p&gt; &lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 425px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 408px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CreatingSPSiteLeak()
{
    SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;);
    &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// siteCollection leaked&lt;/font&gt;&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; CreatingSPSiteExplicitDisposeNoLeak()
{
    SPSite siteCollection = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
    &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
    {
        siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;);
    }
    &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (siteCollection != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
            siteCollection.Dispose();
    }
}

CreatingSPSiteWithAutomaticDisposeNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_120"&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms473155.aspx" target="_blank"&gt;OpenWeb()&lt;/a&gt;&lt;/strong&gt; returns a &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; object which needs to be disposed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;font color="#000000"&gt;Avoid the following pattern which collapses &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; calls.&amp;nbsp; The example returns the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; site object wrapped by a using statement which gets disposed but there is no way to dispose the underlying &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; object.&lt;/font&gt;&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 98.64%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 285px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OpenWebLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(SPContext.Current.Web.Url).OpenWeb())
    {
        &lt;span style="color: #008000"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;// SPSite leaked !&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;
    } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OpenWebNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_130"&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.allwebs.aspx" target="_blank"&gt;AllWebs[]&lt;/a&gt;&lt;/strong&gt; Indexer returns &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; object that needs to be disposed to avoid aggregation of memory which can lead to memory pressure when running on a site collection with large number of sub sites.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 870px; max-height: 1000px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 856px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllWebsForEachLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (SPWeb innerWeb &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; siteCollection.AllWebs)
            {
                &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// explicit dispose here to avoid OOM's with large # of webs&lt;/font&gt;&lt;/span&gt;
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllWebsForEachNoLeakOrMemoryOOM()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (SPWeb innerWeb &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; siteCollection.AllWebs)
            {
                &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
                {
                    &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
                }
                &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
                {
                    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(innerWeb != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                        innerWeb.Dispose();
                }
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllWebsIndexerLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPWeb web = siteCollection.AllWebs[0];
        &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// SPWeb web leaked&lt;/font&gt;&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllWebsIndexerNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.AllWebs[0])
        {
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_150"&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spwebcollection.add.aspx" target="_blank"&gt;AllWebs.Add()&lt;/a&gt;&lt;/strong&gt; returns a instance of &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; object which needs to be disposed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 294px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 98.14%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 262px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllWebsAddLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPWeb web = siteCollection.AllWebs.Add(&lt;span style="color: #006080"&gt;"site-relative URL"&lt;/span&gt;);
        &lt;span style="color: #008000"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;// SPWeb web Leaked&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AllWebsAddNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.AllWebs.Add(&lt;span style="color: #006080"&gt;"site-relative URL"&lt;/span&gt;))
        {
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h4&gt;Microsoft.SharePoint.SPWeb&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_170"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt; – &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.parentweb.aspx" target="_blank"&gt;ParentWeb&lt;/a&gt;&lt;/strong&gt; property no longer requires Dispose() as previously indicated in the Whitepaper &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx" target="_blank"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; .&amp;nbsp; Note that &lt;strong&gt;the owning SPSite object must be properly Disposed (or not Disposed in the case of SPContext)&lt;/strong&gt; as described by the rules listed elsewhere in this blog.&lt;br&gt;&lt;br&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_160"&gt;&lt;/a&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.getlimitedwebpartmanager.aspx" target="_blank"&gt;&lt;strong&gt;GetLimitedWebPartManager()&lt;/strong&gt;&lt;/a&gt; method returns a &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; object which needs to be disposed.&amp;nbsp; &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 570px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 548px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPLimitedWebPartManagerLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            SPFile page = web.GetFile(&lt;span style="color: #006080"&gt;"Source_Folder_Name/Source_Page"&lt;/span&gt;);
            SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared);
            &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// SPWeb object webPartManager.Web leaked&lt;/font&gt;&lt;/span&gt;
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPLimitedWebPartManagerNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            SPFile page = web.GetFile(&lt;span style="color: #006080"&gt;"Source_Folder_Name/Source_Page"&lt;/span&gt;);
            &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPLimitedWebPartManager webPartManager = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
            {
                &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
                {
                    &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
                }
                &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
                {
                    webPartManager.Web.Dispose();
                }
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}
 &lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_180"&gt;&lt;/a&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spweb.webs.aspx" target="_blank"&gt;&lt;strong&gt;SPWeb.Webs&lt;/strong&gt;&lt;/a&gt; property returns &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spwebcollection.aspx" target="_blank"&gt;&lt;strong&gt;SPWebCollection&lt;/strong&gt;&lt;/a&gt; object that needs to be disposed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 587px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 554px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; WebsLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (SPWeb innerWeb &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; outerWeb.Webs)
            {
                &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// SPWeb innerWeb leak&lt;/font&gt;&lt;/span&gt;
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; WebsNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (SPWeb innerWeb &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; outerWeb.Webs)
            {
                &lt;span style="color: #0000ff"&gt;try&lt;/span&gt; &lt;span style="color: #008000"&gt;//should be 1st statement after foreach&lt;/span&gt;
                {
                    &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
                }
                &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
                {
                    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(innerWeb != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                        innerWeb.Dispose();
                }
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_190"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt; – SPWeb.Webs.Add() method returns a SPWeb object that needs to be disposed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 438px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 407px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; WebsAddLeak(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; strWebUrl)
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            SPWeb addedWeb = web.Webs.Add(strWebUrl);   &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// will leak&lt;/font&gt;&lt;/span&gt;

        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; WebsAddNoLeak(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; strWebUrl)
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb addedWeb = web.Webs.Add(strWebUrl))
            {
                &lt;span style="color: #008000"&gt;//..&lt;/span&gt;
            }

        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Microsoft.SharePoint.SPWebCollection&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_200"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt; – &lt;a href="http://msdn.microsoft.com/en-us/library/ms411806.aspx" target="_blank"&gt;&lt;strong&gt;SPWebCollection.Add()&lt;/strong&gt;&lt;/a&gt; method returns a &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; object that needs to be disposed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 433px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 419px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPWebCollectionAddLeak(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; strWebUrl)
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            SPWebCollection webCollection = siteCollection.AllWebs; &lt;span style="color: #008000"&gt;// no AllWebs leak just getting reference&lt;/span&gt;
            SPWeb innerWeb = webCollection.Add(strWebUrl);  &lt;span style="color: #008000"&gt;// must dispose of innerWeb&lt;/span&gt;
            &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// innerWeb Leak&lt;/font&gt;&lt;/span&gt;
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPWebCollectionAddNoLeak(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; strWebUrl)
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb outerWeb = siteCollection.OpenWeb())
        {
            SPWebCollection webCollection = siteCollection.AllWebs; &lt;span style="color: #008000"&gt;// no AllWebs leak just getting reference&lt;/span&gt;
            &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb innerWeb = webCollection.Add(strWebUrl))
            {
                &lt;span style="color: #008000"&gt;//...&lt;/span&gt;
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Microsoft.SharePoint.WebControls.SPControl&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_210"&gt;&lt;/a&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spcontrol.getcontextsite.aspx" target="_blank"&gt;&lt;strong&gt;GetContextSite(Context)&lt;/strong&gt;&lt;/a&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spcontrol.getcontextweb.aspx" target="_blank"&gt;GetContextWeb(Context)&lt;/a&gt;&lt;/strong&gt; methods return &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; objects respectively that &lt;strong&gt;&lt;font color="#ff0000"&gt;DO NOT&lt;/font&gt; &lt;/strong&gt;need a call to &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx" target="_blank"&gt;&lt;strong&gt;Dispose()&lt;/strong&gt;&lt;/a&gt; and will be cleaned up automatically by SharePoint&lt;strong&gt;.&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 233px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 98.36%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 219px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPControlBADPractice()
{
    SPSite siteCollection = SPControl.GetContextSite(Context);
    siteCollection.Dispose();   &lt;span style="color: #008000"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;// DO NOT DO THIS&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;
    SPWeb web = SPControl.GetContextWeb(Context);
    web.Dispose();  &lt;span style="color: #008000"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;// DO NOT DO THIS&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPControlBestPractice()
{
    SPSite siteCollection = SPControl.GetContextSite(Context);
    SPWeb web = SPControl.GetContextWeb(Context);
    &lt;span style="color: #008000"&gt;// Do NOT call Dispose()&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Microsoft.SharePoint.SPContext&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_220"&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.current.aspx" target="_blank"&gt;SPContext.Current.Site&lt;/a&gt; &amp;amp; &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.site.aspx" target="_blank"&gt;SPContext.Site&lt;/a&gt;&lt;/strong&gt; as well as &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.current.aspx" target="_blank"&gt;SPContext.Current.Web&lt;/a&gt; &amp;amp; &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spcontext.web.aspx" target="_blank"&gt;SPContext.Web&lt;/a&gt;&lt;/strong&gt; properties return &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&lt;/strong&gt; objects respectively that &lt;strong&gt;&lt;font color="#ff0000"&gt;DO NOT&lt;/font&gt; &lt;/strong&gt;need a call to &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spsite.dispose.aspx" target="_blank"&gt;&lt;strong&gt;Dispose()&lt;/strong&gt;&lt;/a&gt; and will be disposed automatically by SharePoint&lt;strong&gt;.&lt;/strong&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 227px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 98.36%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 207px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPContextBADPractice()
{
    SPSite siteCollection = SPContext.Current.Site;
    siteCollection.Dispose(); &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// DO NOT DO THIS&lt;/font&gt;&lt;/span&gt;
    SPWeb web = SPContext.Current.Web;
    web.Dispose(); &lt;span style="color: #008000"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;// DO NOT DO THIS&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPContextBestPractice()
{
    SPSite siteCollection = SPContext.Current.Site;
    SPWeb web = SPContext.Current.Web;
    &lt;span style="color: #008000"&gt;// Do NOT call Dispose()&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Microsoft.SharePoint.Publishing&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_300"&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.getpublishingwebs.aspx" target="_blank"&gt;GetPublishingWebs()&lt;/a&gt;&lt;/strong&gt; method returns a &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingwebcollection.aspx" target="_blank"&gt;PublishingWebCollection&lt;/a&gt;&lt;/strong&gt; that you are required to call &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.close.aspx" target="_blank"&gt;Close()&lt;/a&gt;&lt;/strong&gt; on each of the returned PublishingWeb objects inside the loop.&amp;nbsp; Note that when you are only calling &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms497306.aspx" target="_blank"&gt;GetPublishingWeb(SPWeb)&lt;/a&gt;&lt;/strong&gt; method (not the collection) you should &lt;strong&gt;not &lt;/strong&gt;call Close().&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 898px; max-height: 1000px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 885px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PublishingWebCollectionLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            &lt;span style="color: #008000"&gt;// passing in web you own, no dispose needed on outerPubWeb&lt;/span&gt;
            PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);

            PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (PublishingWeb innerPubWeb &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; pubWebCollection)
            {
                &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// innerPubWeb leak&lt;/font&gt;&lt;/span&gt;
            }
            &lt;span style="color: #008000"&gt;// PublishingWeb will leak for each innerPubWeb referenced&lt;/span&gt;
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PublishingWebCollectionNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            &lt;span style="color: #008000"&gt;// passing in web you own, no dispose needed on outerPubWeb&lt;/span&gt;
            PublishingWeb outerPubWeb = PublishingWeb.GetPublishingWeb(web);
            PublishingWebCollection pubWebCollection = outerPubWeb.GetPublishingWebs();
            &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (PublishingWeb innerPubWeb &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; pubWebCollection)
            {
                &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
                {
                    &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
                }
                &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
                {
                    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(innerPubWeb != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                        innerPubWeb.Close();
                }
            }
            &lt;span style="color: #008000"&gt;// outerPubWeb.Close(); not needed and if called will log warning in ULS log&lt;/span&gt;
        }  &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; GetPublishingWebNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            &lt;span style="color: #008000"&gt;// passing in web you own, no dispose needed on singlePubWeb&lt;/span&gt;
            PublishingWeb singlePubWeb = PublishingWeb.GetPublishingWeb(web);
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_320"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated]&lt;/font&gt;&amp;nbsp;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.getvariation.aspx" target="_blank"&gt;&lt;strong&gt;PublishingWeb.GetVariation()&lt;/strong&gt;&lt;/a&gt; method returns a &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.aspx" target="_blank"&gt;&lt;strong&gt;PublishingWeb&lt;/strong&gt;&lt;/a&gt; object which which needs to be explicitly closed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 579px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 570px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; GetVariationLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);  &lt;span style="color: #008000"&gt;// Passing in web so no Close() needed&lt;/span&gt;
            VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
            PublishingWeb variationPublishingWeb = publishingWeb.GetVariation(variationLabel);  &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// must be Closed()&lt;/font&gt;&lt;/span&gt;
            &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
        } &lt;span style="color: #008000"&gt;// SPWeb object web.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; GetVariationNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb web = siteCollection.OpenWeb())
        {
            PublishingWeb variationPublishingWeb = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
            &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
            {
                PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);  &lt;span style="color: #008000"&gt;// Passing in web so no Close() needed&lt;/span&gt;
                VariationLabel variationLabel = Variations.Current.UserAccessibleLabels[0];
                variationPublishingWeb = publishingWeb.GetVariation(variationLabel);  &lt;span style="color: #008000"&gt;// must be Closed()&lt;/span&gt;
                &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
            }
            &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
            {
                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(variationPublishingWeb != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                    variationPublishingWeb.Close();
            }
        } &lt;span style="color: #008000"&gt;// SPWeb object outerWeb.Dispose() automatically called&lt;/span&gt;
    }  &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called &lt;/span&gt;
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_310"&gt;&lt;/a&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingwebcollection.add.aspx" target="_blank"&gt;&lt;strong&gt;PublishingWebCollection.Add()&lt;/strong&gt;&lt;/a&gt; method requires you to call Close() on the returned &lt;strong&gt;PublishingWeb&lt;/strong&gt; object.&amp;nbsp; For a code sample refer to the MOSS SDK &lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.publishing.publishingweb.getpublishingwebs.aspx" target="_blank"&gt;&lt;strong&gt;GetPublishingWebs()&lt;/strong&gt;&lt;/a&gt; sample.&lt;/li&gt;&lt;/ul&gt;
&lt;h3&gt;Microsoft.Office.Server.UserProfiles&lt;/h3&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;a name="SPDisposeCheckID_400"&gt;&lt;/a&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.office.server.userprofiles.userprofile.personalsite.aspx" target="_blank"&gt;&lt;strong&gt;UserProfiles.PersonalSite&lt;/strong&gt;&lt;/a&gt; requires a Dispose() call before leaving scope if you use the property in your code.&amp;nbsp; For addition code optimization information please see my blog post &lt;a href="http://blogs.msdn.com/rogerla/archive/2009/01/14/sharepoint-2007-userprofiles-personalsite-property-leak.aspx" target="_blank"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 177px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 152px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PersonalSiteLeak()
{
    &lt;span style="color: #008000"&gt;// open a site collection&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        UserProfileManager profileManager = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; UserProfileManager(ServerContext.GetContext(siteCollection));
        UserProfile profile = profileManager.GetUserProfile(&lt;span style="color: #006080"&gt;"domain\\username"&lt;/span&gt;);
        SPSite personalSite = &lt;font color="#ff0000"&gt;profile.PersonalSite;&lt;/font&gt;    &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// will leak&lt;/font&gt;&lt;/span&gt;
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 220px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 199px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; PersonalSiteNoLeak()
{
    &lt;span style="color: #008000"&gt;// open a site collection&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        UserProfileManager profileManager = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; UserProfileManager(ServerContext.GetContext(siteCollection));
        UserProfile profile = profileManager.GetUserProfile(&lt;span style="color: #006080"&gt;"domain\\username"&lt;/span&gt;);
        &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite personalSite = profile.PersonalSite)
        {
            &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
        }
    }
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Microsoft.SharePoint.Administration&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_230"&gt;&lt;/a&gt;&lt;b&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.administration.spsitecollection.aspx" target="_blank"&gt;SPSiteCollection[]&lt;/a&gt;&lt;/b&gt; indexer returns a &lt;b&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/b&gt; object which needs to be disposed.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 1077px; max-height: 1200px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 1061px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPSiteCollectionIndexerLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollectionOuter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPWebApplication webApp = siteCollectionOuter.WebApplication;
        SPSiteCollection siteCollections = webApp.Sites;

        SPSite siteCollectionInner = siteCollections[0];
        &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// SPSite siteCollectionInner leak&lt;/font&gt; &lt;/span&gt;
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollectionOuter.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPSiteCollectionIndexerNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollectionOuter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPSite siteCollectionInner = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
        &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
        {
            SPWebApplication webApp = siteCollectionOuter.WebApplication;
            SPSiteCollection siteCollections = webApp.Sites;

            siteCollectionInner = siteCollections[0];
        }
        &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
        {
            &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (siteCollectionInner != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                siteCollectionInner.Dispose();
        }
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollectionOuter.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPSiteCollectionForEachLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollectionOuter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPWebApplication webApp = siteCollectionOuter.WebApplication;
        SPSiteCollection siteCollections = webApp.Sites;

        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (SPSite siteCollectionInner &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; siteCollections)
        {
            &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// SPSite siteCollectionInner leak&lt;/font&gt;&lt;/span&gt;
        }
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollectionOuter.Dispose() automatically called&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPSiteCollectionForEachNoLeak()
{
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollectionOuter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;))
    {
        SPWebApplication webApp = siteCollectionOuter.WebApplication;
        SPSiteCollection siteCollections = webApp.Sites;

        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (SPSite siteCollectionInner &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; siteCollections)
        {
            &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;
            {
                &lt;span style="color: #008000"&gt;// ...&lt;/span&gt;
            }
            &lt;span style="color: #0000ff"&gt;finally&lt;/span&gt;
            {
                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt;(siteCollectionInner != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
                    siteCollectionInner.Dispose();
            }
        }
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollectionOuter.Dispose() automatically called&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_240"&gt;&lt;/a&gt;&lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms437468.aspx" target="_blank"&gt;Add()&lt;/a&gt;&lt;/strong&gt; returns a &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&lt;/strong&gt; object which needs to be disposed. &lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 258px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 98.14%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 243px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPSiteCollectionAddLeak()
{
    SPWebApplication webApp = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;).WebApplication;
    SPSiteCollection siteCollections = webApp.Sites;
    SPSite siteCollection = siteCollections.Add(&lt;span style="color: #006080"&gt;"sites/myNewSiteCollection"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"DOMAIN\\User"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"roger.lamb@litwareinc.com"&lt;/span&gt;);
    &lt;span style="color: #008000"&gt;&lt;strong&gt;&lt;font color="#ff0000"&gt;// SPSite siteCollection leak&lt;/font&gt;&lt;/strong&gt;&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; SPSiteCollectionAddNoLeak()
{
    SPWebApplication webApp = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;).WebApplication;
    SPSiteCollection siteCollections = webApp.Sites;
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPSite siteCollection = siteCollections.Add(&lt;span style="color: #006080"&gt;"sites/myNewSiteCollection"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"DOMAIN\\User"&lt;/span&gt;, &lt;span style="color: #006080"&gt;"roger.lamb@litwareinc.com"&lt;/span&gt;))
    {
    } &lt;span style="color: #008000"&gt;// SPSite object siteCollection.Dispose() automatically called&lt;/span&gt;
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Microsoft.SharePoint.Portal.SiteData.Area (Obsolete)&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a name="SPDisposeCheckID_500"&gt;&lt;/a&gt;&lt;font color="#ff0000"&gt;[Updated] -&lt;/font&gt; &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.portal.sitedata.area.web.aspx" target="_blank"&gt;&lt;strong&gt;Area.Web&lt;/strong&gt;&lt;/a&gt; property creates a SPWeb object that needs to be Disposed().&amp;nbsp; Although the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.portal.sitedata.area.aspx" target="_blank"&gt;Area&lt;/a&gt;&lt;/strong&gt; class is &lt;a href="http://msdn2.microsoft.com/en-us/library/ms544843.aspx" target="_blank"&gt;&lt;strong&gt;obsolete&lt;/strong&gt;&lt;/a&gt; in MOSS 2007 it is still of concern when migrating legacy code.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 297px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 90%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 284px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AreaWebLeak()
{
    &lt;span style="color: #008000"&gt;// AreaManager and Area are obsolete in MOSS but this should still be noted&lt;/span&gt;
    Area area = AreaManager.GetArea(PortalContext.Current, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Guid(&lt;span style="color: #006080"&gt;"{GUID}"&lt;/span&gt;));
    SPWeb areaWeb = area.Web;   &lt;span style="color: #008000"&gt;&lt;font color="#ff0000"&gt;// this SPWeb object must be disposed&lt;/font&gt;&lt;/span&gt;
    &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; str = areaWeb.Title;
    &lt;span style="color: #008000"&gt;// SPWeb areaWeb leak&lt;/span&gt;
}

&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AreaWebNoLeak()
{
    &lt;span style="color: #008000"&gt;// AreaManager and Area are obsolete in MOSS but this should still be noted&lt;/span&gt;
    Area area = AreaManager.GetArea(PortalContext.Current, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Guid(&lt;span style="color: #006080"&gt;"{GUID}"&lt;/span&gt;));
    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (SPWeb areaWeb = area.Web)
    {
        &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; str = areaWeb.Title;
    }
}&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Cross Method Dispose Patterns&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;The following example demonstrates the common practice of holding onto the &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;objects across methods in a class.&amp;nbsp; There are times where this design pattern is required however make sure you don't overlook the appropriate time to call dispose when you are finished with the cross method calls.&amp;nbsp; Below is an example of this pattern and shows and example of a leak of both &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/ms462369.aspx" target="_blank"&gt;SPSite&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;and &lt;strong&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/microsoft.sharepoint.spweb.aspx" target="_blank"&gt;SPWeb&lt;/a&gt;&amp;nbsp;&lt;/strong&gt;when the class is torn down.&lt;/li&gt;&lt;/ul&gt;
&lt;blockquote&gt;
&lt;div style="border-bottom: gray 1px solid; border-left: gray 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: consolas, 'Courier New', courier, monospace; height: 439px; max-height: 800px; font-size: 8pt; overflow: auto; border-top: gray 1px solid; cursor: text; border-right: gray 1px solid; padding-top: 4px"&gt;&lt;pre style="border-bottom-style: none; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 97.74%; padding-right: 0px; font-family: consolas, 'Courier New', courier, monospace; border-top-style: none; height: 242px; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; CrossMethodLeak
{
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; SPSite _siteCollection = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;
    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; SPWeb _web = &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;;

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MethodA()
    {
        _siteCollection = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SPSite(&lt;span style="color: #006080"&gt;"http://moss"&lt;/span&gt;);
        _web = _siteCollection.OpenWeb();
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MethodB()
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_web != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; title = _web.Title;
        }
    }

    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; MethodC()
    {
        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (_web != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name = _web.Name;
        }
    }
}
&lt;/pre&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;h3&gt;Summary&lt;/h3&gt;
&lt;p&gt;If you have extended MOSS 2007 or WSS 3.0 with custom code you should digest this post carefully to avoid expensive consequences commonly found in production environments.&amp;nbsp; For a more verbose explanation of many of the topics covered in this blog post I suggest you read Scott Harris's MSDN White Papers &lt;a href="http://msdn2.microsoft.com/en-us/library/aa973248.aspx" target="_blank"&gt;Best Practices: Using Disposable Windows SharePoint Services Objects&lt;/a&gt; and &lt;em&gt;&lt;a href="http://msdn2.microsoft.com/en-us/library/bb687949.aspx" target="_blank"&gt;Best Practices: Common Coding Issues When Using the SharePoint Object Model&lt;/a&gt;&lt;/em&gt; .&lt;/p&gt;
&lt;p&gt;Special thanks to my Microsoft colleagues Scott Harris, Stefan Goßner, Steve Sheppard, Sean Thompson, Lisa Guthrie, Cliff Green, and Rick Caudle for reviewing and helping contribute to this blog post.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7660792" width="1" height="1"&gt;</description></item></channel></rss>