Skip to content

Conversation


## Make JVM respect CPU and RAM limits

On starup JVM tries to detect the number of available CPU cores and the amount of RAM to adjust its internal parameters (like the number of garbage collector threads to spawn) accordingly. When container is run with limited CPU/RAM, standard system API, used by JVM for probing, will return host-wide values. This can cause excessive CPU usage and memory allocation errors with older versions of JVM.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/starup/startup.

@akashche
Copy link
Contributor Author

akashche commented May 7, 2017

@TimWolla , thanks for the correction! Fixed.


On startup JVM tries to detect the number of available CPU cores and the amount of RAM to adjust its internal parameters (like the number of garbage collector threads to spawn) accordingly. When container is run with limited CPU/RAM, standard system API, used by JVM for probing, will return host-wide values. This can cause excessive CPU usage and memory allocation errors with older versions of JVM.

Inside Linux containers, recent versions of OpenJDK 8 can correctly detect container-limited number of CPU cores by default. To enable the detection of container-limited amount of RAM the following options can be used:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the version we have actually include this new functionality? (I thought we were still a release or two behind due to Debian's backport process? 8u121 in jessie-backports vs 8u131 in stretch)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not yet, openjdk:8-jdk is at 1.8.0_121

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OpenJDK 9 too? (>=b150 afaict)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, it looks like all docker-library jdks 8 are now at least on 8u131 that has cgroups flag, is it possible to have this doc change merged?

$ start /b /wait /affinity 0x3 path/to/java.exe ...
```

In this example CPU affinity hex mask `0x3` will limit JVM to 2 CPU cores.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this limit to 2 CPU cores generally, or does this limit to 2 specific cores? (ie, docker run --cpuset-cpus vs docker run --cpus)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mask 0x3 - 0011 limits CPU to 2 specific cores - core0 and core1, its a windows feature, not jdk-specific.


In this example CPU affinity hex mask `0x3` will limit JVM to 2 CPU cores.

RAM limit is supported by Windows Server containers, but currently JVM cannot detect it. To prevent excessive memory allocations, `-XX:MaxRAM=...` option must be specified with the value that is not bigger than a containers RAM limit.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooh, nice -- I had no idea there was a JVM parameter for controlling MaxRAM rather than trying to set -Xmx and friends intelligently! (related to docker-library/openjdk#57 and docker-library/openjdk#71)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, MaxRAM may be more convenient. This one-liner can be used to check what Xmx (max heap) value will be used when MaxRAM is specified:

jrunscript -J-XX:MaxRAM=256M -e "print(Packages.java.lang.Runtime.getRuntime().maxMemory()/(1<<20) + 'M')"

@tianon tianon merged commit 6ddcede into docker-library:master Aug 3, 2017
@tianon
Copy link
Member

tianon commented Aug 3, 2017

Thanks @akashche!

@workmanw
Copy link

@akashche Thank you for this!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants