Skip to content
Home » Programming » C# » Garbage Collection in C# .NET

Garbage Collection in C# .NET

Garbage Collection in CSharp

Garbage Collection in C#.NET Framework

This article explains garbage collection in C#.NET with an example. By the end of this article, you will understand what the .NET Framework garbage collector is and how it works.

What is Garbage Collection in .NET Framework?

Running a .NET application creates many objects. At some point, application doesn’t use some of these objects. The .NET Framework garbage collector is just a small routine. Or you could say it’s a background process thread that runs deterministically and tries to identify objects that are not currently in use by the application and deallocates the memory.

In short, the garbage collector is a facility provided by the CLR (Common Language Runtime) that helps clean up or dispose of unused managed objects. Cleaning up or disposing of these unused managed objects essentially reclaims memory.

Garbage Collection Generations in C#.NET

Let’s understand what the garbage collector generation is and how it affects garbage collection performance. There are three generations. Generation 0, Generation 1, Generation 2.

Generation 0, 1, and 2

Suppose you have a simple application called DemoApp. Seven managed objects are created when the application starts. Whenever a new object is created, it is moved to a bucket called generation 0 (Gen 0).

Gen 0

Garbage Collection first round.

The Garbage Collector, runs continuously as a background process thread, checking if there are any unused managed objects and cleaning them up so that memory can be reclaimed. Suppose he doesn’t need two objects (object1, object2, object3, and object4) in his application. So the garbage collector will destroy these four objects (Object1, Object2, Object3, and Object4) and reclaim memory from the gen 0 bucket. However, the remaining three objects of his (Object5, Object6, and Object7) are still required by the application. So the garbage collector doesn’t delete these three objects. What the garbage collector does is move these three managed objects (Object5, Object6, and Object7) into the first-generation bucket.

Gen 0 and Gen 1

Now suppose the application creates two more new objects (Object8 and Object9). As a new object, it should be created in a gen 0 bucket.

Gen 0  and Gen 1

Garbage Collection second round.

Then the garbage collector runs again and visits the Gen 0 bucket to see what objects are in use. Suppose both objects (Object8 and Object9) are not used by the application, so it deletes both objects to reclaim memory. Next, go to your Gen 1 bucket and check for unused objects. Assume that Object6 and Object7 are still needed by the application, but object5 is not needed. So, what the garbage collector does is destroy Object5 to reclaim memory and move Object6 and Object7 to the Gen 2 bucket.

Gen 0, Gen 1 and Gen 2

In this way Garbage Collection runs continuously.

In short,

Gen 0: When the application creates new objects, they are marked as Gen 0.

Gen 1: If the GC fails to remove items from Gen 0 in the first round, it moves them to Gen 1 buckets.

Gen 2: When the GC accesses Gen 1 objects and cannot remove objects from Gen 1, it moves them to Gen 2 buckets.

How to see Garbage Collection Using Performance Profiler?

Here I’ve Simple Console App in with the function Fun1(), which I’m calling inside Main() 10000000000 time. This, function basically creates Object of Class Customer every time we call it.. And after the certain period of time Garbage Collection deallocates the memory.

using System;

namespace GarbageColletion
    class Program
        static void Main(string[] args)
            for (double i = 0; i < 10000000000; i++)
        static void Fun1()
            Customer x = new Customer();
    class Customer
        public string? Name { get; set; }

To see the Garbage Collection Using Profiler, follow the following steps.

Step 1: Click on Debug.
GC with Performance Profiler Step 1
Click on Debug
Step 2: Click on Performance Profiler.
GC with Performance Profiler Step 2
Click on Performance Profiler
Step 3: Check ‘Memory Usage’.
GC with Performance Profiler Step 3
Check ‘Memory Usage’
Step 4: Click on Start. 
GC with Performance Profiler Step 4
Click on Start
Garbage Collection Graph using Performance Profiler
Garbage Collection Graph using Performance Profiler

Also Read:
C# for Beginners
Stack, Heap, Value Type and Reference Type in C#

Leave a Reply

Your email address will not be published. Required fields are marked *