Thursday, February 17, 2011

Performance-Performance-Performance, short review!

Every time I teach the basics of SharePoint to a .net developer, I make sure he is aware for disposing certain objects in the SharePoint API.

It's wrong to think the garbage collector will dispose objects for you. You have to dispose all the objects that are unmanaged code ( SPSite,SPWeb, WebServices objects ).

If I don't do it, what will happen ?

1. More server memory consume - hence lower performance.
2. More application pool recycles (reaching the limit of it earlier).

So it is important, how should I do that ?

You have 3 approaches:
1. Pure manually
 
SPSite site = new SPSite("http://mysite");
//do some work
site.Dispose();

2. Try, catch and FINALLY
 
try{
SPSite site = new SPSite("http://mysite");
//do some work
return ...
}
catch (Exception e ){
//handle the exception
}
finally
{
if(site!=null)
site.dispose();
}


3. Using - auto dispose, my favorite. The objects will be disposed once you are done using them automatically.
 
using(SPSite site = new SPSite("http://mysite"))
{
using(SPWeb web = site.OpenWeb())
{
//do more stuff
}
}


Just before closing the post -
Remember to check if you need to do it yourslef or not (event though it seems u should), for exmple cases like -
SPSite.RootWeb - Used to be needed, not anymore...
SPSite().OpenWeb - No need to call SPWeb.Dispose, being called auto.
and etc...

So be sure you are familiar with this article to get the full understanding of this issue.
Msdn article

No comments:

Post a Comment