Specializations

Tuesday, December 4, 2012

What is the difference between Finalize() and Dispose()

What is the difference between Finalize() and Dispose()
Finalize () is called by Garbage Collector implicitly to free unmanaged resources. The garbage collector calls this method at some point after there are no longer valid references to the object. There are some resources like windows handles, database connections which cannot be collected by the garbage collector. Therefore the programmer needs to call Dispose() method of IDisposable interface.
Dispose () of IDisposable interface is called by the programmer to explicitly release resources when they are no longer being used. Dispose () can be called even if other references to the object are alive.


1>CLR uses the Dispose and Finalize methods for performing garbage collection of runtime objects of .Net applications.

2>CLR has a Garbage Collector(GC) which periodically checks for unused and unreferenced objects in Heap.It call Finalize() method to free the memory used by such objects.

3>Dispose is another method which is invoked by Garbage Collector to release the memory occupied by an object.Dispose method needs to be explicitly called in code for removing an object from Heap.

4>Dispose method can be invoked only by the classes that IDisposable interface.
 

No comments:

Post a Comment