How to prevent Java from exceeding the container memory limits?

后端 未结 2 1497
鱼传尺愫
鱼传尺愫 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: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!

提交回复
热议问题