This Angular tutorial helps you get started with Angular quickly and effectively through many practical examples.

Garbage Collection in .Net


In this article we are going to discuss Garbage collection in C#. There are many blogs, articles are available on the internet regarding Garbage collection but in this particular article I will try to explain to you with as much as simplest and realistic examples so you can get a clear idea of the Garbage collection use in C#.

Garbage collection Introduction

Garbage collection (GC) in the .NET Framework is an automatic memory management feature that reclaims memory occupied by objects that are no longer in use by the application. This process helps to prevent memory leaks and optimize the performance of the application by freeing up memory that can be reused.

In order to understand, physically the Generations are nothing more than the identification of the Heap area into 3 categories. Once the Garbage Collector receives the memory heap, it divides/distributes the contents in the heap into three categories of objects and stores these objects into three tables:

  1. Generation 0 - This stores short lived objects, such as temporary variables.
  2. Generation 1 - This stores older objects.
  3. Generation 2 - This stores the oldest objects, such as static data and objects that persist throughout the application life.

1. Generation 0

At first when there are no objects on the Heap, then every time a new object is added, it goes into Generation 0 as discussed above. Hence the objects in the Generation 0 are all latest objects that have never been examined by the Garbage Collector.

Garbage Collection in C#

2. Generation 1

Here Objects A, C, E, F and I are free objects, i.e. they do not have any reference, neither from a Root nor from any other object on the heap. When this Generation 0 is completely full, i.e. there is no more space for a new Object then the garbage collection occurs. All the objects that survive the collection (Objects B, D, G and H) are compacted into the left most portion of the heap. These objects are the old objects and are now said to be in Generation1.

Garbage Collection in C#

Now any new object added on the heap, goes to the Generation 0. So we see that in Generation 0 we have New Objects and in Generation 1 we have relative older objects.

3. Generation 2

Same as before, if again the Generation 0 is full, then again the heap is subjected to Garbage Collection. However, this time first all the objects in Generation 1, that survive the collection (Objects B, G & H) are compacted and are now considered to be in Generation 2.

Then, all the objects in Generation 0 are compacted as before and are now considered to be in Generation 1. This clears the space in Generation 0 for new objects.

Hence, we see that in Generation 2 we have the oldest objects (objects B, G & H), in generation we have relatively older objects (objects K & N) and in generation we have the most recently added i.e. the newest objects (objects O, P, Q & R).

Garbage Collection in C#

Currently, generation 2 is the highest generation supported by the runtime's garbage collector. When future collections occur, any surviving objects currently in Generation 2 simply stays in generation 2.

Garbage Collection Process

  1. Mark Phase - The GC identifies which objects are still reachable and marks them.
  2. Sweep Phase - The GC reclaims memory occupied by unmarked objects.
  3. Compact Phase - (Optional) The GC compacts the memory to reduce fragmentation.

Prev Next