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.
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.
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.
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. |