Finalize and DISPOSE in C#

What is the significance of Finalize method in .NET?

.NET Garbage collector does almost all clean up activity for your objects. But unmanaged resources (example: Windows API created objects, File, Database connection objects, COM objects, etc.) are outside the scope of .NET Framework. We have to explicitly clean our resources. For these types of objects, .NET Framework providesObject.Finalize method, which can be overridden and clean up code for unmanaged resources can be put in this section?

Why is it preferred to not use finalize for clean up?

The problem with finalize is that garbage collection has to make two rounds in order to remove objects which have finalize methods.

The below figure will make things clear regarding the two rounds of garbage collection rounds performed for the objects having finalized methods.

Note: Few of the contents have been taken from various blogs/articles.

In this scenario, there are three objects, Object1Object2, and Object3Object2 has the finalize method overridden and remaining objects do not have the finalize method overridden.

Now when garbage collector runs for the first time, it searches for objects whose memory has to free. He can see three objects but only cleans the memory for Object1 and Object3Object2 it pushes to the finalization queue.

Now garbage collector runs for the second time. He sees 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, object2 was released from memory in the second round and not first. That is why the best practice is not to write clean up Non.NET resources in Finalize method rather use the DISPOSE.

What is the use of DISPOSE method?

Dispose method belongs to ‘IDisposable’ interface. We had seen in the previous section how bad it can be to override the finalize method for writing the cleaning of unmanaged resources. So if any object wants to release its unmanaged code, the best is to implement IDisposable and override the Dispose method of IDisposableinterface. Now once your class has exposed the Dispose method, it is the responsibility of the client to call theDispose method to do the cleanup. How do I force the Dispose method to be called automatically, as clients can forget to call Dispose method?

Call the Dispose method in Finalize method and in Dispose method, suppress the finalize method usingGC.SuppressFinalize. Below is the sample code of the pattern. This is the best way we do clean our unallocated resources and yes not to forget we do not get the hit of running the Garbage collector twice.

 Collapse | Copy Code
public class CleanClass : IDisposable
        public void Dispose()
        protected override void Finalize()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s