In this article, I will be talking about one of the most important topics related to memory management in. NET. We know that managing memory is the primary concern of any application. Thus, to help the programmers focus on implementing their functionality, .NET introduced automatic memory management, using Garbage Collector. Garbage collection is the heart of .NET Applications. The garbage collector has a limitation, in that it can clean up only managed resources. Thus, now the question is what is a managed resource and what is an unmanaged resource.
Managed Resource
Managed resource means anything which can be managed by CLR (any code that uses CLR, can be managed code written in C# or C++).CLR handles the memory management for such resources and automatically clears the memory, when not required.
Unmanaged Resources
Unmanaged resources are generally C or C++ code, libraries, or DLLs. These are called unmanaged because the coder has to do the memory management (allocate memory for the object and clean the memory after the object is no longer required). These can file the handles, database connections, etc.
Thus, now that we have a basic idea of the Managed and Unmanaged resources, we will move further toward our main topic, how to implement memory management for unmanaged resources. CLR provides some help in releasing the memory claimed by the unmanaged resources. For clearing the unmanaged resources, we have a virtual method finalized in the System. Object Class.
Finalize Method
Object Class does not provide any implementation to the Finalize method. Unless a class derived from the object class overrides, the Finalize method garbage collector cannot mark it for finalization. The garbage collector maintains a finalization queue for all the objects in the Heap, whose finalization method code must run before the Garbage Collector can run to reclaim the memory. The Garbage Collector automatically calls the Finalize method, but it is not sure when the Garbage Collector will run and call the Finalize method.
Now, I will show you, how to implement Finalize Method. Finalize is a virtual method of an Object Class. It does not have any access modifier. We cannot call the Finalize method directly, as there is no keyword like Finalize. To use Finalize, we need to create a destructor. Destructor is a special method, which has the same name as the class name with a tilt prefixed before it. The destructor cannot have any parameters. At compile time, the destructor is converted to Finalize() method. The sample code is given below.
using System;
namespace FinalizeDemo
{
class Program
{
static void Main(string[] args)
{
FinalizeDemo d = new FinalizeDemo();
d = null;
Console.ReadLine();
}
}
class FinalizeDemo
{
public FinalizeDemo()
{
Console.WriteLine("Object Created");
}
~FinalizeDemo()
{
Console.WriteLine("Destructor Called.");
}
}
}
now, we have added a destructor for the class. Let's verify whether it has created a Finalize method for the same. For this purpose, I will be using ILSPY.In ILSPY, we will browse the EXE created. Below is the snapshot for the same. Here, we can see the constructor, but not the destructor. Now, click the Finalize method and you will see the destructor. The destructors are converted to the Finalize method at the compile time. Now, we will run the code, given above. Even though we assign null to the object, still it’s not garbage collected. To see the destructor is called, run the Application, given above, through the command prompt. See the below snapshot for the same.





Ghiath NajjarPosted May 5, 2020, 7:39 PM
The else after the if (disposing) is not correct, show this please https://docs.microsoft.com/en-us/dotnet/api/system.gc.suppressfinalize?redirectedfrom=MSDN&view=netcore-3.1#examples
Vivek KumarPosted Aug 21, 2016, 4:47 PM
Nice one...
Vignesh ManiPosted Aug 9, 2016, 8:34 AM
Nice
Piyush MahurkarPosted Aug 9, 2016, 6:23 AM
Nice one
Bhavik PatelPosted Aug 9, 2016, 1:33 AM
Nice