By now most of you should know that there is a SharePoint Diagnostics Tool. This really is a great tool, since it scans your assemblies for memory leaks.
You should also know, that there is an occasion you can create a memory leak which the tool will not recognize.
1: using (var site = new SPSite("http://yoururl")) 2: { 3: SPWeb web = null;
4: try
5: { 6: web = site.OpenWeb();
7: // do something with this web
8: // ...
9: // done. don't need it anymore. but i need another web
10: web = site.AllWebs["webname"];
11: }
12: finally
13: { 14: if (web != null) web.Dispose();
15: }
16: }
The SPWeb from line 6 will not be disposed in line 14, because in line 10 we assign another SPWeb to the web!
So what do we do?
Just dispose the web manually, before we assign a new SPWeb to it. You can use two usings around the webs, or dispose it via web.Dispose(). That’s it.