articles

Home / DeveloperSection / Articles / Garbage Collection in C#

Garbage Collection in C#

Chris Anderson 6615 02-Feb-2013

The Garbage collection is very important technique in the .Net framework to free the unused managed code objects in the memory and free the space to the process.The .NET Framework's garbage collector manages the allocation and release of memory for your application. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects.

In C#, you cannot destroy an object explicitly in code. In fact, it has the feature of garbage collection, which destroys the objects for the programmers. The process of garbage collection happens automatically. It ensures that:

·    Objects get destroyed: It does not specify when the object shall be destroyed.

·    Only unused objects are destroyed: An object is never destroyed if it holds the reference of another object.

C# provides special methods that are used to release the instance of a class from memory, Finalize () and Dispose ().

Finalize ()

The Finalize () destructor is a special method that is called from the class to which it belongs or from the derived classes. The Finalize () destructor is called after the last reference to an object I released from the memory. The .NET Framework automatically runs the Finalize () destructor to destroy objects in the memory. However, it is important to remember that the Finalize () destructor may not execute immediately when an object loses scope. This is because the Common Language Runtime (CLR) calls the Finalize () destructor using a system called reference-tracing garbage collection. In reference tracing garbage collection, the CLR periodically checks for objects that are not used by the application. When such an object is found, the Finalize () destructor is called automatically and the garbage collector of the CLR releases the object from the memory.

Dispose ()

The Dispose () method is called to release a resource, such as a database connection, as soon as the object using such a resource is no longer in use. Unlike the Finalize () destructor, the Dispose () method is not called automatically, and you must explicitly call it from a client application when an object is no longer needed. The IDisposable interface contains the Dispose method. Therefore, to call the Dispose () method, the class must implement the IDisposable interface.

The following code shows the life cycle of an object TestCalculator class:

    class TestCalculator

    {
        TestCalculator()
        {
            Console.WriteLine("Constructor Invoked");
        }

        ~TestCalculator()
        {
            Console.WriteLine("Destructor Invoked");
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Main() Begins");
            TestCalculator calc1 = new TestCalculator();
            {
                Console.WriteLine("Inner Block Begins ");
                TestCalculator calc2 = new TestCalculator();
                Console.WriteLine("Inner Block Ends ");
            }
            Console.WriteLine("Main() Ends");
        }
    }

 To see the output of the preceeding program, press Ctrl+F5.

The output of the preceedin code is as follows.

Garbage Collection in C#

The explanation of the preceding output is as follows:

·     The calc1 object has function scope. Therefore, its constructor is executed after the execution of Main () begins.

·      The calc2 object has block scope. Therefore, its constructor is executed after the execution of the inner block begins.

·       The destructor of all objects is invoked when garbage collector is invoked.


Updated 07-Sep-2019
hi I am software developer at mindstick software pvt. ltd.

Leave Comment

Comments

Liked By