Archive for November 13th, 2007

Best Practice : Using disposable objects in SharePoint

Tuesday, November 13th, 2007

I found this article on MSDN via Andrew's post posing the question "So is it best practice to only use C# for SharePoint development?". A good question and one that will have plenty of comment from all I imagine. For what it's worth (although not the point of this post), I'm a fan of C# because I like using (SPSite currentSite=new SPSite(http://Server)) …

The interesting thing I took from the MSDN article was that objects that are accessed via SPSite objects, like RootWeb and ParentWeb need to be explicitly disposed of when you're finished with them. I had not appreciated this. I figured that if you 'used' using on the SPSite object then that would dispose all the members etc of that object. Not so say our MS friends! Smile

This means:

String str;
using(SPSite oSPSite = new SPSite("http://server"))
{
   str = oSPSite.RootWeb.Title;
   str = oSPSite.RootWeb.Url;

   … additional processing on RootWeb …

oSPSite.RootWeb.Dispose();
}

And, in the interest of fairness: 

Dim str as string
Dim oSPSite as SPSite

oSPSite=new SPSite(http://server)

str = oSPSite.RootWeb.Title
str = oSPSite.RootWeb.Url

… additional processing on RootWeb …

oSPSite.RootWeb.Dispose()
oSPSite.Dispose()

Not really that difficult, but it will make a difference to your memory usage, have positive performance benefits and keep the "Potentially excessive number of SPRequest objects (11) currently unreleased on thread" error messages out of the already huge logs!

Regardless of you language of choice, this article is definitely worth a read if you are coding for SharePoint!

 

Best Practice : Using disposable objects in SharePoint

Tuesday, November 13th, 2007

I found this article on MSDN via Andrew's post posing the question "So is it best practice to only use C# for SharePoint development?". A good question and one that will have plenty of comment from all I imagine. For what it's worth (although not the point of this post), I'm a fan of C# because I like using (SPSite currentSite=new SPSite(http://Server)) …

The interesting thing I took from the MSDN article was that objects that are accessed via SPSite objects, like RootWeb and ParentWeb need to be explicitly disposed of when you're finished with them. I had not appreciated this. I figured that if you 'used' using on the SPSite object then that would dispose all the members etc of that object. Not so say our MS friends! Smile

This means:

String str;
using(SPSite oSPSite = new SPSite("http://server"))
{
   str = oSPSite.RootWeb.Title;
   str = oSPSite.RootWeb.Url;

   … additional processing on RootWeb …

oSPSite.RootWeb.Dispose();
}

And, in the interest of fairness: 

Dim str as string
Dim oSPSite as SPSite

oSPSite=new SPSite(http://server)

str = oSPSite.RootWeb.Title
str = oSPSite.RootWeb.Url

… additional processing on RootWeb …

oSPSite.RootWeb.Dispose()
oSPSite.Dispose()

Not really that difficult, but it will make a difference to your memory usage, have positive performance benefits and keep the "Potentially excessive number of SPRequest objects (11) currently unreleased on thread" error messages out of the already huge logs!

Regardless of you language of choice, this article is definitely worth a read if you are coding for SharePoint!