Categories: C#

Differences Between Finalize and Dispose Methods in C# and VB.NET

This article is to give a clear understanding of the differences between Finalize and Dispose Methods in C#.This post is answer to following topics,
– Finalize in C# and VB.NET
– Dispose in C# and VB.NET
– Finalize Vs Dispose in C# and VB.NET

Managed resources are .NET objects and that are under the direct control of .NET runtime.
Manged resources use managed memory. When there are no references existing for a managed object the .NET garbage collector will release that managed memory.

What are unmanaged resources?

Unmanaged resources are those that run outside the .NET runtime. For instance, a call to a .dll written in C++ or a call to a DLL in the Win32 API. Unmanaged resources are files, database connections, COM, etc.

There are two methods in the dot net framework to deal with the release of unmanaged objects from memory, Finalize, and Dispose .

What is Finalize( ) Method in C#?

Finalize is used to release unmanaged resources from memory. It does not ensure the garbage collection of the object.

–  Finalize method is part of the object class.

     –  It is automatically raised by the garbage collection mechanism whenever the object goes out of scope.

     –  We need to implement this method whenever we have unmanaged resources in our code and make sure these resources will be freed when the garbage collection process is done.

     –  It will show the effect on the performance of the website and it will not suitable to free objects immediately.  

You are not permitted to call the Finalize method in the code itself.

The garbage collector is the only one who is able to invoke the Finalize() function when an object becomes unavailable.

You can’t just implement the finalize method straight, you have to declare a destructor first.

When the Finalize should be implemented?

Unmanaged resources such as file streams could be defined at the class level. The file may not be ready to be closed, and we’re not certain which stage or procedure would be acceptable.

This object is currently in use in several different places in the program.

Finalize is a good place for unmanaged resources to be released.

Finalize is costly to utilize. An immediate clean is not provided.

When the program starts, the Garbage Collector maintains a separate queue while it checks to see if each object has finished implementing all of its necessary methods.

The GC uses the term “Finalizable” to refer to an object that has been marked for finalization.

Garbage Collector calls the finalize method when the object is ready to be freed and removes it from the memory collection.

What is Dispose( ) Method

Dispose is called when we want an object to release any unmanaged resources with them.

     –  To clear unmanaged resources we need to write code manually to raise dispose() method.

     –  This Dispose() method belongs to IDisposable interface.

     –  If we need to implement this method for any custom classes we need to inherit the class from IDisposable interface.

     –  It will not show any effect on the performance of the website and we can use this method whenever we want to free objects immediately.

Dispose in C#  Vs  Finalize in C#

The Major Distinctions Between Dispose and Finalize in C# are,

Dispose is in the interface IDisposable and  Finalize is in the class object

Dispose can be called at any moment, whereas the garbage collector will call the method Finalize if the object hasn’t been accessed in a long time.

Dispose() must be called manually by a programmer within the code, but the method finalize is called automatically by the garbage collector before it destroys the object.

After implementing the IDisposable interface the method dispose is implemented in the class. Because managed resources are freed immediately by the garbage collector, the method finalize must only be implemented for unmanaged resources.

The dispose() method is quick and releases the object immediately, thus it has no performance impact. Finalize() is a slower method that does not immediately liberate the object’s resources.

Because dispose is specified in the IDisposable it will be implemented by the class that implements this interface with public access mode.  The function finalize()  has a protected access specifier that restricts its accessibility within the class only.

Summary

In this post we explained,  Dispose and Finalize in C#.

Implementing the Finalize method should only be done when absolutely necessary.Dispose should be used first and cleaned up as quickly as possible once all of the unmanaged processing is finished.

Don’t forget to provide your valuable comments in the comment section below.

Rajeev

Recent Posts

OWIN Authentication in .NET Core

OWIN (Open Web Interface for .NET) is an interface between web servers and web applications…

1 year ago

Serializing and Deserializing JSON using Jsonconvertor in C#

JSON (JavaScript Object Notation) is a commonly used data exchange format that facilitates data exchange…

1 year ago

What is CAP Theorem? | What is Brewer’s Theorem?

The CAP theorem is also known as Brewer's theorem. What is CAP Theorem? CAP theorem…

1 year ago

SOLID -Basic Software Design Principles

Some of the Key factors that need to consider while architecting or designing a software…

1 year ago

What is Interface Segregation Principle (ISP) in SOLID Design Principles?

The Interface Segregation Principle (ISP) is one of the SOLID principles of object-oriented design. The…

1 year ago

What is Single Responsibility Principle (SRP) in SOLID Design Priciples?

The Single Responsibility Principle (SRP), also known as the Singularity Principle, is a software design…

1 year ago