Video in progress

Which garbage collector is the default in current Java versions?

Since Java 9, the default on a server-class machine is G1. If the JVM sees fewer than 2 CPUs or less than 1792 MB of memory, Serial GC is chosen. In Java 8 the default was Parallel GC.

📖 Answer

Most people answer "G1" to this question and stop. But there is a subtle point.

For the JVM to consider itself on a "server-class" machine, it must see at least 2 CPUs and about 1792 MB of memory. Otherwise Serial GC runs. This is common in containers: you gave the pod a cpu: 1 limit, the JVM sees 1 CPU and chooses Serial. As a result a large heap is cleaned by a single thread and the pauses get longer.

To check which GC is running:

java -XX:+PrintCommandLineFlags -version
# ... -XX:+UseG1GC ...

jcmd 12345 VM.flags | tr ' ' '\n' | grep 'Use.*GC'

That is why on prod I always set the GC explicitly with a flag, for example -XX:+UseG1GC. That way, even if the pod's resources change, the GC doesn't change.