Ads

C# Programming 20 - Garbage Collector


Hello All,

In this article we will learn about Garbage Collector(GC).

What is Garbage Collector?

All we know GC will take very important role in programming, Garbage collector manages the allocation and releases of the memory, Basically it manages memory by deviding objects into Generation(0,1,2) based on their age.



Lets understand GC in simple real world scenario.

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?


Garbage collector has its own engine to clear the memory known as a Optimizing engine, which runs when required and devides objects into two categories.

1. Objects in Use
2. Idle Objects(Known as Garbage)

 

Objects in use are kept in the memory and idled objects are destroyed from the memory. This whole process we call it as automatic memory management.

Lets Understand How Garbage Collector Works....

Basically Garbage collector will devide complete managed heap into three generations internally named as below.

1. Gen 0
2. Gen 1
3. 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...


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !