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:

  • The system has low physical memory. This is detected by either the low memory notification from the OS or low memory indicated by the host.
  • The memory that is used by allocated objects on the managed heap surpasses an acceptable threshold. This threshold is continuously adjusted as the process runs.
  • The GC.Collect method is called. In almost all cases, you do not have to call this method, because the garbage collector runs continuously. This method is primarily used for unique situations and testing.

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:

  • 0 Generation (Zero): This generation holds short-lived objects, e.g., Temporary objects. GC initiates garbage collection process frequently in this generation.
  • 1 Generation (One): This generation is the buffer between short-lived and long-lived objects.
  • 2 Generation (Two): This generation holds long-lived objects like a static and global variable, that needs to be persisted for a CERTAIN amount of time. Objects which are not collected in generation Zero, are then moved to generation 1, such objects are known as survivors, similarly objects which are not collected in generation One, are then moved to generation 2 and from there onwards objects remain in the same generation.

What happens during a garbage collection

Following phases are there of garbage collection:

  • Marking Phase: that finds and creates a list of all live objects.
  • Relocating Phase: that updates the references to the objects that will be compacted.
  • Compacting Phase: that reclaims the space occupied by the dead objects and compacts the surviving objects. It moves objects that have survived a garbage collection toward the older END of the segment.


Discussion

No Comment Found