When to choose SerialGC, ParallelGC over CMS, G1 in Java?


Serial collector

Mainly for single-cpu machine.

Algorithm:

It use a single thread to handle heap, and perform stop-the-world pause during any gc. Just see it as toy.

This is default for client-class machine (32bit jvm on windows or single-cpu machine).


Parallel collector

Algorithm:

It uses multiple gc threads to handle heap, and perform stop-the-world pause during any gc.

<= Java 8, this is default for server-class machine (multi-cpu unix-like machine or any 64bit jvm).


CMS collector

It’s designed to eliminate the long pause associated with the full gc of parallel & serial collector.

Algorithm:

It use 1 or more gc threads to scan the old generation periodically, and discard unused objects, the pause is very short, but use more cpu time.

Warning: since Java 14, it’s removed.


G1 collector

It’s low pause / server style gc, mainly for large heap (> 4Gb).

Algorithm:

  • Similar as CMS, it use multiple background gc threads to scan & clear heap.
  • It divide old generation into parts, it could clean old generation by copy from 1 part to another.
    Thus it’s less possible to get fragmentation.

Since Java 9, this is default for server-class machine (multi-cpu unix-like machine or any 64bit jvm).


Why use G1 as default?

The main reason is to reduce the gc pause time, though the overall throughput might be reduced.

Leave a Comment