Garbage collector technique in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 22158 red flag

How to collect garbage 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();
        }
    }
}

 


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();
        }
    }
}

 

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();
        }
    }
}

 

 

 

 

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)