Click here to Skip to main content
Click here to Skip to main content

What is Garbage Collector, Gen 0, 1 & 2?

, 6 Jun 2014
Rate this:
Please Sign up or sign in to vote.
About Garbage collector - Gen 0, 1 and 2

Introduction

Garbage Collector (GC) -Garbage Collector is a feature of CLR which cleans unused Managed objects and reclaims memory. It does not clean unmanaged objects. It’s a background thread which runs continuously.

Generation defines age of the object. There are 3 generations:

  1. Gen 0: When application creates fresh objects, they are marked as Gen 0.
  2. Gen 1: When GC is not able to clear the objects from Gen 0 in first round, it moves them to Gen 1 bucket.
  3. Gen 2: When GC visits Gen 1 Objects and it is not able to clear the objects from Gen 1, it moves them to Gen 2 bucket.

Generations are created to improve GC performance. GC will spend more time on Gen 0 objects rather than Gen 1 and Gen 2, thus improving performance. More objects are in Gen 0 is a good sign rather than more objects in Gen 1/ Gen 2.

We can see Generations of Garbage Collector by creating a Windows application in which we will put a button and put the following code in .cs file. We will use CLR Profiler to see the Generations.

private void button1_Click(object sender, EventArgs e)
{
for(int i=0;i<1000000;i++)
{
cls obj=new cls();
}
}
}
public class cls1
{
}

CLRProfiler- CLRProfier is a tool provided by Microsoft.com to see Generations of Garbage Collector (Gen0, 1, 2).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Akash Ashok Jain
Software Developer (Senior)
India India
I am "Akash Jain" working in IT Industry from last 6 years. From the beginning of my career I worked with C# for both windows and web application. In my short career I worked for 2 Major ERP Applications, e-commerce applications, CMS based applications and also few static websites.
 
I am a MCP for web application for ASP.NET 4.0. I have also done PMP training as project management is one of my interest area.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
GeneralMy vote of 2 PinmemberR. Hoffmann9-Jun-14 23:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 6 Jun 2014
Article Copyright 2014 by Akash Ashok Jain
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid