Saturday, November 15, 2008

SharePoint: Best Practices

"Several of the Windows SharePoint Services objects, primarily the SPSite class and SPWeb class objects, are created as managed objects. However, these objects use unmanaged code and memory to perform the majority of their work. The managed part of the object is small; the unmanaged part of the object is much larger. Because the smaller managed part of the object does not put memory pressure on the garbage collector, the garbage collector does not release the object from memory in a timely manner. The object's use of a large amount of unmanaged memory can cause some of the unusual behaviors described earlier. Calling applications that work with IDisposable objects in Windows SharePoint Services must dispose of the objects when the applications finish using them. You should not rely on the garbage collector to release them from memory automatically." - MSDN [Scott Harris - Microsoft Corporation, Mike Ammerlaan - Microsoft Corporation : November 2006]

Best techniques:
- Dispose method
- using clause
- try, catch, and finally blocks

The SPSite.RootWeb property and SPWeb.ParentWeb property create new objects and assign them to a local member variable. So don't forget to dispose them to.

[code]
String str; using(SPSite oSPSite = new SPSite("http://server")) {    using(SPWeb oRootWeb = oSPSite.RootWeb)    {       str = oRootWeb.Title;       str = oRootWeb.Url;    … additional processing on RootWeb …     } }
[/code]

SPSiteCollection Class

This section describes the methods, properties, or operators in the SPSiteCollection object that require the returned SPSite object to be closed after access.

SPSiteCollection.Add Method

The SPSiteCollection.Add method creates and returns a new SPSite object. You should dispose of any SPSite object returned from the SPSiteCollection.Addmethod.

Must visit URL's:

No comments: