Java in a Container: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(26 intermediate revisions by the same user not shown)
Line 2: Line 2:


* https://developers.redhat.com/blog/2017/03/14/java-inside-docker/
* https://developers.redhat.com/blog/2017/03/14/java-inside-docker/
* https://blog.softwaremill.com/docker-support-in-new-java-8-finally-fd595df0ca54
* https://stackoverflow.com/questions/54292282/clarification-of-meaning-new-jvm-memory-parameters-initialrampercentage-and-minr/54297753#54297753
* Andrei Pangin - Memory Footprint of a Java Process https://vimeo.com/364039638


=Internal=
=Internal=


* [[Docker_Container_Best_Practices#Java_in_a_Container|Docker Container Best Practices]]
* [[Docker_Container_Best_Practices#Java_in_a_Container|Docker Container Best Practices]]
* [[Java Memory]]
* [[Kubernetes Resource Management Concepts]]
* [[Docker Resource Management Concepts]]


=Overview=
=Overview=


When running java in a container, the max heap should always be set with -Xmx, based on the application needs and the container limits.  
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.  
Java max heap limit can also be calculated based on the container limits.  


See https://github.com/fabric8io-images/run-java-sh
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=2 -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. Effectively, this option sets -XX:MaxRAM to the CGroups limit. By default, without any other configuration, the max heap is set to 1/4 of the CGroups value. To initialize max heap to a different value, use <code>-XX:MaxRAMFraction</code>, according to the formula:
 
              MaxRAM
max_heap = -------------
            MaxRAMFraction
 
Using the -XX:MaxRAMFraction options, we can [https://bugs.openjdk.java.net/browse/JDK-8186315|only set fractional values] 1/2, 1/3, 1/4 etc.
 
===Java 1.8.0_221===
 
java -XX:MaxRAMPercentage=20.0 -XX:+PrintFlagsFinal -version | grep MaxHeapSize
 
Other flags:
 
-XX:InitialRAMPercentage
 
-XX:MinRAMPercentage
 
-XX:+UseContainerSupport

Latest revision as of 23:15, 13 January 2021

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=2 -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. Effectively, this option sets -XX:MaxRAM to the CGroups limit. By default, without any other configuration, the max heap is set to 1/4 of the CGroups value. To initialize max heap to a different value, use -XX:MaxRAMFraction, according to the formula:

              MaxRAM 
max_heap = -------------
           MaxRAMFraction

Using the -XX:MaxRAMFraction options, we can set fractional values 1/2, 1/3, 1/4 etc.

Java 1.8.0_221

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

Other flags:

-XX:InitialRAMPercentage
-XX:MinRAMPercentage
-XX:+UseContainerSupport