Significance of the Finalize method
The .NET Garbage Collector does nearly all the clean up activity for your objects. But unmanaged resources (for example: Windows API created objects, File, Database connection objects, COM objects and so on) are outside the scope of the .NET Framework that we need to explicitly clean our resources. For these types of objects, the .NET Framework provides the "Object.Finalize" method, which can be overridden and to clean up the code for unmanaged resources. Can it be put in this section?
Headache with Finalize
The problem with Finalize is that garbage collection must make two rounds to remove objects that have Finalize methods.
The figure below will make things clearer regarding the two rounds of garbage collection rounds for the objects having finalized methods.

The .NET Garbage Collector does nearly all the clean up activity for your objects. But unmanaged resources (for example: Windows API created objects, File, Database connection objects, COM objects and so on) are outside the scope of the .NET Framework that we need to explicitly clean our resources. For these types of objects, the .NET Framework provides the "Object.Finalize" method, which can be overridden and to clean up the code for unmanaged resources. Can it be put in this section?
Headache with Finalize
The problem with Finalize is that garbage collection must make two rounds to remove objects that have Finalize methods.
The figure below will make things clearer regarding the two rounds of garbage collection rounds for the objects having finalized methods.
In this scenario there are three objects, Object1, Object2 and Object3. Object2 has the Finalize method overridden and the remaining objects do not have the Finalize method overridden.
Now when the Garbage Collector runs for the first time it searches for objects whose memory must be freed. He can see three objects but only cleans the memory for Object1 and Object3. Object2 is pushed to the finalization queue.
Now the Garbage Collector runs for the second time. It sees that there are no objects to be released and then checks for the finalization queue and at this moment, it clears object2 from the memory.
So if you notice that object2 was released from memory in the second round and not the first. That is why the best practice is to not write clean up of unmanaged resources in the Finalize method but instead use Dispose.
Dispose method
The Dispose method belongs to the "IDisposable" interface. We had seen in the previous section how bad it can be to override the Finalize method to do the cleanup of unmanaged resources. So if any object wants to release its unmanaged code then it is best to implement IDisposable and override the Dispose method of the IDisposable interface. Now once your class has exposed the Dispose method it is the responsibility of the client to call the Dispose method to do the cleanup.
Dispose and Finalize together - A Better Result
Call the Dispose method in the Finalize method and in the Dispose method to suppress the Finalize method from using "GC.SuppressFinalize". The following is the sample code of the pattern. This is the best way to clean our unallocated resources, and yes do not forget, we do not get the hit of running the Garbage Collector twice.
public class CleanClass : IDisposable
{
public void Dispose()
{
GC.SuppressFinalize(this);
}
protected override void Finalize()
{
Dispose();
}
}
No comments:
Post a Comment