Introduction to Java Garbage Collectors and Their Characteristics
This article provides a comprehensive overview of the eight Java garbage collectors, detailing their target heap generations, operational characteristics, configuration flags, and suitability for different application performance and latency requirements.
Java provides eight garbage collectors, each suited for specific heap generations or whole‑heap collection.
Young‑generation (copying) collectors: Serial, ParNew, Parallel Scavenge.
Old‑generation (mark‑sweep/compact) collectors: Serial Old, Parallel Old, CMS.
Whole‑heap collectors: G1 (Garbage‑First) and ZGC (Z Garbage Collector).
Serial is a single‑threaded collector that pauses all application threads (Stop‑The‑World) during collection; enabled with -XX:+UseSerialGC. It uses a mark‑copy algorithm for the young generation and is suitable for client‑mode JVMs or single‑CPU environments.
Parallel Scavenge is a multi‑threaded young‑generation collector focused on throughput; it also pauses application threads. Enable with -XX:+UseParallelGC. It uses the same copy algorithm as Serial but can adjust pause times dynamically.
Parallel Old extends Parallel Scavenge to the old generation using a parallel mark‑compact algorithm; enabled with -XX:+UseParallelOldGc and thread count set via -XX:ParallelGCThreads. It is ideal for batch or data‑processing workloads that prioritize throughput.
ParNew is the multithreaded version of Serial for the young generation, typically paired with CMS for the old generation; enabled with -XX:+UseParNewGC and thread count configurable via -XX:ParallelGCThreads.
CMS (Concurrent Mark‑Sweep) aims for low pause times by performing most of the work concurrently; suitable for latency‑sensitive services. Enable with -XX:+UseConcMarkSweepGC and configure threads with -XX:ConcGCThreads. It uses a mark‑clear algorithm and may suffer from fragmentation.
G1 is a server‑oriented collector that partitions the heap into many regions and collects the regions with the most garbage first; it balances throughput and pause‑time goals. Enable with -XX:+UseG1GC. It performs a series of phases: initial mark (STW), concurrent mark, final mark (STW), and region‑based evacuation.
ZGC is a scalable low‑latency collector introduced in JDK 11, targeting very large heaps (up to terabytes) with pause times typically under 10 ms. Enable with -XX:+UseZGC. It uses colored pointers, read barriers, and region‑based allocation to achieve mostly concurrent collection.
The article also includes configuration parameters and performance considerations for each collector.
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.
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.
