synapse
synapse

Reputation: 5728

Optimal number of GC threads for a single CPU machine

I'm using G1 garbage collector for an application that should use a single CPU core (it's a Spark job running on Yarn), but probably because JVM sees all the available cores, it uses quite large number of parallel threads, from 18 to 23. Does it really matter? Should I set the number of parallel threads manually?

Upvotes: 3

Views: 1928

Answers (1)

Eugene
Eugene

Reputation: 120848

Here is a rather interesting observation, first (at least on jdk-15).

Suppose this code:

public class Sandbox {
      
    public static void main(String[] args) {

        while (true) {
           LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10_000));
       }

    }
  
}

And I run it with: java -XX:ActiveProcessorCount=1 Sandbox.java. Connect to it via: jcmd <PID> VM.flags and notice a rather interesting thing: -XX:+UseSerialGC. Since you have specified a single CPU, there is no point in using the G1GC according to the JVM (which makes sense). So be prepared.


You can what you want via : -XX:ActiveProcessorCount= and this is how many CPUs your JVM will see (and most probably build heuristics internally based on that).

There are two types of threads that G1 uses : ParallelGCThreads and ConcGCThreads, the fulfill different purposes.

If I do the same exercise from the above (start a process and connect to it, but also enabled -XX+UseG1GC), I see that ParallelGCThreads is not changed (defaults to 10), neither is ConcGCThreads (which defaults to 3); which to me is a surprise. Or not. It depends on how you look at it - the VM tried to prohibit usage of G1GC to begin with.

Of course 10 parallel threads competing for 1 CPU isn't a great way to set-up your VM.

Upvotes: 1

Related Questions