Dispose Vs Finalizer

The Dispose and Finalizer are methods used to clear the resources used for the object.
The Dispose is a method of IDisposable interface. we have to inherit this interface for our object and implement the method Dispose, to clear our objects used in our application. suppose if we use collection and dictionary or any other huge resource for a specific object, we have to clear all the memory used by the object when it is destroyed. to make this happen internallly for every object we have to implement Dispose method and it takes care of the clearing the memory.
The Finalizer method is also similar type but this will be called when GC gets invoked. this GC.Collect call will invoke Finalizer method for each and every object. this will do a emergency clean, it may lead to data loss some times.

we can also implement finalizer method as a special case. the call of finalizer method after a dipose is a waste of process time, we can bypass this action by setting the property SupressFinalizer to true. this will help to avoid calling the finalizer method when GC gets activated.

No comments:

Post a Comment