Methods dispose() and finalize() are the methods of C# which are invoked to free the unmanaged resources held by an object. The dispose() method is defined inside the interface IDisposable whereas, the method finalize() is defined inside the class object. The main difference between dispose() and finalize() is that the method dispose() has to be explicitly invoked by the user whereas, the method finalize() is invoked by the garbage collector, just before the object is destroyed.
Let us study some other differences between them with the help of a comparison chart shown below.
Content: dispose() Vs finalize() in C#
Comparison Chart
Basis for Comparison | dispose( ) | finalize( ) |
---|---|---|
Defined | The method dispose( ) is defined in the interface IDisposable interface. | The method finalize( ) id defined in java.lang.object class. |
Syntax | public void Dispose( ){ // Dispose code here } | protected void finalize( ){ // finalization code here } |
Invoked | The method dispose( ) is invoked by the user. | The method finalize( ) is invoked by the garbage collector. |
Purpose | Method dispose( ) is used to free unmanaged resources whenever it is invoked. | Method finalize( ) is used to free unmanaged resources before the object is destroyed. |
Implementation | The method dispose( ) is to be implemented whenever there is a close( ) method. | The method finalize( ) is to be implemented for unmanaged resources. |
Access specifier | The method dispose( ) is declared as public. | The method finalize( ) is declared as private. |
Action | The method dispose( ) is faster and instantly disposes an object. | The method finalize is slower as compared to dispose |
Performance | The method disposes( ) performs the instantaneous action hence, does not effect the performance of websites. | The method finalize( ) being slower affects the performance of the websites. |
Definition of dispose()
The dispose() method releases the unmanaged resources that are held by an object of the class. The unmanaged resources are files, data connections, etc. The method dispose() is declared in the interface IDisposeable and it is implemented by the class by implementing the interface IDisposable. This method is not called automatically. The programmer has to implement it manually when you are creating a custom class that will be used by others.
The method has the following syntax:
public void dispose( ){ // Dispose code here }
In the above syntax, you can observe that the method is declared as public. It is because this method is defined in the interface IDisposable and it has to be implemented by the class that implements this interface. So, to provide accessibility to the implementing class, the method is declared as public.
This method is invoked manually by the code of a program as it is implemented to invoke. The methods performance is fast, and it instantly frees the resources held by the object of a class.
Definition of finalize()
The finalize() method is defined in the object class. It is used for cleanup activities. This method is called by the garbage collector when the reference of an object is not used for a long time. Garbage collector frees the managed resources automatically but if you want to free the unmanaged resources like file handle, data connection, etc., the finalize method has to be implemented manually. The garbage collector invokes the method finalize() just before it destroys the object completely.
The syntax of the method finalize():
protected void finalize( ){ // finalization code here }
In the syntax above, the method finalize() is declared as protected. The reason behind this is, the method finalize() must not be accessible from outside the class, and it must only be accessible to the garbage collector.
The finalize() method affects the cost of the performance as it does not free the memory instantly. In C# the finalize method is called automatically with destructors.
Key Differences Between dispose() and finalize()
- The method dispose() is defined in an interface IDisposable. On the other hand, the method finalize() is defined in the class object.
- The method dispose() has to be manually invoked inside the code by a programmer, while the method finalize is automatically invoked by the garbage collector before it destroys the object.
- The method dispose could be invoked anytime, whereas the method finalize is invoked by the garbage collector when it finds that that object has not been referenced for a long time.
- The method dispose() is implemented in a class after implementing the interface IDisposable. The method finalize() has to be implemented only for unmanaged resources because the managed resources are automatically freed by the garbage collector.
- The access specifier of the method dispose() is public as it is defined in the interface IDisposable and it would be implemented by the class that implements this interface hence, it should be public. On the other hand, the method finalize() has protected access specifier so that it should not be accessible to any member outside the class.
- The method dispose() is fast and frees the object instantly hence, it does not affects the performance cost. The method finalize() is slower and does not free the resources held by the object instantly.
Conclusion
It is suggested to use method dispose() over the method finalize() as it is faster than finalize. Also, it could be called any time, when needed.
Sani says
Good and to the point. Thanks.
Shivaji Kakad says
Thanks for detailed info.
Amol Lendave says
The method finalize( ) id defined in java.lang.object class. ????
Is this Article for C# or for Java?