Destructors Vs Dispose Vs Finalize

Destructors are special methods which contains the clean-up code for an object. A destructor cannot be explicitly called in C#. It is called implicitly by the Garbage Collector.
In VB.NET, destructors are implemented by overriding the Finalize() method of the System.Object class. However, in C# the compiler translates the destructor to a Finalize() method.

Dispose() of System.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. Dispose() is used to explicitly release resources before the Garbage Collector frees the object. It can only be implemented in classes which implement the System.IDisposable interface.
Note: A Dispose method should call the GC.SuppressFinalize method for the object it is disposing. If the object is currently on the finalization queue, SuppressFinalize prevents its Finalize method from being called. Remember that executing a Finalize method is costly to performance. If your Dispose method has already done the work to clean up the object, then it is not necessary for the garbage collector to call the object's Finalize method. GC.SupressFinalize(this): Request the system not to call finalize for the specified instance.

Using() statement defines a scope at the end of which an object will be disposed. An instance is created in a using statement to ensure that Dispose is called on the object when the using statement is exited. The object you instantiate must implement the System.IDisposable interface.

Finalize() method is called by Garbage Collector implicitly to free (perform cleanup operations) unmanaged resources held by the current object before it is destroyed. The Garbage Collector calls this method automatically after an object becomes inaccessible (unless the object has been exempted from finalization by a call to GC.SuppressFinalize). Note that Finalize() has performance issues. Finalize() can NOT be overridden or called in C#.

Finalize operations have the following limitations:
  • The Finalize() method is protected and therefore is accessible only through this class or through a derived class.
  • The exact time when the finalizer executes during garbage collection is undefined. Resources are not guaranteed to be released at any specific time, unless calling a Close method or a Dispose method.
  • The order of execution of the finializer is non-deterministic. The finalizers of two objects are not guaranteed to run in any specific order, even if one object refers to the other. That is, if Object A has a reference to Object B and both have finalizers, Object B might have already finalized when the finalizer of Object A starts.
  • The thread on which the finalizer is run is unspecified.
There are some resources like windows handles, database connections, file system, etc; which cannot be collected by the garbage collector. Therefore the programmer needs to call Dispose() method of IDisposable interface.

No comments:

Post a Comment