Wednesday, 6 September 2017

Difference Between Finalize and Dispose Method

.NET Framework provides two methods Finalize and Dispose for releasing unmanaged resources like files, database connections, COM etc. This article helps you to understand the difference between Finalize and Dispose method.
Difference between Dispose & Finalize Method
Dispose
Finalize
It is used to free unmanaged resources like files, database connections etc. at any time.
It can be used to free unmanaged resources (when you implement it) like files, database connections etc. held by an object before that object is destroyed.
Explicitly, it is called by user code and the class which is implementing dispose method, must has to implement IDisposable interface.
Internally, it is called by Garbage Collector and cannot be called by user code.
It belongs to IDisposable interface.
It belongs to Object class.
It's implemented by implementing IDisposable interface Dispose() method.
It's implemented with the help of destructor in C++ & C#.
There is no performance costs associated with Dispose method.
There is performance costs associated with Finalize method since it doesn't clean the memory immediately and called by GC automatically.

Example for implementing the dispose method

  1. public class MyClass : IDisposable
  2. {
  3. private bool disposed = false;
  4. //Implement IDisposable.
  5. public void Dispose()
  6. {
  7. Dispose(true);
  8. GC.SuppressFinalize(this);
  9. }
  10.  
  11. protected virtual void Dispose(bool disposing)
  12. {
  13. if (!disposed)
  14. {
  15. if (disposing)
  16. {
  17. // TO DO: clean up managed objects
  18. }
  19. // TO DO: clean up unmanaged objects
  20.  
  21. disposed = true;
  22. }
  23. }
  24. }

Example for implementing Finalize method

If you want to implement Finalize method, it is recommended to use Finalize and Dispose method together as shown below:
  1. // Using Dispose and Finalize method together
  2. public class MyClass : IDisposable
  3. {
  4. private bool disposed = false;
  5. //Implement IDisposable.
  6. public void Dispose()
  7. {
  8. Dispose(true);
  9. GC.SuppressFinalize(this);
  10. }
  11.  
  12. protected virtual void Dispose(bool disposing)
  13. {
  14. if (!disposed)
  15. {
  16. if (disposing)
  17. {
  18. // TO DO: clean up managed objects
  19. }
  20. // TO DO: clean up unmanaged objects
  21. disposed = true;
  22. }
  23. }
  24. //At runtime C# destructor is automatically Converted to Finalize method
  25. ~MyClass()
  26. {
  27. Dispose(false);
  28. }
  29. }

Note

  1. It is always recommended to use Dispose method to clean unmanaged resources. You should not implement the Finalize method until it is extremely necessary.
  2. At runtime C#, C++ destructors are automatically Converted to Finalize method. But in VB.NET you need to override Finalize method, since it does not support destructor.
  3. You should not implement a Finalize method for managed objects, because the garbage collector cleans up managed resources automatically.
  4. A Dispose method should call the GC.SuppressFinalize() method for the object of a class which has destructor because it 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.

No comments:

Post a Comment