If you use an official Java image, or a Docker image built on a Java image, you can JAVA_OPTS easily set the JVM's memory parameters by passing environment variables. For example, for an official Tomcat image, we can execute the following command to launch a Tomcat instance with a maximum memory of 512M
Docker run--rm-e java_opts= '-xmx512m ' Tomcat:8
In the log, we can clearly see that the settings are in effect "Command line argument:-xmx512m"
02-apr-2016 12:46:26.970 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version:apache T omcat/8.0.3202-apr-2016 12:46:26.974 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Feb 2 2016 19:34:53UTC02-apr-2016 12:46:26.975 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server number:8.0.32.0 02-apr-2016 12:46:26.975INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS name:linux02-apr-2016 12:46:26.975 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS version:4.1.19-Boot2docker02-apr-2016 12:46:26.975INFO [main] org.apache.catalina.startup.VersionLoggerListener.log architecture:amd6402-apr-2016 12:46:26.975 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:/usr/lib /jvm/java-7-openjdk-amd64/JRE02-apr-2016 12:46:26.976 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM version:1.7.0_95 -b0002-apr-2016 12:46:26.976INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM vendor:oracle Corporation02-apr-2016 12:46:26.977 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log catalina_base:/usr/loc al/Tomcat02-apr-2016 12:46:26.977 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log catalina_home:/usr/loc al/Tomcat02-apr-2016 12:46:26.978 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:- djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties02-apr-2016 12:46:26.978 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:- Djava.util.logging.manager=Org.apache.juli.ClassLoaderLogManager02-apr-2016 12:46:26.978 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:-xmx512m ...
However, when deploying Java container applications on a docker cluster, it is not sufficient to set the JVM's heap parameters, and we also need to limit the memory resources of the Docker container:
1. Limit the maximum amount of memory used by the container to prevent damage to the system or other applications
2. Ability to dispatch Docker containers to a node with enough free memory to ensure the required running resources for the application
For resource allocation constraints on containers, Docker provides the appropriate startup parameters
For memory, the basic thing is to constrain the size of the -m container's memory using parameters.
-M,--memory= "" <NUMBER>[<UNIT>]). Number is a positive integer. Unit can be one of B, K, M, or G. Minimum is 4M.
So the problem is, in order to properly set the size of the Docker container memory, do we need to pass both the container's memory limit and the JAVA_OPTS environment variable? As shown below:
Docker run--rm-m 512m-e java_opts= '-xmx512m ' Tomcat:8
There are two problems with this method
1. Requires the administrator to ensure that the container memory and JVM memory settings match, or may raise an error
2. When adjusting the container memory limit, the environment variable needs to be reset, which requires rebuilding a new container
Is there a way to allow the JVM inside the container to automatically adapt the container's memory limit? This allows for a more unified approach to resource management and simplifies configuration efforts.
You know that Docker is resource constrained through cgroup, and since 1.7, Docker mounts the container's local cgroups as read-only to the file system inside the container, so that we can get inside the container, Cgroups information to obtain the system's resource limit for the current container.
I have created a sample imageregistry.aliyuncs.com/denverdino/tomcat:8-autoheap
, its source code can be obtained from GitHub. It is created based on the Docker official Tomcat image, and its startup script checks the memory limit in Cgroup and calculates the JVM's maximum heap size to be passed to Tomcat. The code is as follows
#!/bin/bashlimit_in_bytes=$ (cat/sys/fs/cgroup/memory/default limit_in_bytes in Cgroupif ["$limit _in_bytes"-ne "9223372036854771712" ]then limit_in_megabytes=$ (expr $ limit_in_bytes \ 1048576) heap_size=$ (expr $limit _in_megabytes- $RESERVED _megabytes) Export Java_opts= "-xmx${heap_size}m $JAVA _opts" echo java_opts=$JAVA _optsfiexec catalina.sh Run
Description
- for monitoring, troubleshooting and other scenarios, we have reserved some memory (default 64M), and the rest of the container memory we have allocated to the JVM heap.
- There is no further processing of the boundary condition. In the production system, it is necessary to make the corresponding setting according to the situation, such as the largest heap size and so on.
Now we start a tomcat running in a container of 512 megabytes
Docker run-d--name test-m 512m registry.aliyuncs.com/denverdino/tomcat:8-autoheap
From the log we can detect that the corresponding JVM parameter has been set to 448MB (512-64) via the following command
Docker logs Test ... 02-apr-2016 14:18:09.870 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:- xmx448m
...
We can also easily adjust the memory of the Java application.
Docker 1.10 provides the ability to dynamically modify container resource limits. However, since the JVM cannot perceive container resource modifications, we still need to restart Tomcat to change the JVM's memory settings, for example, we can adjust the container memory limit to 1GB with the following command
Docker Update-m 1024m testdocker restart test
Check the log again, the corresponding JVM Heap size maximum has been set to 960MB
Docker logs Test ... 02-apr-2016 14:21:07.644 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument:- xmx960m ...
How to set memory limits for Java apps in Docker containers