Java in a Container: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 27: Line 27:
===Java 1.8.0_181===
===Java 1.8.0_181===


  java -XX:+UnlockExperimentalVMOptions  -XX:+UseCGroupMemoryLimitForHeap -XX:+PrintFlagsFinal -version | | grep MaxHeapSize
(since Java 1.8.0_131)
 
  java -XX:+UnlockExperimentalVMOptions  -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=50 -XX:+PrintFlagsFinal -version | | grep MaxHeapSize
 
<code>-XX:+UseCGroupMemoryLimitForHeap</code> configures the JVM to read [[Linux_cgroups#Memory_Limit|CGroups memory limit]] from /sys/fs/cgroup/memory/memory.limit_in_bytes and use it as a base when setting the max heap.


===Java 1.8.0_221===
===Java 1.8.0_221===

Revision as of 20:29, 6 November 2019

External

Internal

Overview

When running java in a container, the max heap should always be explicitly set with -Xmx, based on the application needs and the container limits.

Java max heap limit can also be calculated based on the container limits.

Generic script that correlates JVM memory settings to the container limits: https://github.com/fabric8io-images/run-java-sh, https://github.com/fabric8io-images/run-java-sh/blob/master/fish-pepper/run-java-sh/fp-files/run-java.sh.


Let JVM Adjust Memory Relative to CGroups Limits

Experimental Options

Java 1.8.0_181

(since Java 1.8.0_131)

java -XX:+UnlockExperimentalVMOptions  -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=50 -XX:+PrintFlagsFinal -version | | grep MaxHeapSize

-XX:+UseCGroupMemoryLimitForHeap configures the JVM to read CGroups memory limit from /sys/fs/cgroup/memory/memory.limit_in_bytes and use it as a base when setting the max heap.

Java 1.8.0_221

java -XX:MaxRAMPercentage=20.0 -XX:+PrintFlagsFinal -version | grep MaxHeapSize

Other flags:

-XX:InitialRAMPercentage
-XX:MinRAMPercentage