In this real world, think of a garbage collector like a different care taker who cleans up after a party, imagine people attending a big party(also called creating object), and the care taker (Garbage collector) identifying and removing unused items(freeing up memory) to keep space clean and funtional.
What is Automatic Memory Management?
1. Objects in Use2. Idle Objects(Known as Garbage)
1. Gen 02. Gen 13. Gen 2
Garbage Collector Generations:
Generation
is a portion of memory from the managed heap
Maximum
number of generations are allowed. Only three.
In
general always generation 2 memory size is bigger than the generation 1 memory size and generation 1 memory size is bigger than the generation 0 memory size
This can be represented by mathematically like
Gen 2 > Gen 1 > Gen 0
Generally, memory to the generations is allocated by the CLR based on the application size. Garbage collector has thumb rule that it should place the newly created object only in generation zero. GC has no rights to place the new newly created object into generation one and two.
When application execution starts and application is trying to create new object, then GC will place this object into generation zero, Like this newly created object, object will be placed in generation zero until the completion of generation zero memory.
When Gen 0 Is Completely full?
When generation zero is completely filled and application is trying to create new object, then GC will perform an operation known as collection means. It examines all the objects present in generation 0, identifies iddle objects and object in use.
Iddle objects are destroyed or placed in finalisation queue.
Object in use are transferred to the next generation so
that generation 0 will become empty so then newly created objects is placed
in Generation 0.
When Gen 0 and 1 Is Completely full?
When generation zero is completely filled and application is trying to create new object, then GC will perform an operation known as collection means. It examines all the objects present in generation 0 and 1, identifies iddle objects and object in use.
Iddle objects are destroyed or placed in finalisation queue.
Object in use are transferred to the next generation so that generation 0 and 1 will become empty so then newly created objects is placed in Generation 0.
Like this after performing so many collection of Gen 0, 1 and 2 in case all the Gen filled then same steps like above will be perfomed to Generation 0, 1 and 2, so then newly created objects is placed in Generation 0
I hope you understood this article, see you in next article.
Take care Bye Bye...