Articles → .NET → Garbage Collector

Garbage Collector







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



Managed Heap




Picture showing the pictorical representation of Managed Heap





Application Roots







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






  1. The garbage collector (GC) identifies all active application roots.
  2. The garbage collector (GC) traverses the application roots and identifies all objects reachable from them. Objects that are not reachable from any application root are considered 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 newly created, short-lived objects that are frequently collected by the garbage collector.
  2. Generation 1 → his generation contains objects that have survived Generation 0 garbage collection and have been promoted to Generation 1.
  3. Generation 2 → This generation contains long-lived objects that survived garbage collection in earlier generations and have been promoted to Generation 2.





Are Primitive Types Collected By The Garbage Collector?





Empty Destructor







Can We Force The Garbage Collector?





What Is The Memory Leak?





What Happens When You Use A Destructor In .NET













Posted By  -  Karan Gupta
 
Posted On  -  Thursday, June 10, 2010
 
Updated On  -  Monday, May 6, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250