在单个CPU计算机上的最佳GC线程数量

huangapple go评论59阅读模式
英文:

Optimal number of GC threads for a single CPU machine

问题

我正在使用 G1 垃圾收集器处理一个应用程序,该应用程序应该只使用一个 CPU 核心(它是在 Yarn 上运行的 Spark 作业)。但很可能是因为 JVM 看到了所有可用的核心,它使用了相当多的并行线程,从 18 到 23 个不等。这真的重要吗?我应该手动设置并行线程的数量吗?

英文:

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?

答案1

得分: 1

这里有一个相当有趣的观察,首次出现(至少在jdk-15上)。

假设这段代码:

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

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

    }
  
}

我使用以下命令运行它:java -XX:ActiveProcessorCount=1 Sandbox.java。通过jcmd <PID> VM.flags连接到它,然后注意一个相当有趣的事情:-XX:+UseSerialGC。由于您指定了一个单个 CPU,根据JVM的观点,使用G1GC没有意义(这是有道理的)。所以要有所准备。


您可以通过-XX:ActiveProcessorCount=来指定您想要的内容,这是您的JVM将看到的CPU数量(并且很可能会基于此在内部构建启发式算法)。

G1使用了两种类型的线程:ParallelGCThreadsConcGCThreads,它们有不同的用途。

如果我从上面的示例中进行相同的练习(启动一个进程并连接到它,同时启用了-XX+UseG1GC),我会看到ParallelGCThreads没有更改(默认为10),ConcGCThreads也没有更改(默认为3);对我来说,这是一个惊喜。或者也可以说不是。这取决于您的看法 - 虚拟机首先试图禁止使用G1GC

当然,10个并行线程竞争一个CPU并不是设置您的虚拟机的好方法。

英文:

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.

huangapple
  • 本文由 发表于 2020年10月23日 03:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64489101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定