Mastering Java Garbage Collectors: When to Choose Serial, Parallel, CMS, or G1
This article explores the evolution of Java garbage collectors—from the simple Serial and Parallel collectors to the concurrent CMS and region-based G1—detailing their algorithms, tuning parameters, strengths, weaknesses, and best‑practice recommendations for selecting the optimal collector based on workload and memory constraints.
Introduction
As Java has evolved, various garbage collectors have emerged, ranging from serial to parallel execution, from high‑throughput to low‑latency designs, aiming to let developers focus on code rather than memory management.
Serial Collector
The older single‑threaded collector pauses application threads during collection. It is simple and efficient. Serial works on the young generation using a mark‑copy algorithm, while Serial Old handles the old generation with a mark‑compact algorithm.
Parallel Collector
Multiple GC threads work in parallel, offering higher throughput on multi‑core CPUs, but application threads still wait. It includes ParNew (young) and Parallel Old (old) collectors, essentially multithreaded versions of Serial.
ParallelScavenge
ParallelScavenge is a young‑generation collector similar to ParNew, using a mark‑copy algorithm. It targets a controllable throughput, with key tuning parameters such as -XX:MaxGCPauseMillis and -XX:GCTimeRatio. Adaptive size policy ( -XX:+UseAdaptiveSizePolicy) can automatically adjust generation sizes.
CMS Collector (Concurrent Mark Sweep)
CMS aims for low pause times and works concurrently with application threads. It performs concurrent marking and sweeping, reducing stop‑the‑world phases. Its four phases are Initial Mark (STW), Concurrent Marking, Remark (STW), and Concurrent Sweep. Advantages include reduced pause times; disadvantages involve higher CPU usage, memory sensitivity, and potential fragmentation.
G1 Collector (Garbage‑First)
G1 introduces a region‑based heap and a pause‑time model, targeting a pause goal (e.g., -XX:MaxGCPauseMillis). It tracks region “value” to prioritize reclamation. G1 includes Young GC, Mixed GC, and Full GC phases, each with specific triggers and steps such as root scanning, RSet updates, object copying, and reference processing.
Key concepts: Regions, Humongous objects, TAMS pointers, Remembered Sets (RSet), and Dirty Card Queues.
Best Practices
Let G1 manage young‑generation size automatically.
Set realistic pause‑time goals; overly aggressive targets increase GC frequency.
Choose CMS for small‑memory workloads and G1 for larger heaps (typically 6‑8 GB).
Conclusion
No single collector is a silver bullet; selection should consider application characteristics and system constraints.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
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.
