Demystify garbage collection: Part 6: Understand concept of generation

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.

  1. 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.
  2. 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.
  3. Almost all objects within a generation are in same age.
  4. 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?

GarbageCollection1.jpg

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

Generation 0

GarbageCollection2.jpg

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

GarbageCollection3.jpg

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

GarbageCollection4.jpg

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.

GarbageCollection5.jpg

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;

    }

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

        }

    }

}

Here we are calling overloaded function of GC.Collect(0). The argument (0) represents that garbage collection will perform operation only in generation 0. And as the object is not garbage object it is promoted to generation 1. Like below

GarbageCollection6.jpg

Now object is in generation 1 means for long time it is staying in memory.

Concept 3: Generation 1 promotes to generation 2

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";

            GC.Collect(0);

            Console.WriteLine("Object promoted to Generation:-" + GC.GetGeneration(d));

            Console.ReadLine();

        }

    }

}


Here is output of the program.

GarbageCollection6.5.jpg

Now we are seeing that object is still referred by application's root and not consider as garbage. Though we have tried to clear objects from generation 0 and generation 1 by calling GC.Collect() with 0 and 1 as argument.

What happen? If we call GC.Collect() ?

Nothing, it collects objects from all generation. We will understand same concept with example. 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";

            GC.Collect(0);

            GC.Collect(1);

            Console.WriteLine("Object promoted to Generation:-" + GC.GetGeneration(d));

            Console.ReadLine();

        }

    }

}


Here intentionally we were promoting object towards to higher generation. And at last we are using GC.Collect() to clear dead object from all generation. Below is the sample output.

GarbageCollection7.jpg

Conclusion

I have tried to represent concept of generation with live example associated with pictorial representation of heap memory. Though memory representation is not exact and real time, I have tried to make it simple as much as possible.

Up Next
    Ebook Download
    View all
    Learn
    View all