默认垃圾收集器 Hotspot JVM 11/17 的标准

问题描述 投票:0回答:2

我找到一个 source 描述使用的默认 gc 根据可用资源而变化。似乎 jvm 使用 g1gc 或串行 gc 取决于硬件和操作系统。

在某些硬件和操作系统配置上默认选择串行收集器

有人可以指出更详细的资料来源,说明具体标准是什么,以及如何在码头化/kubernetes 环境中应用。 换句话说:

可以将 k8s 中 pod 的资源请求设置为例如。 1500 mCpu 使 jvm 使用串行 gc 并更改为 2 Cpu 将默认 gc 更改为 g1gc?何时使用哪个 gc 的限制是否会根据 jvm 版本(11 对 17)而变化?

java garbage-collection jvm java-11 java-17
2个回答
14
投票

在 JDK 11 和 17

Serial
收集器在只有一个 CPU 可用时使用。否则
G1
被选中

如果限制容器可用的 CPU 数量,JVM 会选择

Serial
而不是默认的
G1

JDK11

1 个中央处理器

docker run --cpus=1 --rm -it eclipse-temurin:11 java -Xlog:gc* -version
[0.004s][info][gc] Using **Serial**

它使用

Serial

不止一个CPU

docker run --cpus=2 --rm -it eclipse-temurin:11 java -Xlog:gc* -version
[0.008s][info][gc     ] Using G1

它使用

G1

JDK17

1 个中央处理器

docker run --cpus=1 --rm -it eclipse-temurin:17 java -Xlog:gc* -version
[0.004s][info][gc] Using Serial

它使用

Serial

不止一个CPU

docker run --cpus=2 --rm -it eclipse-temurin:17 java -Xlog:gc* -version
[0.007s][info][gc] Using G1

它使用

G1


13
投票

在当前的 OpenJDK 中,“服务器类机器”默认选择 G1 GC,否则为 Serial GC。 “服务器类机器”被定义为具有 2 个或更多非 HT CPU 和 2 个或更多 GiB RAM 的系统。

具体算法见src/hotspot/share/runtime/os.cpp:

// This is the working definition of a server class machine:
// >= 2 physical CPU's and >=2GB of memory, with some fuzz
// because the graphics memory (?) sometimes masks physical memory.
// If you want to change the definition of a server class machine
// on some OS or platform, e.g., >=4GB on Windows platforms,
// then you'll have to parameterize this method based on that state,
// as was done for logical processors here, or replicate and
// specialize this method for each platform.  (Or fix os to have
// some inheritance structure and use subclassing.  Sigh.)
// If you want some platform to always or never behave as a server
// class machine, change the setting of AlwaysActAsServerClassMachine
// and NeverActAsServerClassMachine in globals*.hpp.
bool os::is_server_class_machine() {
  // First check for the early returns
  if (NeverActAsServerClassMachine) {
    return false;
  }
  if (AlwaysActAsServerClassMachine) {
    return true;
  }
  // Then actually look at the machine
  bool         result            = false;
  const unsigned int    server_processors = 2;
  const julong server_memory     = 2UL * G;
  // We seem not to get our full complement of memory.
  //     We allow some part (1/8?) of the memory to be "missing",
  //     based on the sizes of DIMMs, and maybe graphics cards.
  const julong missing_memory   = 256UL * M;

  /* Is this a server class machine? */
  if ((os::active_processor_count() >= (int)server_processors) &&
      (os::physical_memory() >= (server_memory - missing_memory))) {
    const unsigned int logical_processors =
      VM_Version::logical_processors_per_package();
    if (logical_processors > 1) {
      const unsigned int physical_packages =
        os::active_processor_count() / logical_processors;
      if (physical_packages >= server_processors) {
        result = true;
      }
    } else {
      result = true;
    }
  }
  return result;
}
© www.soinside.com 2019 - 2024. All rights reserved.