Display current generations : GC « Development « C# / CSharp Tutorial






using System;

public class MyClass : IDisposable
{
  ~MyClass()
  {
    Console.WriteLine("In destructor!");
  }
  
  public void Dispose()
  {
    Console.WriteLine("In Dispose() for {0}!");
    GC.SuppressFinalize(this);
  }
}

public class MainClass
{
  public static void Main(string[] args)
  {    
    MyClass c1, c2, c3, c4;

    c1 = new MyClass();
    c2 = new MyClass();
    c3 = new MyClass();
    c4 = new MyClass();
    
    Console.WriteLine("\n***** Displaying generation of cars *****");
    Console.WriteLine("C1 is gen {0}", GC.GetGeneration(c1));
    Console.WriteLine("C2 is gen {0}", GC.GetGeneration(c2));
    Console.WriteLine("C3 is gen {0}", GC.GetGeneration(c3));
    Console.WriteLine("C4 is gen {0}", GC.GetGeneration(c4));
  }
}
***** Displaying generation of cars *****
C1 is gen 0
C2 is gen 0
C3 is gen 0
C4 is gen 0
In destructor!
In destructor!
In destructor!
In destructor!








14.18.GC
14.18.1.Garbage collection type
14.18.2.Display current generations
14.18.3.Clean up gen 0
14.18.4.Forced garbage collection
14.18.5.Get total memory
14.18.6.weak references