Understanding Java Garbage Collection Algorithms and Their Use Cases
This article provides a comprehensive overview of Java garbage collection, covering the principles of reference counting and reachability analysis, detailing major GC algorithms such as mark‑sweep, copying, and mark‑compact, explaining object allocation strategies, and reviewing traditional and modern collectors like Serial, CMS, and G1, with practical tuning tips for each.
Introduction
Java automatically manages memory through garbage collection, and understanding how the JVM performs this task helps build more stable systems.
The series will be divided into three parts: garbage identification methods, collection algorithms, and analysis tools.
Garbage Identification
Reference Counting
Reference counting assigns a counter to each object; the counter increments on new references and decrements when references are lost. When the counter reaches zero, the object can be reclaimed. However, cyclic references prevent collection, which is why Java does not use this method.
Object A references B (counter 1); B references C (counter 1); C references A (counter 1). All counters are non‑zero, causing a memory leak.
Reachability Analysis
Reachability analysis starts from GC Roots and follows reference chains. Objects not reachable from any root are considered garbage.
Objects referenced from the JVM stack JNI references in native stacks Static fields in the method area Constant pool references
Garbage Collection Algorithms
Mark‑Sweep
Two phases: mark all reachable objects starting from GC Roots , then sweep the heap to reclaim unmarked objects. Simple but requires two passes and can cause fragmentation.
Copying
The heap is split into two halves; live objects are copied from the active half to the free half, after which the original half is cleared. This eliminates fragmentation but requires extra space.
Mark‑Compact
After marking live objects, they are moved to one end of the heap, and the remaining space is reclaimed, combining the benefits of mark‑sweep and copying without extra memory.
Object Allocation and Distribution
The heap is divided into Young (Eden + two Survivor spaces) and Old generations (default ratio 1:2). Objects are first allocated on the stack; if that fails, they go to Eden. Thread‑Local Allocation Buffers (TLAB) reduce contention during multithreaded allocation.
TLAB allocation conditions: small thread‑private objects, non‑escaping, scalar replacement.
Traditional Garbage Collectors
Serial Collector
A single‑threaded collector using copying for the young generation and stop‑the‑world pauses.
Parallel Scavenge
Multithreaded version of Serial for the young generation, default in JDK 8, often paired with Parallel Old.
ParNew
Parallel young collector compatible with CMS, similar to Parallel Scavenge but designed for use with the concurrent collector.
Serial Old
Single‑threaded old‑generation collector using mark‑compact.
Parallel Old
Multithreaded old‑generation collector paired with Parallel Scavenge.
CMS (Concurrent Mark‑Sweep)
Focuses on reducing pause times by performing most work concurrently with application threads. It follows five phases: initial mark, concurrent mark, remark, concurrent sweep, and concurrent reset.
CMS can suffer from concurrent mode failure, fragmentation, and may fall back to a full stop‑the‑world collection.
G1 (Garbage‑First)
Default in modern JDKs, G1 divides the heap into equal‑sized regions and performs mixed collections based on the regions with the most reclaimable space, aiming to meet a user‑specified pause‑time target.
CMS vs. G1
Both target low pause times; however, G1 offers configurable maximum pause, region‑based collection, and uses a mark‑compact algorithm, while CMS is being phased out and can suffer from fragmentation.
Conclusion
The article covered garbage identification techniques, various GC algorithms, object allocation details, and detailed the traditional collectors as well as the modern CMS and G1 collectors, which are frequent interview topics and crucial for performance tuning in production Java applications.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.