Garbage Collection

  • Garbage collection is a process of deleting objects from memory, to free up memory; so that same memory can be re-used
  • CLR automatically allocates memory for all objects created any where in the application, whenever it encounters “new className()” statement. This process is called as “Memory management”, which is done by “Memory Manager” component of CLR.
  • All objects are stored in heap
  • Heap is only one for the entire application life time.
  • Default heap size -> 64MB (approx) and extendable.
  • When CLR can’t find space for storing new objects, it performs a process called “Garbage Collection” automatically, which incudes “identification of un-referenced objects and deleting them from heap; so that making room for new objects”. This process is done by “Garbage Collector (GC)” component of CLR.

        How GC decides objects are alive ?

  • GC check belongs information from the MSIL code:
    • It collects references of an object.
    • It identifies whether any object is referenced by static field.
    • The objects that has at least one living reference variable in any stack or static field are alive objects. Others are dead objects/ un-used objects.

       When GC gets triggered ?

  • No specific timings when it will be triggered.

       GC gets triggered in these following conditions.

  • When the heap is full or free space is too low.
  • When we call GC.Collect() explicitly.


Leave a Comment