InterviewSolution
| 1. |
What is Garbage Collector? |
|
Answer» GARBAGE collector manages allocation and reclaiming of memory. GC (Garbage collector) makes a trip to the heap and collects all objects that are no longer used by the application and then makes them free from memory. Each time you create a new object, the common language runtime allocates memory for the object from the managed heap. As long as address space is available in the managed heap, the runtime continues to allocate space for new objects. However, memory is not infinite. Eventually the garbage collector must perform a collection in order to free some memory. The garbage collector's optimizing engine determines the best time to perform a collection, based upon the allocations being made. When the garbage collector performs a collection, it checks for objects in the managed heap that are no longer being used by the application and performs the necessary operations to reclaim their memory. Garbage collection occurs when one of the following conditions is true:
Managed Heap After the garbage collector is initialized by the CLR, it allocates a segment of memory to store and manage objects. This memory is called the managed heap, as opposed to a native heap in the operating system. When the garbage collector is triggered, it reclaims the memory occupied by dead objects, also compacts the live objects so that they are moved together and dead space is removed. This way it makes the heap smaller and ensure that allocated objects stay together on managed heap. Based on object life heaps can be considered as accumulation of two heap: large object heap and small object heap.Heap is managed by DIFFERENT 'Generations', it stores and handles long-lived and short-lived objects, SEE the below generations of Heap:
What happens during a garbage collection Following phases are there of garbage collection:
|
|