200 likes | 339 Views
Matt Vignau RJB Technical Consulting www.rjbtech.com matt.vignau@rjbtech.com. Memory Management in SharePoint 2007 Development. Overview. In C# .NET development we have the Garbage Collector No more destructors SPSite and SPWeb not always disposed Leaves growing used memory block
E N D
Matt Vignau RJB Technical Consulting www.rjbtech.com matt.vignau@rjbtech.com Memory Management in SharePoint 2007 Development
Overview • 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
Memory Leaks • 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
Example 1 • public void GetNavigationInfo() • { • SPWebOurWeb = http://intranet.litwareinc.com; • foreach(SPWebOurWebin OurWeb.GetSubWebsForCurrentUser()) • { • //Our Subsite code here • } • }
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? • Does the entire system crash or users receive “Page not available”? • Does the system use custom webparts or third party webparts?
Example 2 • public void GetNavigationInfo() • { • SPWebOurWeb= http://intranet.litwareinc.com; • foreach(SPWebOurWebin OurWeb.GetSubWebsForCurrentUser()) • { • //Our Subsite code here • OurWeb.Dispose(); • } • }
Example 3 pt 1 • try • { • SPSiteOurSiteObject; • SPWebOurObject; • //our main code body here • }
Example 3 pt 2 • catch • { • //Our exception handling code • }
Example 3 pt 3 • finally • { • if(OurObject != NULL) • { • OurObject.Dispose(); • } • if(OurSiteObject != NULL) • { • OurSiteObject.Dispose(); • } • }
Breakdown • Try-catch block allows for exception handling • Finally block executes the dispose after the code block is complete to avoid problems • No explicit calls to dispose necessary for SPContext initializations
Using statements • One of the most efficient means • Single block usage with automatic disposal • No additional call out to the dispose method is needed • Cannot use method outbound transfer of object beyond the using block
Example 4 • using (SPWebOurWeb = null){ • //Our Code Here; • }
Caching • Saves on memory • Improves access time • Is not thread-safe • IIS is multi-threaded • Best for single-user applications
Example 5 • public void CacheData() • { • SPListItemCollectionoListItems; • oListItems = (SPListItemCollection)Cache["ListItemCacheName"]; • if(oListItems == null) • { • oListItems = DoQueryToReturnItems(); • Cache.Add("ListItemCacheName", oListItems, ..); • } • }
Example 6 • public void CacheData() • { • DataTableoDataTable; • SPListItemCollectionoListItems; • lock(this) • { • oDataTable = (DataTable)Cache["ListItemCacheName"]; • if(oDataTable == null) • { • oListItems = DoQueryToReturnItems(); • oDataTable = oListItems.GetDataTable(); • Cache.Add("ListItemCacheName", oDataTable, ..); • } • }
Scalability • Target scale can impact performance considerations • Even with proper dispose, memory can grow • Projected concurrent users are a factor in your design
Scale Questions Pt 1 • Is the data static, somewhat static (occasionally changes), or dynamic (often changes)? • Is the data the same for all users or does it change? (Dependent on user account, department within the company, etc) • Is the data easily accessible or does it require a long time to return the data?
Scale Questions Pt 2 • Is the data public or does it require a higher level of security? • What is the size of the data? • Is the SharePoint site on a single server or on a server farm?
Links • My Blog: http://blogs.rjbtech.com/MV/ • MSDN Best practices: http://msdn2.microsoft.com/en-us/library/aa973248.aspx# • Memory Pressure in MOSS: http://blogs.technet.com/stefan_gossner/archive/2007/11/26/dealing-with-memory-pressure-problems-in-moss-wss.aspx • Memory leak checking tool: http://blogs.rjbtech.com/RJB
Questions? • Ask away!