RamPrakash
RamPrakash

Reputation: 3234

Java heap inside docker container

I ran this command which I found in this blog.

docker run -m 1GB openjdk:10 java \
-XX:+UseContainerSupport \
-XX:MinRAMPercentage=50 \
-XX:MaxRAMPercentage=80 \
-XshowSettings:vm \
-version

My output is this.

VM settings:
    Max. Heap Size (Estimated): 3.86G
    Using VM: OpenJDK 64-Bit Server VM

openjdk version "10.0.2" 2018-07-17
OpenJDK Runtime Environment (build 10.0.2+13-Debian-2)
OpenJDK 64-Bit Server VM (build 10.0.2+13-Debian-2, mixed mode)

I am little bit confused now is that , when I limit the memory to 1G, why does Java see 3.86G or What needs to be done to make java see container memory limits ?

Note: I am on MAC. docker creates container inside the VM. So not sure if it matters.

Upvotes: 2

Views: 1848

Answers (1)

Fritz Duchardt
Fritz Duchardt

Reputation: 11860

When using Java in containers, one needs to be mindful of to what degree the JVM is aware of the resource limits set for the container it runs in. With Java version 10 (released in 2018) the JVM was first enabled to figure out whether it is running in a container, and if yes, how much memory was allocated to the container. Back then, CGroups V1 was used by the Linux Kernel to curtail the memory available to containers.

In the mean time, CGroups V2 (conceived in 2016) is gaining traction and is increasingly becoming the default for new Linux releases (e.g. Ubuntu and Flatcar Linux made it default in 2021). This means, once again a Java update is required to support CGroups V2 for correct handling of JVM resource limits and Heap configuration, which for OpenJDK came with Java version 15.

Upvotes: 3

Related Questions