Best Practices : Using Disposable WSS Objects

Merci à Stéphane pour cet article !

Plusieurs objets de SharePoint peuvent être gourmands en ressources, alors il vaut mieux les détruire le plus rapidement possible. Pour ce faire, il suffit d'appeler Dispose sur les objets SPSite et SPWeb ou encore mieux, les déclarer dans un bloc using. Voici un extrait :

Why Dispose? 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 much smaller than the unmanaged part. Because the smaller managed part 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'''.

Pour lire l'article complet :
 * http://msdn.microsoft.com/en-us/library/aa973248.aspx