The Garbage Collector (GC) has two primary strategies for a full Gen 2 cleanup.
The choice depends on the situation: application responsiveness vs. maximum memory reclamation.


Background GC (BGC): The Quiet Housekeeper 🤫

  • Analogy: A quiet housekeeper tidying the office while everyone is still working.
  • Goal: Maintain application responsiveness.
  • Method:
    • Only sweeps, does not compact.
    • Creates a free list of available memory spaces.
  • Outcome: Minimal interruption; some fragmentation remains.

Full Blocking GC: The Emergency Crew 🚨

  • Analogy: “All hands on deck” cleanup — everything pauses while the GC works.
  • Goal: Reclaim maximum memory.
  • Method:
    • Compacts the heap aggressively.
    • Pauses the entire application (Stop-the-World).
  • Used When:
    1. High Memory Pressure – system RAM is low.
    2. Severe Fragmentation – sweeping alone isn’t enough.
    3. Last Resort – just before throwing an OutOfMemoryException.

Diagram: Cleaning Strategy Choice

flowchart TB
    A[Full Gen 2 Cleanup Needed] -->|Default| B[Background GC - BGC]
    A -->|Critical Situation| C[Full Blocking GC]

    B --> D[Sweeps Only]
    B --> E[Free List Created]
    C --> F[Compacts Heap]
    C --> G[Application Paused]