CSharp - Essential Types GC Type

Introduction

GC.Collect() method can be used to force the garbage collection mechanism.

There are many overloaded versions of this method.

In the following example, we use GC.Collect(Int32), which forces an immediate garbage collection from generation 0 through the specified generation.

Demo

using System;

class MyClass// ww w. ja  va2s. c om
{
    private int myInt;
    private double myDouble;

    public MyClass()
    {
        myInt = 25;
        myDouble = 100.5;
    }
    public void ShowMe()
    {
        Console.WriteLine("MyClass.ShowMe()");
    }
    public void Dispose()
    {
        GC.SuppressFinalize(this);
        Console.WriteLine("Dispose() is called");
        Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));
    }
    ~MyClass()
    {
        Console.WriteLine("Destructor is Called..");
        Console.WriteLine(" After this destruction total Memory:" + GC.GetTotalMemory(false));
        System.Threading.Thread.Sleep(60000);
    }
}

class Program
{
    public static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Maximum Generations of GC:" + GC.MaxGeneration);
            Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));
            MyClass myOb = new MyClass();
            Console.WriteLine("myOb is in Generation : {0}", GC.GetGeneration(myOb));
            Console.WriteLine("Now Total Memory is:{0}", GC.GetTotalMemory(false));
            Console.WriteLine("Collection occured in 0th Generation:{0}", GC.CollectionCount(0));
            Console.WriteLine("Collection occured in 1th Generation:{0}", GC.CollectionCount(1));
            Console.WriteLine("Collection occured in 2th Generation:{0}", GC.CollectionCount(2));

            //myOb.Dispose();

            GC.Collect(0);//will call generation 0
            Console.WriteLine("\n After GC.Collect(0)");

            Console.WriteLine("Collection occured in 0th Generation:{0}", GC.CollectionCount(0));//1
            Console.WriteLine("Collection occured in 1th Generation:{0}", GC.CollectionCount(1));//0
            Console.WriteLine("Collection occured in 2th Generation:{0}", GC.CollectionCount(2));//0
            Console.WriteLine("myOb is in Generation : {0}", GC.GetGeneration(myOb));
            Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));

            GC.Collect(1);//will call generation 1 with 0
            Console.WriteLine("\n After GC.Collect(1)");

            Console.WriteLine("Collection occured in 0th Generation:{0}", GC.CollectionCount(0));//2
            Console.WriteLine("Collection occured in 1th Generation:{0}", GC.CollectionCount(1));//1
            Console.WriteLine("Collection ccured in 2th Generation:{0}", GC.CollectionCount(2));//0
            Console.WriteLine("myOb is in Generation : {0}", GC.GetGeneration(myOb));
            Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));

            GC.Collect(2);//will call generation 2 with 1 and 0
            Console.WriteLine("\n After GC.Collect(2)");
            Console.WriteLine("Collection occured in 0th Generation:{0}", GC.CollectionCount(0));//3
            Console.WriteLine("Collection occured in 1th Generation:{0}", GC.CollectionCount(1));//2
            Console.WriteLine("Collection ccured in 2th Generation:{0}", GC.CollectionCount(2));//1
            Console.WriteLine("myOb is in Generation : {0}", GC.GetGeneration(myOb));
            Console.WriteLine("Total Memory:" + GC.GetTotalMemory(false));

        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:" + ex.Message);
        }

    }
}

Result

Analysis

You cannot call the destructor. The garbage collector takes care of it.