-XX:MaxRAMFraction = 1对于在容器环境中进行生产是否安全? [英] Is -XX:MaxRAMFraction=1 safe for production in a containered environment?
问题描述
Java 8/9带来了对-XX:+UseCGroupMemoryLimitForHeap
(带有-XX:+UnlockExperimentalVMOptions
)的支持.这会将-XX:MaxRAM
设置为cgroup内存限制.默认情况下,JVM分配大约25%的最大RAM,因为-XX:MaxRAMFraction
默认为4.
Java 8/9 brought support for -XX:+UseCGroupMemoryLimitForHeap
(with -XX:+UnlockExperimentalVMOptions
). This sets -XX:MaxRAM
to the cgroup memory limit. Per default, the JVM allocates roughly 25% of the max RAM, because -XX:MaxRAMFraction
defaults to 4.
示例:
MaxRAM = 1g
MaxRAMFraction = 4
JVM is allowed to allocate: MaxRAM / MaxRAMFraction = 1g / 4 = 256m
仅使用25%的配额对于部署(通常)由单个JVM进程组成的部署来说似乎是浪费.因此,现在人们设置了-XX:MaxRAMFraction=1
,因此理论上允许JVM使用100%的MaxRAM.
Using only 25% of the quota seems like waste for a deployment which (usually) consists of a single JVM process. So now people set -XX:MaxRAMFraction=1
, so the JVM is theoretically allowed to use 100% of the MaxRAM.
对于1g的示例,这通常导致堆大小约为900m.这似乎有点高-JVM或其他东西(如远程Shell或进程外任务)没有太多可用空间.
For the 1g example, this often results in heap sizes around 900m. This seems a bit high - there is not a lot of free room for the JVM or other stuff like remote shells or out-of-process tasks.
那么该配置(-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1
)对于产品甚至最佳实践是否被认为是安全的?还是我还是应该手动选择-Xmx
,-Xms
,-Xss
等?
So is this configuration (-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=1
) considered safe for prod or even best practice? Or should I still hand pick -Xmx
, -Xms
, -Xss
and so on?
推荐答案
我们进行了一些简单的测试,结果表明,设置-XX:MaxRAM=$QUOTA
和-XX:MaxRAMFraction=1
会导致装入的容器被杀死. JVM分配了超过900M的堆,这太多了. -XX:MaxRAMFraction=2
似乎很安全.
We did some simple testing which showed that setting -XX:MaxRAM=$QUOTA
and -XX:MaxRAMFraction=1
results in killed containers under load. The JVM allocates more than 900M heap, which is way too much. -XX:MaxRAMFraction=2
seems safe(ish).
请记住,您可能希望为其他进程留出空间,例如在容器中获取调试shell(docker exec
)或诊断程序.
Keep in mind that you may want to leave headroom for other processes like getting a debug shell (docker exec
) or diagnostics in the container.
我们已在文章.货币报价:
TL'DR: Java内存管理和配置仍然很复杂.自Java 9/8u131起,尽管JVM可以读取cgroup内存限制并相应地调整内存使用量,但这并不是万灵药.您需要知道
-XX:+UseCGroupMemoryLimitForHeap
的作用,并且需要为每个部署微调一些参数.否则,您可能会在最坏的时间浪费资源和金钱,或者杀死集装箱.-XX:MaxRAMFraction=1
特别危险. Java 10+带来了许多改进,但仍需要手动配置.为了安全起见,请对您的东西进行负载测试.
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.
和
最优雅的解决方案是升级到Java 10+. Java 10弃用了
-XX:+UseCGroupMemoryLimitForHeap
(11)并引入了-XX:+UseContainerSupport
(12),它将取代它.它还引入了-XX:MaxRAMPercentage
(13),其值在0到100之间.这允许对允许JVM分配的RAM数量进行精细控制.由于默认情况下启用了+UseContainerSupport
,因此所有操作均应立即可用.
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.
我们已经写了一些关于-XX:+UseContainerSupport
Edit #2: we've written a little bit more about -XX:+UseContainerSupport
Java 10引入了
+UseContainerSupport
(默认情况下启用),它使JVM在容器环境中使用合理的默认值.自8u191以来,此功能已反向移植到Java 8,这可能允许大量野生Java部署正确配置其内存.
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.
这篇关于-XX:MaxRAMFraction = 1对于在容器环境中进行生产是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!