Garbage Collection
Automatic reclamation of heap memory performed by the runtime.
Classification
- ComplexityMedium
- Impact areaTechnical
- Decision typeArchitectural
- Organizational maturityAdvanced
Technical context
Principles & goals
Use cases & scenarios
Compromises
- Heaps sized too small cause frequent collections
- Pause spikes may violate SLAs
- Misconfigured logging introduces overhead
- Set heap sizes explicitly instead of using defaults
- Enable JSON-format GC logs for analysis
- Monitor and reduce object allocation rate
I/O & resources
- Heap sizing parameters (initial and maximum)
- Collector and tuning parameters per runtime
- Monitoring and profiling tools
- Reclaimed heap memory
- Stable latency profiles
- GC telemetry for capacity planning
Description
Garbage Collection (GC) comprises strategies by managed runtimes to detect unreachable objects and reclaim their memory automatically. Common approaches include tracing collectors (mark-sweep, generational, incremental, concurrent) and reference counting with cycle detection. The goal is to avoid memory leaks, eliminate manual deallocation, and balance latency and throughput across varied workloads.
✔Benefits
- Reduces memory leaks and double-free errors
- Developers avoid explicit deallocation
- Stable performance despite object churn
✖Limitations
- Stop-the-world pauses can raise latency
- Non-deterministic reclamation timing
- Tuning effort depends on workload
Trade-offs
Metrics
- Max GC pause
Longest stop-the-world duration per collection.
- Throughput (ops/s)
Operations per second under steady GC load.
- Heap utilization
Heap occupancy before and after collections.
Examples & implementations
Generational GC in a web service
Default collector with short pauses for API workloads.
Concurrent collector for memory-heavy analytics
Very large heaps with pauses in low milliseconds.
Throughput-oriented collector in batch pipelines
Throughput prioritized over latency.
Implementation steps
Analyze workload (latency vs throughput)
Select an appropriate collector (e.g., generational, concurrent, reference counting)
Set GC flags, instrument metrics, run load test
⚠️ Technical debt & bottlenecks
Technical debt
- Outdated runtime version lacking modern collectors
- No automated GC log analysis
- Missing load tests for GC regressions
Known bottlenecks
Misuse examples
- Region-based collector with a tiny heap on latency-critical services
- Throughput-only collector for UI applications
- Relying on aggressive finalizers instead of clear lifecycles
Typical traps
- Pause spikes from full GC after allocation bursts
- Accidental promotion of long object chains
- Underestimated fragmentation with non-copying collectors
Required skills
Architectural drivers
Constraints
- • Maximum pause time per SLA
- • Limited CPU cores for GC threads
- • Compatible runtime version