0
Memory Management in SharePoint 2007 Development Matt Vignau Liquid Hub, Inc www.liquidhub.com [email_address]
About me <ul><li>Matt Vignau
Working with SharePoint since SP 2003
Work for Liquid Hub
User Group speaker </li></ul>
Overview <ul><li>In C# .NET development we have the Garbage Collector
No more destructors
SPSite and SPWeb not always disposed
Leaves growing used memory block
Leads to problems </li></ul>
Memory Usage <ul><li>Available blocks of space
Application Pool limitations
64 bit vs 32 bit </li></ul>
Restaurant Analogy <ul><li>Space in memory is reserved, then committed
Memory is reserved in chunks – like number of diners
Memory wants to “sit together” </li></ul>
Memory Leaks <ul><li>Build with each use of the code
SPSite and SPWeb sites have ~2kB size wrappers
Object sizes are closer to 1-2 MB
One site with 15 webs accessed twice has used around 30 MB of system memory; for *one* user
Can bring down entire farms
Running in Debug mode
/3 GB switch – Doesn't work in SharePoint! </li></ul>
Example 1 public void GetNavigationInfo() {     SPWeb OurWeb = http://intranet.litwareinc.com;     foreach( SPWeb OurWeb  ...
Memory Danger Signs <ul><li>Does the application pool refresh frequently when under load? </li><ul><li>Memory reset thresh...
Upcoming SlideShare
Loading in...5
×

Matthew Vignau: Memory Management in SharePoint 2007 Development

816

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
816
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • One of the remarkable features of .NET development has been the advent of the garbage collector and automatic management of the memory used by programs.  This is a tremendous time saver as the developer no longer has to create the de-constructors that were traditionally written in languages such as C++.  While this feature is a powerful new tool in the developer’s arsenal, it can also prove to be a dangerous flaw, as numerous programmers have found when working with SharePoint 2007.
  • The above code is now threadsafe and caches to a DataTable instead of using an SPWeb object
  • Transcript of "Matthew Vignau: Memory Management in SharePoint 2007 Development"

    1. 1. Memory Management in SharePoint 2007 Development Matt Vignau Liquid Hub, Inc www.liquidhub.com [email_address]
    2. 2. About me <ul><li>Matt Vignau
    3. 3. Working with SharePoint since SP 2003
    4. 4. Work for Liquid Hub
    5. 5. User Group speaker </li></ul>
    6. 6. Overview <ul><li>In C# .NET development we have the Garbage Collector
    7. 7. No more destructors
    8. 8. SPSite and SPWeb not always disposed
    9. 9. Leaves growing used memory block
    10. 10. Leads to problems </li></ul>
    11. 11. Memory Usage <ul><li>Available blocks of space
    12. 12. Application Pool limitations
    13. 13. 64 bit vs 32 bit </li></ul>
    14. 14. Restaurant Analogy <ul><li>Space in memory is reserved, then committed
    15. 15. Memory is reserved in chunks – like number of diners
    16. 16. Memory wants to “sit together” </li></ul>
    17. 17. Memory Leaks <ul><li>Build with each use of the code
    18. 18. SPSite and SPWeb sites have ~2kB size wrappers
    19. 19. Object sizes are closer to 1-2 MB
    20. 20. One site with 15 webs accessed twice has used around 30 MB of system memory; for *one* user
    21. 21. Can bring down entire farms
    22. 22. Running in Debug mode
    23. 23. /3 GB switch – Doesn't work in SharePoint! </li></ul>
    24. 24. Example 1 public void GetNavigationInfo() {    SPWeb OurWeb = http://intranet.litwareinc.com;    foreach( SPWeb OurWeb in OurWeb .GetSubWebsForCurrentUser())    {       //Our Subsite code here     } }
    25. 25. Memory Danger Signs <ul><li>Does the application pool refresh frequently when under load? </li><ul><li>Memory reset threshold 800 megs -1.5 gigs </li></ul><li>Does the system perform poorly under heavy loads?
    26. 26. Does the entire system crash or users receive “Page not available”?
    27. 27. Does the system use custom webparts or third party webparts?
    28. 28. Are you running multiple applications from the same app pool?
    29. 29. Does the ULS log state “Potentially excessive number of SPRequest threads currently unreleased”?
    30. 30. Was any compiled code released in debug mode? </li></ul>
    31. 31. Example 2 public void GetNavigationInfo() {        SPWeb OurWeb = http://intranet.litwareinc.com;        foreach( SPWeb OurWeb in OurWeb.GetSubWebsForCurrentUser())        {            //Our Subsite code here           OurWeb.Dispose();         } }
    32. 32. Example 3 pt 1 try {      SPSite OurSiteObject;      SPWeb OurObject;     //our main code body here }
    33. 33. Example 3 pt 2 catch {      //Our exception handling code }
    34. 34. Example 3 pt 3 finally {      if(OurObject != NULL)      {                 OurObject.Dispose();      }      if(OurSiteObject != NULL)      {                 OurSiteObject.Dispose();      } }
    35. 35. Breakdown <ul><li>Try-catch block allows for exception handling
    36. 36. Finally block executes the dispose after the code block is complete to avoid problems
    37. 37. No explicit calls to dispose necessary for SPContext initializations* </li></ul>
    38. 38. Using statements <ul><li>One of the most efficient means
    39. 39. Single block usage with automatic disposal
    40. 40. No additional call out to the dispose method is needed
    41. 41. Cannot use method outbound transfer of object beyond the using block </li></ul>
    42. 42. Example 4 <ul><li>using (SPWeb OurWeb = null){
    43. 43.                 //Our Code Here;
    44. 44. } </li></ul>
    45. 45. Caching <ul><li>Saves on memory
    46. 46. Improves access time
    47. 47. Is not thread-safe </li><ul><li>IIS is multi-threaded </li></ul><li>Best for single-user applications </li></ul>
    48. 48. Example 5 <ul><li>public void CacheData()
    49. 49. {
    50. 50.    SPListItemCollection oListItems;
    51. 51.    oListItems = (SPListItemCollection)Cache[&quot;ListItemCacheName&quot;];
    52. 52.    if(oListItems == null)
    53. 53.    {
    54. 54.       oListItems = DoQueryToReturnItems();
    55. 55.       Cache.Add(&quot;ListItemCacheName&quot;, oListItems, ..);
    56. 56.    }
    57. 57. } </li></ul>
    58. 58. Example 6 <ul><li>public void CacheData()
    59. 59. {
    60. 60.    DataTable oDataTable;
    61. 61.    SPListItemCollection oListItems;
    62. 62.    lock(this)
    63. 63.    {
    64. 64.       oDataTable = (DataTable)Cache[&quot;ListItemCacheName&quot;];
    65. 65.       if(oDataTable == null)
    66. 66.       {
    67. 67.          oListItems = DoQueryToReturnItems();
    68. 68.          oDataTable = oListItems.GetDataTable();
    69. 69.          Cache.Add(&quot;ListItemCacheName&quot;, oDataTable, ..);
    70. 70.       }
    71. 71.    } </li></ul>
    72. 72. Scalability <ul><li>Target scale can impact performance considerations
    73. 73. Even with proper dispose, memory can grow
    74. 74. Projected concurrent users are a factor in your design
    75. 75. Size of farm employed to host </li></ul>
    76. 76. Scale Questions Pt 1 <ul><li>Is the data static, somewhat static (occasionally changes), or dynamic (often changes)?
    77. 77. Is the data the same for all users or does it change? (Dependent on user account, department within the company, etc)
    78. 78. Is the data easily accessible or does it require a long time to return the data? </li></ul>
    79. 79. Scale Questions Pt 2 <ul><li>Is the data public or does it require a higher level of security?
    80. 80. What is the size of the data?
    81. 81. Is the SharePoint site on a single server or on a server farm? </li></ul>
    82. 82. Links <ul><li>MSDN Best practices : http://msdn2.microsoft.com/en-us/library/aa973248.aspx#
    83. 83. Memory Pressure in MOSS: http://blogs.technet.com/stefan_gossner/archive/2007/11/26/dealing-with-memory-pressure-problems-in-moss-wss.aspx
    84. 84. Memory leak checking tool : http://blogs.msdn.com/b/rogerla/archive/2009/01/29/automate-sharepoint-dispose-code-reviews-with-spdisposecheck.aspx
    85. 85. Memory Explained; The restaurant analogy: http://blogs.msdn.com/b/tess/archive/2006/09/06/net-memory-usage-a-restaurant-analogy.aspx </li></ul>
    86. 88. Questions? <ul><li>Ask away! </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×