Rien de spécial
Le blog de Régis

Disposing objects in Sharepoint

[ironic mode]Sharepoint is a good development platform. Like in the good old days of C, you need to free objects from memory :-p[/ironic mode]

Stephan Großner explains

Each SPWeb and SPSite object holds a reference to an SPRequest object which holds a reference to a SharePoint COM object that is responsible to communicate with the backend SQL server.

Disposing a SPWeb object will not actually remove the SPWeb object from memory1 but it will call a method of the SPWeb object which causes the COM object to close the connection to the SQL server and to release its allocated memory.

After the dispose only the small managed SPWeb and SPRequest objects remain in memory which do not create a big memory overhead. They are removed by the dot net framework through garbage collection later as with any other managed object as soon as no references to the object exist any more.

If the SPWeb object is not disposed, it will eventually be freed from memory by the garbage collector, but not the COM objected created to make the connection with SQL ; hence the connection to the SQL server will stay open and there will be a memory leak 2. Microsoft has even invented a term for this kind of thing: unmanaged objects.

That’s where SPDisposeCheck tool may become handy. On a broader scope, identifying memory problems is a great article.

  1. this is done by the .net garbage collector and actually the .NET framework does not allow to remove any object from memory in a deterministic way []
  2. that’s about 2MB for the COM object created by a SPWeb []