Advanced Garbage Collection
What is Garbage Collection (GC)?
- The Garbage Collector (GC) is an automatic memory manager in the Common Language Runtime (CLR).
- It frees developers from manually managing memory, reducing problems like:
- Forgetting to release memory (causing memory leaks).
- Accessing memory that’s already been freed.
Benefits of Garbage Collection
- Automatic Memory Management: No need to manually free memory.
- Efficient Memory Allocation: Objects are allocated on the managed heap quickly.
- Memory Reclamation: GC clears memory of unused objects and reclaims it for new ones.
- Memory Safety: Prevents one object from interfering with another’s memory.
How Memory Works
- Virtual Memory:
- Each process has its own virtual memory (not physical memory).
- Virtual memory can be in 3 states:
- Free: Available for use.
- Reserved: Blocked for use but not yet storing data.
- Committed: Allocated and mapped to physical memory.
- Managed Heap:
- The managed heap is where objects are stored. It’s efficient because it allocates memory contiguously.
- Memory is released by GC when objects are no longer in use.
Conditions for Garbage Collection
GC occurs when:
- System memory is low (detected by the OS).
- Allocated memory exceeds a certain threshold.
- The GC.Collect() method is called (rarely needed).
How Garbage Collection Works
- Phases of GC:
- Marking: Finds and lists all objects that are still in use.
- Relocating: Updates references for objects that will be moved.
- Compacting: Moves surviving objects together to free up space.
- Memory Compaction: Compacting only happens if many unused objects are found.
Generations in GC
To make GC efficient, memory is divided into 3 generations:
- Generation 0:
- For new objects with short lifetimes (e.g., temporary variables).
- GC happens most often here.
- Generation 1:
- For objects that survive Generation 0. Acts as a buffer for long-lived objects.
- Generation 2:
- For long-lived objects (e.g., static data).
- Large objects (85,000+ bytes) are stored in the Large Object Heap (LOH).
- Objects move to the next generation if they survive a GC. Generation 2 GC is the most thorough and reclaims memory across all generations.
Managed Heap and Segments
- The managed heap has two parts:
- Small Object Heap: For smaller objects.
- Large Object Heap (LOH): For large objects.
- Ephemeral Segment: Contains Generation 0 and 1 objects.
Releasing Memory for Unmanaged Resources
- Unmanaged resources (like file handles or network connections) are not automatically cleaned up by GC.
- Developers should:
- Implement a Dispose method to release resources.
- Use a finalizer as a fallback if Dispose isn’t called.
Key Tip: Avoid unnecessary memory allocation. Fewer objects mean less work for the GC, improving performance.
Types of Garbage Collection (GC)
The Garbage Collector (GC) is smart and automatically adjusts to different workloads. However, you can choose a specific type of GC based on your app:
- Workstation Garbage Collection
- Designed for client applications (like desktop apps).
- Default GC for standalone apps.
- Can be concurrent (background) or non-concurrent:
- Concurrent/Background GC allows app threads to keep running while GC is working.
- Background GC is used starting from .NET Framework 4.
- Server Garbage Collection
- Designed for server applications that need high performance and scalability.
- Uses multiple threads to speed up garbage collection.
- In .NET Core and .NET Framework 4.5+, it supports background GC. In earlier versions, it is non-concurrent.
Performance Considerations
Workstation GC
- Threading:
- GC runs on the same thread that triggered it (user thread).
- It competes for CPU time with other threads, as all run at normal priority.
- Single CPU Machines:
- Workstation GC is always used if the computer has only one logical CPU, no matter the settings.
Server GC
- Threading:
- GC runs on multiple high-priority threads (THREAD_PRIORITY_HIGHEST).
- There’s a separate heap and a GC thread for each logical CPU.
- Faster Collection:
- Server GC is faster than Workstation GC on large heaps because it uses multiple threads simultaneously.
- Segment Size:
- Server GC often uses larger memory segments (though the exact size may vary).
- Resource Intensive:
- If many processes (e.g., 12 processes) use Server GC on a machine with a few CPUs (e.g., 4 CPUs), they will compete for resources, slowing down performance.
Optimization Tip:
- If you’re running many instances of an app, use Workstation GC with concurrent GC disabled.
- This reduces context switching and improves performance.
In Short
- Workstation GC: Best for client apps, uses fewer threads, slower on large workloads.
- Server GC: Best for server apps, uses multiple threads, faster but resource-heavy.


