Monday, February 1, 2010

Improve MOSS Performance


Sharepoint/ MOSS is a resource intensive application. And this is exactly the reason why the sharepoint / MOSS developer needs to be extra cautious on the performance implication of the code that they spin out.
Here is some pointers to areas where the processing power is drained out.  A little caution here and there can increase the performance of the application significantly.

During the development phase, the developers may not notice the impact of the performance to the full extent. However, once the application moves to production and data accumulates over the time, then the same lines of codes will start taking more and more time to execute.

Check for memory Leaks 
Most Sharepoint developers will know that they have to dispose the SPSite object. But why doesn’t the garbage collector treat them also like any other variable?
The SPSite and the SPWeb object are created as managed property.  So ideally they should be disposed properly. However, they internally use large amount of unmanaged data. This will not get disposed properly. In general, any object that uses the IDisposable object in MOSS should be disposed properly.

The most common coding method used is as follows
using(SPSite mySite = new SPSite(http://server:portno)
{
using (SPWeb myWeb=mySite.open())
{
//My code
}
}

Word of Caution:
1.       Do not use this method when getting hold of the context web (SPControl.GetContextWeb(HttpContext.Current) because this will refer to current context and when the code moves out of scope, it will try to close the current context object.
2.       Another major pit fall is the below code
       using (SPWeb web = new SPSite(SPContext.Current.Web.Url).OpenWeb())

this will dispose the web object created. But the new spsite object will not be disposed and will take the precious memory.

BOOST Performance of the Code
1.       SPWeb.Lists(“MyList”)
This is a expensive method. It will load all the list metadata un the spweb and then string compares all the title and retrieve the required list.
Instead use SPWeb object to retrieve list using the url of the list
Web.getList(“http://site/list/allitem.aspx”);
This will make considerable resource saving when there is a large number of lists in the web object
2.       Iterating the list item
Another common performance drainer that I have come across.
For(id=0; item in list;id++)
{
Variable = list.items[id].fieldname;
}
This will create a item collection each time it loops. Imagine the number of collection it needs to create for a large list!! Simply initializing the item collection outside the loop will gain a lot of performance in this case.
3.       Retrieving the list item count
List.Items.Count method will retrieve all the items in the list and then count it. A huge list means huge resource wastage. Instead of this method, use list.ItemsCount method. ItemsCount is part of the list metadata. That means that you already have this information in memory and don’t have to burn any more cpu cycles to get this.
4.       Retrieving list items using itemQuery
Query is a better way to retrieve the list items than simply opening the list item object. This can be further optimized by setting item limit to the retrieval. Smaller the batches that is retrieved, lesser the load on machine.
5.       Getting list item by GUID
SPList.items[GUID / Int32] or SPList.Items.GetItembyID(Int32)
This will retrieve all the items in the list to the memory and then search for the required item. Instead use the below syntax
SPList.GetItemByUniqueID(GUID)/SPList.GetItemByID(Int32)  

No comments:

Post a Comment