Wednesday, October 28, 2009

SharePoint 101 - Dispose of SPWeb Objects

One of the most important things you can do to ensure a stable and responsive SharePoint environment is to dispose of SPWeb objects. SPWeb object are fat. The "automatic" garbage collection in .net 2.0 just doesn't do the job. So, use a using statement or explicitly call dispose. If you are passing SPWeb objects around you need to dispose of them in the method were they are used. The only time you'll run into issues disposing of an SPWeb object is when it's the current web like SPWeb myWeb = SPContext.Current.Web. Go ahead and try disposing of it anyway, it's better to fix the error and make sure you're disposing properly. A tool is available to help you check your code, SPDisposeCheck.

SPWeb myWeb = site.OpenWeb();
    if(myWeb != null)

using(SPWeb myWeb = site.OpenWeb())

No comments:

Post a Comment