Thursday, August 5, 2010

Memory Leaks in Sharepoint

Hi Folks,

I faced quite a few issues while writing the webparts and other code snippets and found memory leaks while we were using Object Model for sharepoint programming.

Please cosider the below scenario:

Scenario 1:
----------------------------------------------------------------------------------------------
SPSite site = new SPSite("site URL");
SPWeb web = site.OpenWeb();
.....................
................

Note: Here i am not disposing any objects

But I need to do that, so rather than disposing explicitly, we could follow the below approach

***************************
using(SPSite site = new SPSite("site URL"))
{
using(SPWeb web = site.OpenWeb())
{
.................
...............
}
}
**************************

otherwise we could write the same program as below:

***********************************
String str;

SPSite oSPSite = null;
SPWeb oSPWeb = null;
try
{
oSPSite = new SPSite("http://server");
oSPWeb = oSPSite.OpenWeb(..);
str = oSPWeb.Title;
}
catch(Exception e)
{
//Handle exception, log exception, etc.
}
finally
{
if (oSPWeb != null)
oSPWeb.Dispose();
if (oSPSite != null)
oSPSite.Dispose();
}
*********************************

This has solved many of the memory leak issues which I was facing.




Scenario 2:
----------------------------------------------------------------------------------------------
This is a case whenever we access the listCollection.Items.Count/web.Lists.Count several times.

Consider the below scenario:
***********************************
for(int i=0;i<listCollection.Items.Count;i++)
{
...............................
...........................
}
***********************************
Observation: for each and every iteration it gets the Item count in the list and checks with the value of variable "i", which would some times cause performance issues when the list size increases.
Resolution:
**********************************
int itemCount = listCollection.Items.Count;
for(int i=0;iitemCount;i++)
{
...............................
...........................
}
**********************************
Observation:First we need to assign the item count to some variable to avoid multiple times calculation.


Scenario 3:
------------------------------------------------------------------------------------------------------------------
We basically require only few items to be returned while querying i.e., while we are using SPQuery object I require only only few rows from the list to be retrieved.

Consider the below scenario:
I require the top 3 rows in a list
********************************
SPQuery query = new SPQuery();
query.Query("actual query copied from CAML Builder");
list.getItems(query);
********************************
Observation: This would return all the items in the list irrespective of what i needed and even in the CAML query there is no possibility for limiting the rows.

That we could do using the SPQuery object as below:
Resolution:
********************************
SPQuery query = new SPQuery();
query.Query("actual query copied from CAML Builder");
query.RowLimit(3);
list.getItems(query);
********************************
Observation: using RowLimit I would be able to get only limited rows what I was intended to retrieve.

Scenario 4:
----------------------------------------------------------------------------------------------
We require only few columns while retrieveing the list items using the SPQuery object.

Consider the below scenario:
********************************
SPQuery query = new SPQuery();
query.Query("actual query copied from CAML Builder");
list.getItems(query);
********************************
Observation: This would return all the columns in the list irrespective of what are all the columns which we require.
Resolution:
********************************
SPQuery query = new SPQuery();
query.Query("actual query copied from CAML Builder");
query.ViewFields = "";
list.getItems(query);
********************************
Observation:This would return the required columns and we wouldnt face any performance issues.


Please feel free to addon your comments if any.
Any clarification please post me back

No comments:

Post a Comment