Mastering JVM Garbage Collectors: Which One Fits Your Application?
This article explains the main JVM garbage collectors—including Serial, ParNew, Parallel Scavenge, Serial Old, Parallel Old, CMS, and G1—detailing their algorithms, use cases, advantages, and configuration options, and provides visual diagrams and parameter tables for clear understanding.
This article introduces the main JVM garbage collectors, showing diagrams for each and explaining their characteristics.
Serial (Serial Collector) : Used for young generation GC with a copying algorithm; runs single‑threaded by default in Client mode, leading to longer stop‑the‑world pauses on multi‑core CPUs.
ParNew (Parallel Collector) : Similar to Serial but typically used for parallel young‑generation collection and runs in Server mode; performance may be lower than Serial on single‑core CPUs.
Parallel Scavenge (Parallel Young) : Young‑generation collector using a copying algorithm with parallel threads; focuses on throughput control, e.g., achieving 99% throughput when GC time is 1 min of 100 min total runtime.
Serial Old (Serial Collector for Old Generation) : Old‑generation version of Serial, using a mark‑compact algorithm, mainly for JVMs started in Client mode.
Parallel Old (Parallel Collector for Old Generation) : Old‑generation version of Parallel, also using a mark‑compact algorithm.
CMS (Concurrent Mark‑Sweep) : A concurrent collector aiming for minimal pause times, suitable for B/S architectures; uses a mark‑sweep algorithm, which can cause memory fragmentation. Its phases are Initial Mark → Concurrent Mark → Remark → Concurrent Sweep.
G1 Collector : A server‑oriented collector that may replace CMS. Its key features include:
a) Concurrency and parallelism: utilizes multiple CPU cores to reduce stop‑the‑world pauses while running alongside application threads. b) Region‑based collection: maintains generational concepts, managing regions independently based on object lifetimes. c) Space efficiency: employs a mark‑compact algorithm, minimizing memory fragmentation for long‑running applications. d) Predictable pauses: offers shorter and more predictable pause times compared to CMS.
G1’s operation (without counting the Remembered Set) proceeds through Initial Mark → Concurrent Mark → Final Mark → Live Data Counting and Evacuation.
GC Parameter Summary
-XX:+<option> enables the option.
-XX:-<option> disables the option.
-XX:<option>=<value> sets the option to a numeric or string value.
-XX:+UseSerialGC: use Serial collector for both young and old generations.
-XX:SurvivorRatio: set the ratio between Eden and Survivor spaces.
-XX:NewRatio: set the ratio between young and old generations.
-XX:+UseParNewGC: use ParNew for the young generation.
-XX:+UseParallelGC: use Parallel Scavenge for the young generation.
-XX:+UseParallelOldGC: use Parallel Old for the old generation.
-XX:ParallelGCThreads: specify the number of GC threads.
-XX:+UseConcMarkSweepGC: use CMS (with Serial Old for the old generation).
-XX:ParallelCMSThreads: set the number of CMS threads.
-XX:CMSInitiatingOccupancyFraction: trigger CMS when old‑gen usage reaches this percentage.
-XX:+UseCMSCompactAtFullCollection: compact memory after a full CMS collection.
-XX:CMSFullGCsBeforeCompaction: perform memory compaction after this many CMS cycles.
-XX:+CMSClassUnloadingEnabled: allow class metadata to be reclaimed.
-XX:CMSInitiatingPermOccupancyFraction: start CMS when the permanent generation reaches this percentage.
-XX:UseCMSInitiatingOccupancyOnly: trigger CMS only when the occupancy threshold is reached.
GC Parameter Combinations
-XX:+UseSerialGC → Serial (young) / Serial Old (old)
-XX:+UseParallelGC → Parallel Scavenge (young) / Serial Old (old)
-XX:+UseConcMarkSweepGC → ParNew (young) / CMS (old)
-XX:+UseParNewGC → ParNew (young) / Serial Old (old)
-XX:+UseParallelOldGC → Parallel Scavenge (young) / Parallel Old (old)
-XX:+UseConcMarkSweepGC -XX:+UseParNewGC → Serial (young) / CMS (old)
-XX:+UnlockExperimentalVMOptions -XX:+UseG1GC → G1 (both young and old)
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
