[英]Java memory settings in a docker container
I have a Java application, based on Java8 OpenJDK.我有一个基于 Java8 OpenJDK 的 Java 应用程序。 It's running in a docker container which has a limit of 20GB.
它在 docker 容器中运行,该容器的限制为 20GB。
The -Xms and -Xmx settings of tomcat is set as follows: tomcat的-Xms和-Xmx设置如下:
-Xms = 60% of the container memory (as dictated by the cgroup) - so 12GB -Xmx = 80% of the container memory (as dictated by the cgroup) - so 16GB -Xms = 容器 memory 的 60%(由 cgroup 指定) - 所以 12GB -Xmx = 容器 memory 的 80%(由 cgroup 指定) - 所以 16GB
This leaves 4GB free on the container, which is usually fine, but sometimes under load I see the docker container exited (and java process killed) with OOM due to the fact container memory usage has exceeded 20GB.这会在容器上留下 4GB 空闲空间,这通常很好,但有时在负载下我看到 docker 容器退出(并且 java 进程被杀死),由于容器 ZCD69B4957F06CD818D7BF3D6208 使用量超过了 OOM,导致 OOM
I know that the -Xmx settings is for the heap, and not the whole Java process and JVM, therefore would expect that the 4GB 'headroom' on the container is enough, but it appears not.我知道 -Xmx 设置是针对堆的,而不是整个 Java 进程和 JVM,因此预计容器上的 4GB“净空”就足够了,但似乎还不够。
I know all use cases are wildly different, but my question is whether, in general terms, setting the -Xmx setting is too high for a container whose memory limit is 20GB.我知道所有用例都大相径庭,但我的问题是,一般而言,对于 memory 限制为 20GB 的容器,设置 -Xmx 设置是否太高。
I was toying with the idea of using the MaxRAM setting, which again, I know only dictates the heap memory, but I'm unsure whether that would have any impact in positive terms?我在玩弄使用 MaxRAM 设置的想法,我知道这只是决定堆 memory,但我不确定这是否会产生积极影响?
Is it generally the case that you use either MaxRAM or -Xmx, or is there any benefit to setting both?通常情况下您使用 MaxRAM 或 -Xmx,还是同时设置两者有什么好处?
If I were to use MaxRAM instead of -Xmx, how would java allocate memory to the heap?如果我使用 MaxRAM 而不是 -Xmx,java 将如何将 memory 分配给堆? Is there a simple algorithm for this, for example, 50% of the MaxRAM setting?
是否有一个简单的算法来解决这个问题,例如 MaxRAM 设置的 50%? Will java manage memory any more efficiently doing it that way?
java 会更有效地管理 memory 吗?
whether, in general terms, setting the -Xmx setting is too high for a container whose memory limit is 20GB
一般而言,对于 memory 限制为 20GB 的容器,设置 -Xmx 设置是否过高
It depends.这取决于。 An application can use less RAM than the specified
-Xmx
, as well as 2x or 3x more RAM than the specified -Xmx
.应用程序可以使用比指定的
-Xmx
更少的 RAM,以及比指定的-Xmx
多 2 倍或 3 倍的 RAM。 I've seen many applications of both kinds.我见过很多这两种应用。
See what takes memory in a Java process .在 Java 过程中查看 memory 需要什么。
Instead of trying to guess an appropriate heap size basing on the given container limit (those can be completely unrelated), why don't you set -Xmx
to the value, which is comfortable enough for your particular application?与其尝试根据给定的容器限制(这些限制可能完全不相关)来猜测适当的堆大小,不如将
-Xmx
设置为该值,这对于您的特定应用程序来说已经足够舒适了? I mean, if your application works fine with just 8 GB heap, there is no need to give it more, even if the container permits.我的意思是,如果您的应用程序仅使用 8 GB 堆就可以正常工作,那么即使容器允许,也无需提供更多。
Is it generally the case that you use either MaxRAM or -Xmx, or is there any benefit to setting both?
通常情况下您使用 MaxRAM 或 -Xmx,还是同时设置两者有什么好处?
Setting both is meaningless, as Xmx
overrides MaxRAM
.两者都设置是没有意义的,因为
Xmx
会覆盖MaxRAM
。
I were to use MaxRAM instead of -Xmx, how would java allocate memory to the heap?
我要使用 MaxRAM 而不是 -Xmx,java 如何将 memory 分配给堆? Is there a simple algorithm for this, for example, 50% of the MaxRAM setting?
是否有一个简单的算法来解决这个问题,例如 MaxRAM 设置的 50%?
See What is the difference between xmx and MaxRAM JVM parameters?请参阅xmx 和 MaxRAM JVM 参数有什么区别?
Will java manage memory any more efficiently doing it that way?
java 会更有效地管理 memory 吗?
No. MaxRAM
only affects calculation of the heap size and the default garbage collector (when not explicitly specified). MaxRAM
仅影响堆大小和默认垃圾收集器的计算(未明确指定时)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.