How can I get the guest total available memory from inside a Docker container?

3/17/2021

We have a Linux Docker container run on Kubernetes. We would like to set appropriate Xms and Xmx such as 75% of total available memory in the container.

However, it seems that /proc/meminfo is not namespaced (gets host memory instead of guest one), and that the cgroup memory limits are off the chart, meminfo returns correct on macos but likely will return total host memory on Linux host.

memory.limit_in_bytes
9223372036854771712

Do you have any idea how to correctly get the total memory available inside a Docker container in Kubernetes?

is there any env var?

Do you have any experience how to appropriately set Xmx directly or indirectly in a Docker in Kubernetes, so that it uses say 75% of the total available memory that is allocated to that container say 1.5G?

-- NicuMarasoiu
docker
java
kubernetes
linux
unix

1 Answer

3/17/2021

By finding out new flags in Java 10+, i solved my need in a different way: by not passing Xms or Xmx to the JVM, but the percentages below:

https://merikan.com/2019/04/jvm-in-a-container/#java-10

https://blog.arkey.fr/2020/10/27/maxrampercentage-is-not-what-i-wished-for/

https://github.com/openjdk/jdk11u/blob/master/src/hotspot/share/runtime/arguments.cpp -> set_heap_size function

percentage_of_memory_to_use_for_heap=${HEAP_SIZE_PERCENTAGE:-75}
java -XshowSettings:vm -XX:+UseContainerSupport \
  -XX:InitialRAMPercentage=$((percentage_of_memory_to_use_for_heap)) \
  -XX:MinRAMPercentage=$((percentage_of_memory_to_use_for_heap)) \
  -XX:MaxRAMPercentage=$((percentage_of_memory_to_use_for_heap)) \
-- NicuMarasoiu
Source: StackOverflow