gpt4 book ai didi

java - 为什么我的 Java 进程在 docker 容器内消耗内存是主机的两倍

转载 作者:搜寻专家 更新时间:2023-11-01 03:16:14 26 4
gpt4 key购买 nike

我遇到了一个有趣的问题,试图分析在 docker 容器和主机上运行的 Java 应用程序的内存消耗。

  1. Java 应用程序是 Jetty 服务器 9.4.9 上的网络应用程序
  2. Java 版本:1.8
  3. 主持人:MAC
  4. Docker 镜像:jetty:9.4-jre8
  5. docker 守护进程是 18.03.1-ce 版本。

在主机上我正在使用 Yourkit 工具来分析内存消耗。

对于 docker 容器 docker stats <docker id/name>

我得到的是,在 MAC 上 yourkit 向我显示 50M 非堆大小 + ~40M 堆大小,总计 ~100M

enter image description here

然而,当我在容器上部署和运行相同的 war 时,统计数据显示 200M

CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
879fb113ca8d jetty-app 0.19% 214.6MiB / 1.952GiB 10.74% 1.49MB / 88.9kB 31.7MB / 6.42MB 29

任何人都可以阐明这种现象吗?

假设stats提供了错误的结果,我尝试使用 --memory 限制容器上的内存标志没有多大帮助,我遇到了 OOM。

提前致谢

最佳答案

您可能想再次尝试测量,使用 openJDK 8u212或更多(2019 年 4 月 16 日)。 (没有 Oracle JDK,因为 their license has changed)

请参阅 Docker support in Java 8 — finally! 中的“Grzegorz Kocur” .
现在:

There is no need to use any hacky workarounds in a docker entrypoint, nor setting Xmx as fixed value anymore.

Docker support was also backported to Java 8.
Let’s check the newest openjdk image tagged as 8u212. We’ll limit the memory to 1G and use 1 CPU:

docker run -ti --cpus 1 -m 1G openjdk:8u212-jdk

您可以使用新标志(已存在于 Java 10+ 中,但现在已移植回 Java 8)和 explained here 微调堆大小。 .

-XX:InitialRAMPercentage
-XX:MaxRAMPercentage
-XX:MinRAMPercentage

If for some reason the new JVM behaviour is not desired it can be switched off using -XX:-UseContainerSupport.

关于java - 为什么我的 Java 进程在 docker 容器内消耗内存是主机的两倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719514/

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