Tuesday, August 20, 2013

Why Dispose SharePoint Object ?

We usually don't take care much about disposing the objects explicitly in Asp.net application , as it is taken care by the Garbage Collector itself.As SharePoint itself is built on top of the .net Platform , then why it is always recommended to dispose the SharePoint Object !

In true sense most of the SharePoint Foundation 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 the unusual behaviors specified below :

  • High memory use for Internet Information Services (IIS) worker processes
  • Poor system and application performance 
  • Frequent recycles of the SharePoint Foundation application pool, especially during peak usage
  • Application crashes that appear as heap corruption in the debugger
Calling applications that work with IDisposable objects in SharePoint Foundation must dispose the objects when the applications finish using them. We should not rely on the garbage collector to release them from memory automatically.