Thursday, February 17, 2011

ביצועים - זכרת לשחרר זיכרון ?

אני תמיד נוהג לציין בפני תוכניתני שרפוינט חדשים שעליהם לשחרר אוביקטיים מסויימים ב-API,
לאחר שהורגלו ש"אוסף הזבל" יעשה זאת בשבילם.

מה הבעיה בדיוק ?
חלק מהאובייקטים בשרפוינט עובדים על גבי קוד לא מנוהל, קרי יש לשחרר את הזכרון שהתם תפסו בסוף השימוש.
מי הם ?
SPSite, SPWeb , SharePoint WebService objects.
כל פעם שיש לכם שימוש בהם, זה צריך להדליק לכם תזכורת שכנראה תצטרכו לנקות אחריהם...
גם אם הם מגיעים בוארציות שונות( אך לא תמיד ).

מה יקרה אם לא נעשה זאת ?
1. נצרוך יותר זיכרון בשרת , קרי בעיות ביצועים, שגיאות בלתי צפויות.
2. אתחול רכיבי הזכרון של אפליקצית האירוח של השרפוינט, דבר שיוביל לטעינה מחדש של אתרים בצורה איטית ואף אי זמינות של האפליקציה.

אז זה חשוב, איך עושים את זה ?
כלל האוביקטים הללו יורשים מ
IDisposable
אז נוכל לשחרר את הזכרון בעצמנו.

יש לכך שלוש גישות -

1. שחרור זכרון באופן "ידני"
 
SPSite site = new SPSite("http://mysite");
//do some work
site.Dispose();


2. שימוש ב try ו catch
 
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 , הכל אוטומטי...
 
using(SPSite site = new SPSite("http://mysite")
{
using(SPWeb web = site.OpenWeb())
{
\\do more stuff
}
}


חשוב להכיר מתי צריך לשחרר זיכרון ומתי לא, למשל שמקבלים אוסף של אתרים, האם יש צורך לשחרר גם אותו או לא ? או למשל אתר שורש , כן או לא ?

ממליץ לקרוא את המאמר הבא כדי להבין את הנושא באופן מושלם
לינק למאמר
בהצלחה.

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

Saturday, February 12, 2011

What are the new enhancements in SharePoint Foundation?

Thought about making some order with all the new stuff we have in SharePoint 2010. I will mention in this post my top 10 enhancements:
  • Service application architecture - Now you can share resources across web application and farms. You probably remember the SSP (R.I.P) that couldn't be shared across farms - now you can share those kind of resources among several farms.
  • PowerShell - Goodbye Stsadm, Hello PowerShell. An easier way to write administrative scripts.
  • SandBox solutions - an option to deploy solution in sitecollection scope (and not WebApplication scope). Let alone that you have much more control of the resources this solution will consume.
  • New events for sites,lists and workflows - more new events we can hook up into (list creation - finally)
  • REST-based access to SharePoint list items - no more using the regular web services sharepoint expose for us. Now we can get from wherever we want data from lists using the REST-based webservices.
  • Client side programing - Good stuff comes in threes. Calling SharePoint object remotely via .net, silverlight and javascript.
  • Claim based security - external identity management. Like facaebook which enables you to authenticate to other websites with your facebook credentials, now you can implement the same in SharePoint.
  • BCS/External lists - Connecting legacy systems into SharePoint lists. make it transparent to the user interface.
  • Linq queries - now it's part of SharePoint 2010. Easier than ever.
  • Controlling query execution - prohibiting large,inefficient queries during pick hours or by any other rule you create.