How to prevent Java from exceeding the container memory limits?

后端 未结 2 1469
鱼传尺愫
鱼传尺愫 2021-02-04 20:51

I\'m running a Java program inside a Docker container that has a hard memory limit of 4GB. I\'ve set the max heap to 3GB but still the Java program exceeds the limit and gets ki

相关标签:
2条回答
  • 2021-02-04 21:13

    In addition to Fabian Rivera's answer I've found that Java 10 has good support for running in containers without any custom startup parameters. By default it uses 25% of the containers memory as heap, which might be a bit low for some users. You can change this with the following parameter:

    -XX:MaxRAMPercentage=50
    

    To play around with Java 10 run the following docker command:

    docker run -it --rm -m1g --entrypoint bash openjdk:10-jdk
    

    It will give you a bash environment where you can run executables from the JDK. For instance, to run a small piece of script you can use jrunscript like this:

    jrunscript -e "print(Packages.java.lang.Runtime.getRuntime().maxMemory()/(1<<20) + 'M')"
    

    This will show you the size of the heap in MB. To change the percentage of total container memory that is used for the heap add the MaxRAMPercentage parameter like this:

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

    Now you can play around with the sizing of the container and the max percentage of heap.

    0 讨论(0)
  • 2021-02-04 21:24

    When a Java application is executed inside a container, the JVM ergonomics (which is responsible for dynamically assign resources based on the host's capabilities) does not know it is running inside a container and it calculates the number of resources to be used by the Java app based on the host that is executing your container. Given that, it does not matter if you set limits to your container, the JVM will take your host's resources as the base for doing that calculation.

    From JDK 8u131+ and JDK 9, there’s an experimental VM option that allows the JVM ergonomics to read the memory values from CGgroups. To enable it you must pass the following flags to the JVM:

    -XX:+UnlockExperimentalVMOptions and -XX:+UseCGroupMemoryLimitForHeap

    If you enable these flags, the JVM will be aware that is running inside a container and will make the JVM ergonomics to calculate the app's resources based on the container limits and not the host's capabilities.

    Enabling the flags:

    $ java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -jar app.jar
    

    You can dynamically pass the JVM options to your container with ENV variables.

    Example:

    The command to run your app would like something like:

     $ java ${JAVA_OPTIONS} -jar app.jar
    

    And the docker run command needs to pass the ENV variable like this:

    $ docker run -e JAVA_OPTIONS="-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap" myJavaImage
    

    Hope this helps!

    0 讨论(0)
提交回复
热议问题