matthew vignau: memory management in sharepoint 2007 development

Download Matthew Vignau: Memory Management in SharePoint 2007 Development

If you can't read please download the document

Upload: sharepoint-saturday-ny

Post on 14-Jun-2015

810 views

Category:

Technology


4 download

TRANSCRIPT

  • 1. Memory Management in SharePoint 2007 Development Matt Vignau Liquid Hub, Inc www.liquidhub.com [email_address]

2. About me

  • Matt Vignau

3. Working with SharePoint since SP 2003 4. Work for Liquid Hub 5. User Group speaker 6. Overview

  • In C# .NET development we have the Garbage Collector

7. No more destructors 8. SPSite and SPWeb not always disposed 9. Leaves growing used memory block 10. Leads to problems 11. Memory Usage

  • Available blocks of space

12. Application Pool limitations 13. 64 bit vs 32 bit 14. Restaurant Analogy

  • Space in memory is reserved, then committed

15. Memory is reserved in chunks like number of diners 16. Memory wants to sit together 17. Memory Leaks

  • Build with each use of the code

18. SPSite and SPWeb sites have ~2kB size wrappers 19. Object sizes are closer to 1-2 MB 20. One site with 15 webs accessed twice has used around 30 MB of system memory; for *one* user 21. Can bring down entire farms 22. Running in Debug mode 23. /3 GB switch Doesn't work in SharePoint! 24. Example 1 public void GetNavigationInfo() { SPWeb OurWeb = http://intranet.litwareinc.com; foreach( SPWeb OurWebinOurWeb .GetSubWebsForCurrentUser()) { //Our Subsite code here} } 25. Memory Danger Signs

  • Does the application pool refresh frequently when under load?
  • Memory reset threshold 800 megs -1.5 gigs

Does the system perform poorly under heavy loads? 26. Does the entire system crash or users receive Page not available? 27. Does the system use custom webparts or third party webparts? 28. Are you running multiple applications from the same app pool? 29. Does the ULS log state Potentially excessive number of SPRequest threads currently unreleased? 30. Was any compiled code released in debug mode? 31. Example 2 public void GetNavigationInfo() { SPWeb OurWeb= http://intranet.litwareinc.com; foreach( SPWeb OurWebin OurWeb.GetSubWebsForCurrentUser()) { //Our Subsite code here OurWeb.Dispose(); } } 32. Example 3 pt 1 try { SPSite OurSiteObject; SPWeb OurObject; //our main code body here } 33. Example 3 pt 2 catch { //Our exception handling code } 34. Example 3 pt 3 finally { if(OurObject != NULL) { OurObject.Dispose(); } if(OurSiteObject != NULL) { OurSiteObject.Dispose(); } } 35. Breakdown

  • Try-catch block allows for exception handling

36. Finally block executes the dispose after the code block is complete to avoid problems 37. No explicit calls to dispose necessary for SPContext initializations* 38. Using statements

  • One of the most efficient means

39. Single block usage with automatic disposal 40. No additional call out to the dispose method is needed 41. Cannot use method outbound transfer of object beyond the using block 42. Example 4

  • using (SPWeb OurWeb = null){

43. //Our Code Here; 44. } 45. Caching

  • Saves on memory

46. Improves access time 47. Is not thread-safe

  • IIS is multi-threaded

Best for single-user applications 48. Example 5

  • public void CacheData()

49. { 50. SPListItemCollection oListItems; 51. oListItems = (SPListItemCollection)Cache["ListItemCacheName"]; 52. if(oListItems == null) 53. { 54. oListItems = DoQueryToReturnItems(); 55. Cache.Add("ListItemCacheName", oListItems, ..); 56. } 57. } 58. Example 6

  • public void CacheData()

59. { 60. DataTable oDataTable; 61. SPListItemCollection oListItems; 62. lock(this) 63. { 64. oDataTable = (DataTable)Cache["ListItemCacheName"]; 65. if(oDataTable == null) 66. { 67. oListItems = DoQueryToReturnItems(); 68. oDataTable = oListItems.GetDataTable(); 69. Cache.Add("ListItemCacheName", oDataTable, ..); 70. } 71. } 72. Scalability

  • Target scale can impact performance considerations

73. Even with proper dispose, memory can grow 74. Projected concurrent users are a factor in your design 75. Size of farm employed to host 76. Scale Questions Pt 1

  • Is the data static, somewhat static (occasionally changes), or dynamic (often changes)?

77. Is the data the same for all users or does it change?(Dependent on user account, department within the company, etc) 78. Is the data easily accessible or does it require a long time to return the data? 79. Scale Questions Pt 2

  • Is the data public or does it require a higher level of security?

80. What is the size of the data? 81. Is the SharePoint site on a single server or on a server farm? 82. Links

  • MSDN Best practices : http://msdn2.microsoft.com/en-us/library/aa973248.aspx#

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. Memory leak checking tool :http://blogs.msdn.com/b/rogerla/archive/2009/01/29/automate-sharepoint-dispose-code-reviews-with-spdisposecheck.aspx 85. Memory Explained; The restaurant analogy:http://blogs.msdn.com/b/tess/archive/2006/09/06/net-memory-usage-a-restaurant-analogy.aspx 86. 87. 88. Questions?

  • Ask away!