Krzysztof Krasoń
Krzysztof Krasoń

Reputation: 27476

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.

Upvotes: 2

Views: 2448

Answers (4)

AtliB
AtliB

Reputation: 1273

One important thing that has not been mentioned in this thread is that the G1GC can return the memory (uncommit it) back to the OS, so if other applications are running on the server, they can make use of it.

I noticed this when switching from a single vCPU server to 2 vCPU server, as java by default uses SerialGC for single CPU and G1GC for multi-CPU (well at least it does for JDK 11)

Upvotes: 0

Speakjava
Speakjava

Reputation: 3392

As a more general comment, don't make the assumption that because you only have a single core/CPU that making a task multi-threaded will have no benefit. Depending on the task involved (in this case GC), there may well be situations where one thread becomes blocked (e.g. waiting for IO to complete), which allows other threads performing another part of the task to use the processor and complete useful work. Overall performance is increased, despite only one thread being able to run at a time.

Upvotes: 2

Kayaman
Kayaman

Reputation: 73558

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.

Upvotes: 3

rebeliagamer
rebeliagamer

Reputation: 1278

What do you want to optimize? Do you want to be able always to answer extremely fast or to have better overall performance? In the first case, you should aim for shorter GC pauses, in the second for the lower sum of all the GC pauses.

There are other factors that you have in mind (i.e. how often applications are restarted) so IMO the best approach is a data-driven approach. Use GC easy or GC viewer to analyze the performance of each application and act accordingly.

Please have in mind that GC tuning is not always required so if you do not know what you want to achieve you probably optimize prematurely.

In general:

  • use The Serial GC for applications that do not have low pause time requirements and are run in the environment with low resources
  • go with G1 Garbage Collector if you have more resources or you need to answer fast (remember to measure the performance before and after the change)

Upvotes: 2

Related Questions