javagarbage-collectionjava-11g1gc

Should I use SerialGC or G1GC ona single CPU system?


I have a container that is limited to 1 CPU, the default case for java 11+ (and probably older also) in such case it to user SerialGC. Should I force a threaded GC (like G1GC) or just leave it at SerialGC?

Which one will perform better on a single CPU? I always assumed SerialGC is better in such case but I frequently see G1GC forced in some cases.

EDIT: I'm asking for general case, because we have a lot of different apps running using the same configuration and it is hard to test each and every case.


Solution

  • According to the documentation.

    The serial collector uses a single thread to perform all garbage collection work, which makes it relatively efficient because there is no communication overhead between threads.

    It's best-suited to single processor machines because it can't take advantage of multiprocessor hardware, although it can be useful on multiprocessors for applications with small data sets (up to approximately 100 MB).

    I'm assuming processor = core in the documentation (and your question). While the documentation says that the serial collector is not a good option for multi-core machines, it doesn't say that other collectors would be bad for a single-core machine.

    The other collectors do tend to use multiple threads though, and you won't get the full benefits of those in a single-core environment.

    So why have you seen G1GC used? Maybe no reason other than it was the newest. However if there is a reason, it would most likely be the shorter GC pauses that G1 provides:

    If response time is more important than overall throughput and garbage collection pauses must be kept shorter than approximately one second, then select a mostly concurrent collector with -XX:+UseG1GC or -XX:+UseConcMarkSweepGC.

    The best case scenario is that in those cases they measured the performance with different collectors and chose the one that provided the best results.

    Also consider the String deduplication Holger mentioned in the comments. This is a specific memory optimization that can be the reason behind using G1GC. After all if you have a single core, you probably don't have a lot of memory at your disposal either.