Articles → .NET → Garbage Collector
Garbage Collector
- Allocate memory to resource.
- Initialize the memory to set the initial state of the resource.
- Use the resource.
- Tear down the state of the resource.
- Free the memory.
Managed Heap
Click to Enlarge
Application Roots
Phase I: Mark - Find The Memory That Can Be Reclaimed.
- GC identifies live application roots.
- 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
- Weak References
- Generations
Weak References
Generations
- Generation 0 → This generation contains those objects which are frequently collected by the garbage collector
- Generation 1 → The objects that survived the garbage collection in Generation 0 are promoted to Generation 1
- 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?