SharePoint Object Model Memory Leaks September 26, 2006
Posted by codinglifestyle in SharePoint.Tags: dispose, memory leaks, Object Model, SharePoint
trackback
The SharePoint object model is the best way to develop complex solutions built upon a powerful architecture. It has many faults, inconsistencies (Portal vs. SPS), and security can be a bear to overcome. But never once did I think it leaked memory. Forget that you are developing in C# for a moment, because from now on you’re going to have to start cleaning up after yourself. That’s right, we’re going to program like its 1999:
"Several of the Windows SharePoint Services objects, primarily the SPSite class and SPWeb class objects, are created as managed objects. However, these objects use unmanaged code and memory to perform the majority of their work. The managed part of the object is small; the unmanaged part of the object is much larger. Because the smaller managed part of the object does not put memory pressure on the garbage collector, the garbage collector does not release the object from memory in a timely manner. The object’s use of a large amount of unmanaged memory can cause some of the unusual behaviors described earlier. Calling applications that work with IDisposable objects in Windows SharePoint Services must dispose of the objects when the applications finish using them. You should not rely on the garbage collector to release them from memory automatically."
Technically this is not a memory leak as in forgetting to delete an object in C++. However, when developing web parts IIS is likely to grow large as the webpart is used causing the application pool to recycle quickly. (This can also cause a problem in utilities or services which will grow large). In our last install the application pool, which recycles when IIS reaches around 800megs, recycled every hour losing valuable cached data. This meant the pain of caching that data again was felt every hour and was enough to force our investigation. Please read the following article carefully to realize the full extent of this issue.
http://msdn2.microsoft.com/en-us/library/ms778813.aspx
If you are a seasoned SharePoint developer I’m sure you won’t be surprised when it tell you this: its worse that you think. You will also need to destroy (Dispose) the following:
· All SPWeb/SPSite objects not obtained via GetContextWeb or GetContextSite
· SPWeb/SPSite objects used from indexers or Arrays
· Even SPWeb.RootWeb and SPWeb.ParentWeb must be destroyed if you touch it!
Note on the first point, the single object obtained from GetContextWeb/Site is fine. But any object created from the resulting SPWeb/SPSite must be disposed of.
Comments»
No comments yet — be the first.