gpt4 book ai didi

java - 是-XX :MaxRAMFraction=1 safe for production in a containered environment?

转载 作者:IT老高 更新时间:2023-10-28 12:37:16 25 4
gpt4 key购买 nike

Java 8/9 带来了对 -XX:+UseCGroupMemoryLimitForHeap 的支持(使用 -XX:+UnlockExperimentalVMOptions)。这会将 -XX:MaxRAM 设置为 cgroup 内存限制。默认情况下,JVM 分配大约 25% 的最大 RAM,因为 -XX:MaxRAMFraction 默认为 4。

例子:

MaxRAM = 1g
MaxRAMFraction = 4
JVM is allowed to allocate: MaxRAM / MaxRAMFraction = 1g / 4 = 256m

对于(通常)由单个 JVM 进程组成的部署而言,仅使用 25% 的配额似乎是一种浪费。所以现在人们设置-XX:MaxRAMFraction=1,所以理论上允许JVM使用100%的MaxRAM。

对于 1g 示例,这通常会导致堆大小约为 900m。这似乎有点高 - JVM 或其他东西(如远程 shell 或进程外任务)没有太多可用空间。

那么这种配置(-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1)对于产品甚至最佳实践来说是安全的吗?还是我还是应该手动选择-Xmx-Xms-Xss等等?

最佳答案

我们做了一些简单的测试,结果表明设置 -XX:MaxRAM=$QUOTA-XX:MaxRAMFraction=1 会导致容器在负载下被杀死。 JVM 分配了超过 900M 的堆,这太多了。 -XX:MaxRAMFraction=2 看起来很安全(ish)。

请记住,您可能希望为其他进程留出空间,例如在容器中获取调试外壳 (docker exec) 或诊断。


编辑:我们在 article 中详细记录了我们学到的知识。 .金钱报价:

TL'DR:Java memory management and configuration is still complex. Although the JVM can read cgroup memory limits and adapt memory usage accordingly since Java 9/8u131, it’s not a golden bullet. You need to know what -XX:+UseCGroupMemoryLimitForHeap does and you need to fine tune some parameters for every deployment. Otherwise you risk wasting resources and money or getting your containers killed at the worst time possible. -XX:MaxRAMFraction=1 is especially dangerous. Java 10+ brings a lot of improvements but still needs manual configuration. To be safe, load test your stuff.

The most elegant solution is to upgrade to Java 10+. Java 10 deprecates -XX:+UseCGroupMemoryLimitForHeap (11) and introduces -XX:+UseContainerSupport (12), which supersedes it. It also introduces -XX:MaxRAMPercentage (13) which takes a value between 0 and 100. This allows fine grained control of the amount of RAM the JVM is allowed to allocate. Since +UseContainerSupport is enabled by default, everything should work out of the box.


编辑 #2:我们有 written a little bit more关于-XX:+UseContainerSupport

Java 10 introduced +UseContainerSupport (enabled by default) which makes the JVM use sane defaults in a container environment. This feature is backported to Java 8 since 8u191, potentially allowing a huge percentage of Java deployments in the wild to properly configure their memory.

关于java - 是-XX :MaxRAMFraction=1 safe for production in a containered environment?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49854237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com