Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In Java 9, G1 GC is the default Garbage collector. As of now, I heard of some people preferring CMS garbage collector over G1GC as it seems to be not stable and has some nasty bugs.

What happened with ParallelGC (no buzz these days) ? Is there any use case in which we would like to prefer ParallelGC over CMS/G1 ?

Also, is there any case where SerialGC could out perform all these parallel collectors ?

like image 284
CourseTriangle Avatar asked Feb 10 '19 11:02

CourseTriangle


People also ask

Is G1 better than CMS?

Comparing G1 with CMS reveals differences that make G1 a better solution. One difference is that G1 is a compacting collector. Also, G1 offers more predictable garbage collection pauses than the CMS collector, and allows users to specify desired pause targets.

How do you know which garbage collector to use?

You can use -XX flag for JRE to choose the garbage collector of your choice. Additionally, you can use JConsole to monitor garbage collection.

What is the best garbage collector for Java?

ZGC is a low-latency garbage collector that works well with very large (multi-terabyte) heaps. Like G1, ZGC works concurrently with the application. ZGC is concurrent, single-generation, region-based, NUMA-aware, and compacting. It does not stop the execution of application threads for more than 10ms.

What is the difference between parallel GC and CMS GC?

The primary difference between the serial and parallel collectors is that the parallel collector has multiple threads that are used to speed up garbage collection. The parallel collector is intended for applications with medium-sized to large-sized data sets that are run on multiprocessor or multi-threaded hardware.


1 Answers


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.

like image 183
user218867 Avatar answered Oct 12 '22 00:10

user218867