C# Memory Management— Part 2 (Finalizer & Dispose)

Sena Kılıçarslan
C# Programming
Published in
4 min readMar 8, 2019

I am writing this post as the continuation of part one of this series. You can read the first post from the below link:

In this post, I will write about Finalizer and Dispose method and I will show how to use them with code examples.

Let’s start with why these are needed and used.

Garbage Collector handles memory management in .NET framework and conducts the allocation and reclaiming of memory for the applications (We will talk about the details of garbage collection in the third part of the series). However, when we create objects that include unmanaged resources such as windows, files, network and database connections, we must explicitly release those resources after using them in our applications. This can be done by :

· using finalizers

· implementing Dispose method from the IDisposable interface

First, let’s learn about finalizers.

What is a finalizer?

Finalizers (which are also called destructors) are used to perform any necessary final clean-up when a class instance is being collected by the garbage collector. Some important points about finalizers are:

  • A class can have only one finalizer.
  • A finalizer does not have modifiers or parameters.
  • Finalizers cannot be called explicitly, they are called by the garbage collector (GC) when the GC considers the object eligible for finalization. They are also called when the program finishes in .NET framework applications.

Following is an example of a class with a finalizer:

Finalizer overrides the Finalize method of the base class. So the call to the finalizer is implicitly translated to the following code:

We can see this by creating an example that includes an inheritance chain as follows:

Output of this code is:

As you see, finalizers are called recursively for all instances in the inheritance chain, from the most-derived to the least-derived.

Dispose Method

We mentioned above that finalizers are called by the garbage collector or when the program finishes (in .NET framework applications). This means we cannot call them. If our application uses an expensive external resource, we should then release the resource explicitly. We can do this by implementing Dispose method from IDisposable interface. By this way, we can improve the performance of our application as well. Now, let’s see this in practice.

How to Implement Dispose Method?

First, we create a class that implements IDisposable interface and then choose Implement interface with Dispose pattern from the Quick Actions menu as shown in the following image:

Then a skeleton code is generated as follows:

I changed the code to make it usable and understandable as below:

I want to make some explanations about the variables and methods used in the pattern:

disposedValue boolean variable provides that clients can call the method multiple times without getting an exception.

This override of Dispose method is either called by the client:

or the finalizer:

As you see the distinction is provided by the disposing boolean variable.

As a final note;

prevents finalization because it is not needed as the client explicitly forces the release of resources.

Now, we can use DatabaseConnection class in order to see how Dispose pattern acts in different scenarios.

First, we call the Dispose method explicitly:

Output:

Another and commonly used method to call Dispose is using using statement:

As you see there is no call to Dispose method because the using statement handles that automatically.

Both codes above generate the same output.

Next, let’s call Dispose method more than once and see the output:

Output:

Finally, let’s see what happens if we don’t call the Dispose method explicitly:

Output:

As you see, Dispose method is called implicitly during the finalization. So adding the finalizer during the implementation of the Dispose pattern becomes a safeguard to clean up the resources if the client does not call the Dispose method.

--

--

Sena Kılıçarslan
C# Programming

A software developer who loves learning new things and sharing these..