Heap Generation in .Net Garbage Collection In the previous blog, we saw a brief introduction on the garbage collection concept in .NET. What is garbage collection? How does the garbage collector work? How is the memory allocation done? Structure of the memory heap and advantages of the garbage collector. Over here, we will go into some more depth and look at a few more things related to this concept.
Let us now learn about the various generations in a managed heap.
Basically, a managed heap is broadly classified into three types of generations. This is done in order to manage the long lived and the short lived objects in a better manner. While freeing the memory, the garbage collector first reclaims the short lived objects which consume a small portion of the heap. Then it moves towards the long lived objects. This is how the garbage collector takes its approach. Now, lets proceed to the generation types.
Generation 0:
This is the first or the latest generation wherein the newly created objects reside. This particular generation has short lived objects and are collected on a regular basis. The objects which are able to live in this generation, are transferred to Generation 1. e.g. a temporary object.
Generation 1:
Over here, those objects are present which are long lived and come from the generation 0. The objects that continue to stay in Generation 1 are sent to Generation 2. This generation acts as a buffer or a mediator between the long lived objects and the short lived objects.
Generation 2:
This generation has the most longest existing objects. These are promoted from the first generation and collected rarely.
This was all about the generation types in garbage collection. Now let us see the memory reclaim process and how does it work.
Different Working Phases in Garbage Collector:
1. Marking Phase:
At this stage, the garbage collector enlists the live objects present.
2. Relocating Phase:
In this stage, the collector updates the memory references of those objects which would be compacted.
3. Compacting Phase:
In this stage, the garbage collector obtains the memory previously owned by the dead objects and compresses the existing objects. This phase shifts the surviving objects towards the older portion of the memory segment.
A Thing About Garbage Collection Algorithm:
The garbage collector figures out if any object in the memory is dead or not being utilized by the application. In the event that such object exists, then the memory utilized by these objects can be recovered. In any case, how does the garbage collector become aware of these objects?
Every single application has an arrangement of roots and these distinguish the storage areas for the objects on the managed heap.
The rundown of dynamic roots is kept up by the JIT compiler and CLR, and is supplied to the garbage collector’s algorithm.
The Memory Reclaim Process:
The garbage collector begins to traverse through the roots and make a diagram or a graph of all the objects that can be reached starting from the roots. The underneath fig. demonstrates a stack with designated objects. In this memory heap, the application roots straightforwardly allude to the objects 1,3,4,6 and object 3 and 6 refers to the objects 8 and 10. Henceforth all these items will turn into the part of the live objects chart.
Hope this article would prove informative for you to understand the concept of garbage collection.