GC Algorithms: Serial, G1, ZGC, Shenandoah
Compare GC pause characteristics and throughput trade-offs across modern JVM collectors.
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 MBAll 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