How to spot the problem? |
|
1st Approach |
If your sites are displaying any of the unusual behaviors described previously, you can determine whether the cause is a memory leak due to incorrectly disposed objects by checking the ULS logs (available at C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\LOGS) for entries related to the SPRequest object.Each instance of SPSite and SPWeb contains a reference to an SPRequest object that,in turn, contains a reference to an unmanaged COM object that handles communications with the database server. Windows SharePoint Services monitors the number of SPRequest objects that exist in each specific thread and in parallel threads, and adds useful entries to the logs. For more information on this click here: |
2nd Approach: |
Microsoft provides a tool to help you detect and track down objects you aren’t disposing of properly called the SharePoint Dispose Checker tool. That tool is found here: http://code.msdn.microsoft.com/SPDisposeCheck |
Coding Techniques to Ensure Object Disposal |
You can employ certain coding techniques to ensure object disposal. These techniques include using the following in your code:
|
Dispose method |
The basic idea behind using the Dispose method is you call it on an IDisposable object when you are done with it. At the point you call Dispose on the object the managed and unmanaged memory associated with the object is reclaimed. The object is also no longer usable after you call Dispose on it—any subsequent calls to the object will result in an error. Example SPSite mySite = new SPSite("http://mySite"); //do something with mysite mySite.Dispose(); //disposes of memory used by mysite //now don’t use mySite object as it will throw error |
The using Clause |
You can automatically dispose of SharePoint objects that implement the IDisposable interface by using the Microsoft Visual C# using statement. Example String str; using(SPSite oSPsite = new SPSite("http://Myserver")) { using(SPWeb oSPWeb = oSPSite.OpenWeb()) { str = oSPWeb.Title; str = oSPWeb.Url; } } It also makes your code more clear and makes it impossible for you to accidentally call into the object after it has been disposed because it also goes out of scope when you are done with it. |
The try, catch, and finally Blocks |
Using try, catch, and finally blocks obviously makes sense whenever you need to handle exceptions. Any code within a try/catch block should have a governing finally clause, which ensures that the objects that implement IDisposable are disposed. There are cases in your code where you may be creating objects that are IDisposable that may not be as obvious. For example, you may have a foreach loop that iterates over a collection that returns SPSite or SPWeb objects. For these cases you use a try, finally block in the iterator to ensure that each IDisposable object created in the foreach loop is disposed of properly. Example using (SPSite spSite = new SPSite('http://mysite/')) { foreach (SPWeb spweb in spSite.AllWebs) { try { Console.WriteLine(spweb.Name); } finally { if (spweb != null) { spweb.Dispose(); } } } } |
Friday, October 28, 2011
The Disposable Pattern in SharePoint Development
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment