Tuesday, October 22, 2013

Garbage collector technique in C#

Garbage collector technique in C#

Garbage collection is a nice feature of .NET. It prevents memory leakage. In this tutorial we will discuss about garbage collection in C# and how to use GC class to collect object.
Generally garbage collection technique is automotive process. Means no one need to call garbage collection program. When it finds that there is need for memory, it runs itself and removes unused object from memory.
Generally we know that, we write destructor to destroy the object which is created by constructor. Bur in .NET destructor not runs automatically. Let us see how it works.
using System;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
 
namespace Test1
{
   public class Garbage   {
          public Garbage()
          {
            Console.WriteLine("Reserve memory");
          }
          ~Garbage()
          {
              Console.WriteLine("Free memory");
          }
 
   }
    class Program    {
        static void Main(string[] args)
        {
 
            {
                Garbage g = new Garbage();
  g = null;
 
            }
            Console.ReadLine();
        }
    }
}
 
Garbage collector technique in C#
Use GC.Collect to free memory
If we want to free all unused object from memory, we call garbage collector explicitly using GC.collect() method. Here is GC is garbage collector class. In below example we are removing all unused object from memory using GC.collect() .
using System;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
 
namespace Test1
{
   public class Garbage   {
          public Garbage()
          {
            Console.WriteLine("Reserve memory");
          }
          ~Garbage()
          {
              Console.WriteLine("Free memory");
          }
 
   }
    class Program    {
        static void Main(string[] args)
        {
 
            {
                Garbage g = new Garbage();
                g = null;
                GC.Collect();
            }
            Console.ReadLine();
        }
    }
}
  Garbage collector technique in C#
Memory free before and after collection
If we want we can measure how much memory got free after garbage collection using GetTotalMemory() function of GC class. Have a look on below example.
using System;
using System.Collections;
using System.Globalization;
using System.Data.SqlClient;
using System.Data;
 
namespace Test1
{
   public class Garbage   {
          public Garbage()
          {
            Console.WriteLine("Reserve memory");
          }
          ~Garbage()
          {
              Console.WriteLine("Free memory");
          }
 
   }
    class Program    {
        static void Main(string[] args)
        {
 
                
                Garbage g = new Garbage();
                g = null;
                Console.WriteLine("Total memory before free: - " +   GC.GetTotalMemory(false));
                GC.Collect();
                Console.WriteLine("Total memory before free:- " +   GC.GetTotalMemory(false));
            
         Console.ReadLine();
        }
    }
}
 
 
Garbage collector technique in C# 


No comments:

Post a Comment