Since .NET has a garbage collector why do we need finalizers/destructors/dispose-pattern?
Solution 1:
Finalizers are needed to guarantee the release of scarce resources back into the system like file handles, sockets, kernel objects, etc. Since the finalizer always runs at the end of the objects life, it’s the designated place to release those handles.
The Dispose
pattern is used to provide deterministic destruction of resources. Since the .net runtime garbage collector is non-deterministic (which means you can never be sure when the runtime will collect old objects and call their finalizer), a method was needed to ensure the deterministic release of system resources. Therefore, when you implement the Dispose
pattern properly you provide deterministic release of the resources and in cases where the consumer is careless and does not dispose the object, the finalizer will clean up the object.
A simple example of why Dispose
is needed might be a quick and dirty log method:
public void Log(string line)
{
var sw = new StreamWriter(File.Open(
"LogFile.log", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None));
sw.WriteLine(line);
// Since we don't close the stream the FileStream finalizer will do that for
// us but we don't know when that will be and until then the file is locked.
}
In the above example, the file will remain locked until the garbage collector calls the finalizer on the StreamWriter
object. This presents a problem since, in the meantime, the method might be called again to write a log, but this time it will fail because the file is still locked.
The correct way is to dispose the object when are done using it:
public void Log(string line)
{
using (var sw = new StreamWriter(File.Open(
"LogFile.log", FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))) {
sw.WriteLine(line);
}
// Since we use the using block (which conveniently calls Dispose() for us)
// the file well be closed at this point.
}
BTW, technically finalizers and destructors mean the same thing; I do prefer to call c# destructors 'finalizers' since otherwise they tend to confuse people with C++ destructors, which unlike C#, are deterministic.
Solution 2:
The previous answers are good but let me emphasize the important point here once again. In particular, you said that
If I understand correctly the .net runtime will always clean up after me.
This is only partly correct. In fact, .NET only offers automatic management for one particular resource: main memory. All other resources need manual cleanup.1)
Oddly, main memory gets special status in almost all discussions about program resources. There's of course a good reason for this – main memory is often the scarcest resource. But it's worth remembering that there are other types of resources as well, that also need managing.
1) The usual attempted solution is to couple the lifetime of other resources to the lifetime of memory locations or identifiers in the code – hence the existence of finalizers.
Solution 3:
The Garbage Collector will only run if the system is not under memory pressure, unless it really needs to free up some memory. That means, you can never be sure when the GC will run.
Now, Imagine you are a Database Connection. If you let the GC clean up after you, you may be connected to the database for much longer than needed, causing weird load situation. In that case, you want to implement IDisposable, so that the user can call Dispose() or use using() to really make sure that the connection is closed ASAP without having to rely on GC which may run much later.
Generally, IDisposable is implemented on any class that works with unmanaged resources.
Solution 4:
- There are things the garbage collector can't clean up after you
- Even with things it can cleanup, you can help it clean up sooner