当 运行 在容器内时,Java 8 默认情况下总是会消耗至少 1GB 的 RAM 吗?

Will Java 8 always consume at least 1GB of RAM by default when running inside a container?

我们是 运行 Kubernetes 集群中的 Java 应用程序。应用程序本身对RAM的需求不高,但我注意到它总是消耗1GB。

kubectl top pods
NAME                                                              CPU(cores)   MEMORY(bytes)
my-application-c0ffee                                             100m           1127Mi
my-application-c0ffee                                             100m           1109Mi

当我检查 jcmd <pid> GC.heap_info 容器内部时,我得到以下信息:

def new generation   total 89216K, used 12090K [0x00000000bc200000, 0x00000000c22c0000, 0x00000000d2c00000)
  ...
tenured generation   total 197620K, used 151615K [0x00000000d2c00000, 0x00000000decfd000, 0x0000000100000000)
  ...
Metaspace       used 146466K, capacity 152184K, committed 152576K, reserved 1183744K
  class space    used 18171K, capacity 19099K, committed 19200K, reserved 1048576K

据我了解,默认情况下 Java 保留 1GB 的 虚拟 内存大小用于存储 Class 信息(以便以压缩方式引用它使用 32 位引用这个内存块应该事先保留)。当 运行 在容器外部时这没什么大不了的,因为这个内存实际上并没有 提交 。它只是一个地址 space 保留 .

但是在容器内 运行 的情况下,情况似乎完全不同,其中 reserved 内存变为 committed

这是否意味着容器中的 Java 运行 默认情况下至少会消耗 1GB 的 RAM? 除了显式设置 -XX:CompressedClassSpaceSize?

之外,还有其他方法可以解决这个问题吗?

in case of running inside container, where reserved memory becomes committed

不,保留的内存不会“被提交”。 Virtual Size 和 Resident Set Size 是不同的指标,无论是否在容器中。位于物理内存中的是 RSS。

kubectl top 显示的不是 RSS,而是所谓的“工作集”,它并不总是与实际内存使用情况相匹配。

Does this mean that Java running in a container will by default consume at least 1GB of RAM?

没有

is there any other way to deal with that

这取决于你的目标。如果您想查看实际的容器内存统计信息,请查看 /sys/fs/cgroup/memory/.../memory.statsmemory.usage_in_bytes。或者,如果您使用 docker、运行 docker stats.

如果您想减少进程的虚拟内存,请关闭 -XX:-UseCompressedClassPointers