Skip to main content
Garbage collection · Topic 5 of 6

Choosing a collector

Definition​

Java has several garbage collectors. G1 is the default and balances speed and pauses. ZGC aims for very short pauses on large heaps. Parallel favours total throughput, and Serial is for tiny programs. You choose one with a single -XX:+Use...GC flag.

Example code​

The flags for each collector

# G1: the default on most machines. Balanced; tune with a pause goal
java -XX:+UseG1GC -XX:MaxGCPauseMillis=100 -jar app.jar

# ZGC: very short pauses, even on very large heaps (generational by default in recent Java)
java -XX:+UseZGC -jar app.jar

# Parallel: highest total throughput, longer pauses (good for batch jobs)
java -XX:+UseParallelGC -jar app.jar

# Serial: one thread, for tiny containers and small tools
java -XX:+UseSerialGC -jar app.jar

Finished this page? Track your progress (saved only in your browser).