This topic is related to heap generation in .net garbage collection you will learn how heap is generated, different working phases, memory reclaim process by crb tech reviews
1. 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.
http://crbtech.in/Dot-Net-Training/
2. 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.
e.g. An application level object which stores static data and which is available for the duration for
which the process runs.
This was all about the generation types in garbage collection. Now let us see the memory reclaim
process and how does it work.
http://crbtech.in/Dot-Net-Training/
3. 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.
e.g. All of the global, and static pointers and all the local variable/parameter object pointers on the
string’s stack in the application are considered to be a part of the roots of the application. All in all, any
CPU registers containing pointers to objects in the managed heap are likewise viewed as a part of the
application’s roots.
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.
http://crbtech.in/Dot-Net-Training/
4. The objects which are unreachable from the roots of the application, are taken to be garbage as they are
not located by the application. Over here, objects with the number 2,5,7 and 9 are considered as dead
objects.
http://crbtech.in/Dot-Net-Training/
5. The garbage collector will clean the dead ones from the memory heap. In the next step, the live objects
will traverse to the older portion of the memory. The Garbage collector also assigns references to the
moving objects within the heap. Root references are also included. This is the general process of
claiming the memory and reusing it.
Hope this article would prove informative for you to understand the concept of garbage collection. You
can join training institutes in pune to enhance your knowledge in .net field.
Related Articles :
• Dot Net Release Silver Light Porting Bridge
• How .Net DLL Works?
http://crbtech.in/Dot-Net-Training/
6. The garbage collector will clean the dead ones from the memory heap. In the next step, the live objects
will traverse to the older portion of the memory. The Garbage collector also assigns references to the
moving objects within the heap. Root references are also included. This is the general process of
claiming the memory and reusing it.
Hope this article would prove informative for you to understand the concept of garbage collection. You
can join training institutes in pune to enhance your knowledge in .net field.
Related Articles :
• Dot Net Release Silver Light Porting Bridge
• How .Net DLL Works?
http://crbtech.in/Dot-Net-Training/