Choosing the Right Java Garbage Collector: A Practical Guide for JDK 1.8
This article explains the seven Java garbage collectors available up to JDK 1.8, compares their strengths and weaknesses, and shows how to select and tune the appropriate collector—Serial, Parallel, CMS, or G1—based on application characteristics and deployment environments.
1. Overview
When a Java application starts, besides configuring Xms and Xmx, you must select an appropriate garbage collector. Up to JDK 1.8, seven collectors are available, each with distinct characteristics. The article shows how to match collectors to application and deployment environments.
Serial, ParNew, and Parallel Scavenge handle the Young generation; CMS, Serial Old, and Parallel Old handle the Tenured generation; G1 can operate in both.
2. Garbage Collector Overview
JDK provides multiple collectors, grouped into three categories:
Serial collector: Serial + Serial Old
Parallel collector: Parallel Scavenge + Parallel Old, focuses on throughput
Concurrent collector: CMS, G1, focuses on response time
2.1 Serial Collector
The Serial collector (Serial + Serial Old) uses a single thread; during GC the application pauses (Stop‑The‑World) and uses a copying algorithm.
Advantages:
Simple and efficient; default for client‑mode.
Works well on resource‑constrained environments such as single‑core containers.
Suitable for small desktop programs with limited interaction.
Disadvantages:
Relatively slow collection and limited capacity; frequent STW pauses degrade experience.
ParNew is the multithreaded version of Serial; it shares the same parameters and algorithms. On single‑ or dual‑core machines its efficiency may be lower than Serial, but improves as core count grows.
2.2 Parallel Collector
Parallel collector (Parallel Scavenge + Parallel Old) uses multiple threads. Parallel Scavenge employs a copying algorithm; Parallel Old uses a mark‑compact algorithm.
Key differences:
ParNew aims to reduce STW time.
Parallel Scavenge targets high throughput (user‑thread time / (user‑thread time + GC‑thread time)).
High throughput suits backend workloads with little interaction, such as scientific computing, allowing the CPU to be fully utilized.
Developers can tune two parameters for optimal performance:
Maximum pause time: -XX:MaxGCPauseMillis Throughput ratio: -XX:GCTimeRatio=N (GC time proportion = 1/(1+N)).
2.3 CMS Collector
CMS (Concurrent Mark‑Sweep), introduced in JDK 1.5, is a true concurrent collector for the old generation, aiming for the shortest pause time.
Based on “mark‑sweep” algorithm.
Concurrent collection with short pauses.
CMS collection phases:
Initial Mark (STW, quick root marking).
Concurrent Mark.
Concurrent Preclean.
Remark (STW to catch newly created garbage).
Concurrent Sweep.
Concurrent Reset.
During the most time‑consuming concurrent mark and sweep phases, the application does not pause, keeping pause times short.
Drawbacks:
CPU‑intensive; default thread count = (CPU + 3)/4.
Generates “floating” garbage during concurrent sweep.
Creates memory fragmentation due to mark‑sweep, possibly requiring explicit Full GC.
Mitigation parameters: -XX:ConcGCThreads – number of concurrent GC threads. -XX:CMSInitiatingOccupancyFraction – reserve heap occupancy before starting CMS. -XX:+UseCMSCompactAtFullGC – compact after Full GC. -XX:CMSFullGCBeforeCompaction – perform multiple Full GCs before compaction.
2.4 G1 Collector
G1 works in both young and old generations because it partitions the heap into many equal‑size Regions (1‑32 MiB, up to 2048 regions), each with its own generation attribute, allowing non‑contiguous layout.
G1 maintains a Remembered Set per Region to track inter‑region references, enabling selective reclamation of regions with the highest “garbage‑to‑live” ratio.
Two GC modes are provided:
Young GC – selects all young‑generation Regions; the number of Regions controls pause time.
Mixed GC – selects young Regions plus high‑benefit old Regions based on global concurrent marking, staying within a user‑defined overhead target.
Mixed GC is not a Full GC; it reclaims only selected old Regions. If Mixed GC cannot keep up and the old generation fills, G1 falls back to Serial Old (Full GC), which is much slower. G1 does not provide a dedicated Full GC.
Mixed GC follows these steps after a concurrent marking phase:
Initial marking (STW).
Root region scanning – mark live Regions.
Concurrent marking – mark live objects.
Remark (STW).
Cleanup – reclaim memory.
Mixed GC executes in multiple passes; before each Young or Mixed GC, G1 checks whether the garbage proportion in Regions exceeds -XX:G1MixedGCLiveThresholdPercent. The heap waste allowance is controlled by -XX:G1HeapWastePercent.
References: https://bdqfork.cn/articles/33, https://juejin.im/post/5bade237e51d450ea401fd71
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.
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.
