Why do some people use the Finalize
method over the Dispose
method?
In what situations would you use the Finalize
method over the Dispose
method and vice versa?
Why do some people use the Finalize
method over the Dispose
method?
In what situations would you use the Finalize
method over the Dispose
method and vice versa?
Finalize gets called by the GC when this object is no longer in use.
Dispose is just a normal method which the user of this class can call to release any resources.
If user forgot to call Dispose and if the class have Finalize implemented then GC will make sure it gets called.
The finalizer method is called when your object is garbage collected and you have no guarantee when this will happen (you can force it, but it will hurt performance).
The Dispose
method on the other hand is meant to be called by the code that created your class so that you can clean up and release any resources you have acquired (unmanaged data, database connections, file handles, etc) the moment the code is done with your object.
The standard practice is to implement IDisposable
and Dispose
so that you can use your object in a using
statment. Such as using(var foo = new MyObject()) { }
. And in your finalizer, you call Dispose
, just in case the calling code forgot to dispose of you.
Finalize is the backstop method, called by the garbage collector when it reclaims an object. Dispose is the "deterministic cleanup" method, called by applications to release valuable native resources (window handles, database connections, etc.) when they are no longer needed, rather than leaving them held indefinitely until the GC gets round to the object.
As the user of an object, you always use Dispose. Finalize is for the GC.
As the implementer of a class, if you hold managed resources that ought to be disposed, you implement Dispose. If you hold native resources, you implement both Dispose and Finalize, and both call a common method that releases the native resources. These idioms are typically combined through a private Dispose(bool disposing) method, which Dispose calls with true, and Finalize calls with false. This method always frees native resources, then checks the disposing parameter, and if it is true it disposes managed resources and calls GC.SuppressFinalize.
99% of the time, you should not have to worry about either. :) But, if your objects hold references to non-managed resources (window handles, file handles, for example), you need to provide a way for your managed object to release those resources. Finalize gives implicit control over releasing resources. It is called by the garbage collector. Dispose is a way to give explicit control over a release of resources and can be called directly.
There is much much more to learn about the subject of Garbage Collection, but that's a start.
Others have already covered the difference between Dispose
and finalize (btw the finalize method is still called a destructor in the language specification), so I'll just add a little about the scenarios where the finalize method comes in handy.
Some types encapsulates disposable resources in a manner where it is easy to use and dispose of them in a single action. The general usage is often like this. Open, read or write, close (Dispose). It fits very well with the using
construct.
Others are a bit more difficult. WaitEventHandles
for instances are not used like this as they are used to signal from one thread to another. The question then becomes who should call Dispose
on these? As a safeguard types like these implement a finalize method, which makes sure resources are disposed when the instance is no longer referenced by the application.
Finalize
Dispose
Dispose/Finalized Pattern
code example and additional resources are here: mgznet.com/disposedAndFinalized.aspx