Garbage Collector in c#
The garbage collector (GC) is mainly derived from the Base library – “System”, we can simply say, I would like to Compare Garbage Collector with an Traffic Police Man, Because a Traffic Policeman will clear the traffic in the road, when an unwanted issue happening on a road and make road as hassle free road for the people, Like wise Garbage collector will act as Memory manger to deallocate the memory when it is consumed by unwanted or unused object in a program and make the application to run properly without any deviation in a performance.
Generation of Garbage Collector:
1. Generation 0
2. Generation 1
3. Generation 2
1. Gen 0 è (Temporary Variables) - Life time of object is short time, then Gen 0 Will take care of clearing and releasing the object from Memory. I.e. Simply we can say when it’s a local private variable or the scope is limited to a class then those variable life time would be defined as short span, so it must be cleared by Gen 0.
2. Gen 1 è (Internal Variables) - Life time of object is between the short time and Long time, then Gen 1 Will take care of clearing and releasing the object from Memory. I.e. Simply we can say when it’s an internal variable or the scope is limited to an object then those variable life time would be defined under Gen 1 Category, so it must be cleared by Gen 1.
3. Gen 2 è (Application Variables) - Life time of object is Long time, then Gen 1 Will take care of clearing and releasing the object from Memory. I.e. Simply we can say when it’s an Application variable or the scope has no limited then those variable life time would be defined as short span, so it must be cleared by Gen 2.
Advantage
· We are not responsible for the allocation and deallocation of memory or to define the life span of the objects
· Whenever an Instance is created then the allocation will be happened and whenever boxing is happened for a value type
· If no enough memory available, then GC will come into the picture to deallocate the unwanted object consumed memory
Comments
Post a Comment