Backend Development 10 min read

Understanding Java Garbage Collection: Algorithms, Generations, and Tuning

This article explains how Java's garbage collectors work, covering generational memory spaces, major algorithms such as mark‑sweep, mark‑compact, and copy, the most common collectors for young and old generations, safe points, throughput calculations, and tuning parameters.

Lobster Programming
Lobster Programming
Lobster Programming
Understanding Java Garbage Collection: Algorithms, Generations, and Tuning

When objects on the heap are identified as unreachable, the JVM triggers garbage collection to free memory for new object allocation.

Garbage‑collection algorithms (mark‑sweep, mark‑copy, mark‑compact) define the methodology, while specific collectors implement these strategies. Common collectors include Serial, ParNew, Parallel Scavenge for the young generation, and Serial Old, CMS, Parallel Old for the old generation; G1, ZGC, and Shenandoah work across both generations.

Young‑Generation Collectors

1.1 Serial Collector – the oldest and simplest collector, single‑threaded, pauses all application threads (STW) during collection. It uses a copying algorithm, which is efficient but can cause long pauses for large heaps.

1.2 ParNew Collector – a multithreaded version of Serial, sharing the same parameters and algorithms, often paired with CMS in server‑mode JVMs.

1.3 Parallel Scavenge Collector – a multithreaded collector based on the mark‑copy algorithm, aiming for a controllable throughput. Throughput is calculated as:

<code>吞吐量= 运行用户代码时间 / (运行用户代码时间+运行垃圾收集时间)</code>

Two JVM flags control throughput:

-XX:MaxGCPauseMillis – maximum pause time in milliseconds.

-XX:GCTimeRatio – desired ratio of GC time to total time (inverse of throughput).

Old‑Generation Collectors

2.1 Serial Old Collector – single‑threaded, uses the mark‑compact algorithm, suitable for client‑mode JVMs or as a fallback for CMS failures.

2.2 Parallel Old Collector – multithreaded, also based on mark‑compact, often paired with Parallel Scavenge for high‑throughput, CPU‑intensive workloads.

2.3 CMS (Concurrent Mark‑Sweep) – based on mark‑sweep, designed to minimize STW pauses. It proceeds through initial marking (STW), concurrent marking, remark (short STW), and concurrent sweeping. CMS reduces pause time but is CPU‑intensive and can cause fragmentation.

G1 became the default collector starting with JDK 9, replacing the separate young/old collector model.

Key Takeaways

(1) Young‑generation collectors: Serial, ParNew, Parallel Scavenge; old‑generation collectors: Serial Old, CMS, Parallel Old; unified collectors: G1, ZGC, Shenandoah.

(2) Default collectors: JDK 8 uses Parallel Scavenge + Parallel Old; JDK 9 and later use G1.

(3) CMS employs mark‑sweep; Parallel Old uses mark‑compact.

(4) For low‑pause applications (e.g., e‑commerce, online games), the ParNew + CMS combination is recommended.

(5) For high‑throughput, multi‑core workloads (e.g., scientific computing, big data processing), Parallel Scavenge + Parallel Old is preferred.

backendJavaJVMperformanceGarbage Collection
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.