Wednesday, April 27, 2011

Garbage Collector in Dotnet

Friends, In this article I am going to explain about Garbage Collector and its internal work. 

What is Garbage Collector?
Garbage collector is maintained by CLR in Dotnet. Garbage collector checks for the objects are not being used by the application inside managed heap. If it finds any such objects then it removes those objects from the memory. So during development developer doesn’t need to worry about it.

How memory gets allocated in managed heap?
When process starts, the runtime reserves a contiguous memory for it and this is called as managed heap. When application creates an object it will get allocated to the managed heap. GC internally divides objects in to two categories Small and Large. When CLR loaded initially two heap segments are allocated to it Small Object Heap(SOH) and Large Object Heap(LOH). When object creation request comes, at that time GC will check if the size of object is <= 85KB then it will get allocated on SOH and the objects are larger than 85KB will get allocated to the LOH.

What is Small Object Heap (SOH)?
CLR garbage collector is generational collector. Generations are logical view of GC. It has three generations Generation 0, Generation 1 and Generation 2. Generations only applies to SOH. Each generation maintain its memory threshold. By default all the objects having size <=85KB will get allocated to Gen 0.

What are the generations in GC and how they work internally?
When object allocation on Gen 0 reaches to its max limit the GC will start collecting data on this generation. The objects are not being used will get removed from memory and survived objects moved to the Gen 1. GC resets threshold level of Gen 0. Once Gen 0 collection completed GC start collecting data from Gen 1. Similar to Gen 0, GC collects and remove objects are not being used from memory during Gen 1 collection and survived objects moved to Gen 2. GC also resets threshold level of Gen 1. Objects in Gen 0 called as short lived objects and in Gen 2 called as long lived objects.

When Garbage Collector collects data?
GC automatically starts collecting data
1. When the allocation of objects in Generation 0 exceeds its max threshold limit. This is the most common scenario when garbage collector triggers and collects the data.
2. When system is running under low memory and user explicitly calls GC.Collect method.

What is Large Object Heap (LOH)?
LOH will get collected by GC when user explicitly calls GC.Collect method by passing GC.MaxGeneration as an argument. It also gets collected after completion of Gen 2 collection. While collecting data on LOH, if GC found objects are not being used will get removed from memory and marked as an empty so new object will get allocated in this available memory if the object size fits in available memory. The objects in this LOH will not get rearranged or compacted like Gen 0 and Gen 1 because all the objects are large and moving those objects is more expensive task.

Checkout below links to read more about this

No comments:

Post a Comment