GC Algorithms: Serial, G1, ZGC, Shenandoah
Compare GC pause characteristics and throughput trade-offs across modern JVM collectors.
GC Algorithms: Serial, G1, ZGC, Shenandoah is a free Java Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Choosing a GC Algorithm
The JVM ships with multiple GC algorithms, each optimized for different workloads. Choosing the right one depends on heap size, latency requirements, and throughput goals.
// Enable GC algorithms:
// -XX:+UseSerialGC
// -XX:+UseParallelGC
// -XX:+UseG1GC (default since Java 9)
// -XX:+UseZGC (Java 15+ production)
// -XX:+UseShenandoahGC (Red Hat / OpenJDK builds)Serial GC
Serial GC uses a single thread for collection. All application threads stop during GC ("stop-the-world"). Suitable only for small heaps or single-CPU environments like embedded systems.
// Use:
// java -XX:+UseSerialGC -Xmx256m MyApp
// Pauses: long (proportional to heap size)
// Throughput: low
// Best for: CLI tools, small heaps < 100 MBParallel GC (Throughput Collector)
Parallel GC uses multiple threads for Minor and Major GC. Stop-the-world pauses are shorter than Serial but still significant. Best for batch processing where throughput matters more than latency.
// Use:
// java -XX:+UseParallelGC -XX:ParallelGCThreads=8 MyApp
// Pauses: moderate
// Throughput: high
// Best for: batch jobs, number crunchingG1 GC (Garbage-First)
G1 divides the heap into equal-sized regions (~2 KB – 32 MB). It collects regions with the most garbage first, meeting a configurable pause target. Default since Java 9.
// Use:
// java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 MyApp
// Pauses: bounded by target (but not guaranteed)
// Throughput: good
// Best for: large heaps (4 GB+), mixed workloadsG1 Region Types
G1 regions are dynamically assigned as Eden, Survivor, Old, or Humongous (for large objects > 50% of region size). This flexibility allows incremental collection.
ZGC (Z Garbage Collector)
ZGC performs most work concurrently with the application. Pause times are typically under 1 ms regardless of heap size. Production-ready since Java 15.
// Use:
// java -XX:+UseZGC -Xmx16g MyApp
// Pauses: < 1 ms (usually)
// Throughput: slightly lower than G1 (concurrent work has CPU cost)
// Best for: large heaps, low-latency services (APIs, trading)Shenandoah GC
Shenandoah (Red Hat) also achieves sub-millisecond pauses via concurrent compaction. Available in OpenJDK builds. Very similar goals to ZGC but uses different internal algorithms.
// Use:
// java -XX:+UseShenandoahGC MyApp
// Available in: Red Hat builds, some AdoptOpenJDK distributions
// Pauses: sub-millisecond
// Best for: latency-sensitive workloadsGenerational ZGC (Java 21+)
Java 21 introduced Generational ZGC, combining ZGC's low-pause approach with generational collection, reducing CPU overhead while keeping pauses minimal.
// Enable in Java 21+:
// java -XX:+UseZGC -XX:+ZGenerational -Xmx8g MyAppGC Pause Time vs Throughput Trade-off
No GC algorithm is best at everything. Parallel GC maximizes throughput. G1 balances throughput and latency. ZGC/Shenandoah minimize pauses at a small throughput cost.
Enabling GC Logging
Log GC events with -Xlog:gc*:file=gc.log:time,uptime,level. Analyze logs with tools like GCViewer or GCEasy to find long pauses and allocation spikes.
// JVM flags:
// -Xlog:gc*:file=gc.log:time,uptime,level,tags
// Inspect logs:
// cat gc.log | grep "Pause"Monitoring GC with JVM Metrics
Use ManagementFactory.getGarbageCollectorMXBeans() to programmatically read GC count and time, or export them to Prometheus via Micrometer in Spring Boot apps.
ManagementFactory.getGarbageCollectorMXBeans().forEach(gc ->
System.out.printf("%s: %d collections, %d ms%n",
gc.getName(), gc.getCollectionCount(), gc.getCollectionTime()));Quick Check
Which GC algorithm targets sub-millisecond pause times in production?
Recap
Serial GC for tiny heaps, Parallel GC for batch throughput, G1 for balanced workloads (default), ZGC/Shenandoah for ultra-low-latency. Enable GC logging in production to detect problems early.
Frequently asked questions
Is the “GC Algorithms: Serial, G1, ZGC, Shenandoah” lesson free?
Yes — the full text of “GC Algorithms: Serial, G1, ZGC, Shenandoah” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “GC Algorithms: Serial, G1, ZGC, Shenandoah”?
Compare GC pause characteristics and throughput trade-offs across modern JVM collectors. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “GC Algorithms: Serial, G1, ZGC, Shenandoah” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- JVM Heap Regions and Object Lifecycle
- GC Algorithms: Serial, G1, ZGC, Shenandoah
- Detecting and Fixing Memory Leaks
- GC Tuning Flags and JVisualVM Profiling