Welcome to demystify garbage collection article series. If you are old reader in this series, probably you know how we are moving towards deep with garbage collection concept. So far we have discussed how garbage collection works? Concept of GC class and in previous article we were discussing garbage collection algorithm. If this is your first article in this series I will suggest you to go through all below link to understand above concept with example.
In today's article we will understand the concept of generation more closely. Probably all of you know garbage collection performs its operation through generation (You can find example of generation in first and second article in this series). OK, before going to concept of generation we will recap few necessary concepts very quickly.
- All objects in the heap are allocated from one contagious range of memory address and heap is divided into generation. So that it is easy to eliminate the garbage by garbage collection algorithm.
- Gen 0 and Gen 1 occupy a single segment as the ephemeral segment and Generation 2 is a set of further segment and the large heap is yet another group of segment.
- Almost all objects within a generation are in same age.
- Objects moves from lower generation to higher generation.
OK, let's start with generation. Heap memory
is logically partitioned by a concept called generation. Yes, it's nothing but a
logical partition to keep track of object's age. Basically there are three
generation
Generation 0
Generation 1
Generation 2
Now let's represent generation concept by simplified diagram of heap memory.
How heap looks like after divide by generation?

This is the simplified representation of generation in heap memory. Let's talk
little about each generation.
Generation 0

This is youngest generation among all generation. When new object creates, it
creates in generation 0. And garbage collector starts to sweep from generation
0. Means, if the object has no reference and stay in generation 0 then its get
collects from generation 0. And if the object has reference (means it's live
object) it is promoted to next generation. If object gets collect from
generation 0 then we can assume that (not assume, we can conclude that) object
is not staying long time is application and it is good sign for best memory
management.
Generation 1

Object(s) which survives (though. It should not survive) in generation 0 are
promoted to generation 1. If one object is promoted to generation 1 then we can
say that somehow the object is not used properly. Means, maybe we have used in
code but forgotten to clean up after use.
Generation 2

Generation hell. This generation contains those objects which has created long
time ago and still not cleared from memory and there references are still alive.
OK, so if we conclude above discussion, the basic idea is. "Object creates in
generation 0 and if it not collected then it moves towards upper generation.
Now, we will prove this concept with practical example.
Concept 1: Object creates in Generation 0
Let's prove this concept .Have a look on below example.
using System;
using System.Collections;
namespace Test
{
class Dummy
{
public String Name;
}
class Program
{
static void Main(string[] args)
{
Dummy d = new Dummy();
d.Name = "Sourav Kayal";
Console.WriteLine("Object created in Generation:-" + GC.GetGeneration(d));
Console.ReadLine();
}
}
}
In this example we have created one new object and tried to get generation
information of that object.

Lest side is the output of example and in right side we are seeing the position
of object. Output screen is saying object has created in generation 0. Because
we know that when object creates at first time it creates in generation 0.
Concept 2: If not collect then promotes to generation 1
To prove send concept have a look on below code.
using System;
using System.Collections;
namespace Test
{
class Dummy
{
public String Name;




Join the conversation! Your thoughts help the community grow.