Articles → .NET → Garbage Collector

Garbage Collector







  1. Allocate memory to resource.
  2. Initialize the memory to set the initial state of the resource.
  3. Use the resource.
  4. Tear down the state of the resource.
  5. Free the memory.



Managed Heap




Picture showing the pictorical representation of Managed Heap
Click to Enlarge




Application Roots







Phase I: Mark - Find The Memory That Can Be Reclaimed.






  1. GC identifies live application roots.
  2. It starts walking the roots and graph all the objects reachable from the roots.The objects that are not reachable through the application roots is considered as garbage.

Phase II: Compact - Move All The Live Objects To The Bottom Of The Heap, Leaving Free Space At The Top.







Finalize





Garbage Collector Performance Optimizations


  1. Weak References
  2. Generations

Weak References











Generations




  1. Generation 0 → This generation contains those objects which are frequently collected by the garbage collector
  2. Generation 1 → The objects that survived the garbage collection in Generation 0 are promoted to Generation 1
  3. Generation 2 → This Generation contains the long-lived objects that survived multiple garbage collection





Are Primitive Types Collected By The Garbage Collector?





Empty Destructor







Can We Force The Garbage Collector?





What Is The Memory Leak?





Posted By  -  Karan Gupta
 
Posted On  -  Thursday, June 10, 2010
 
Updated On  -  Thursday, August 31, 2023

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250