If you use the official Java image or Docker image based on Java image, you can easily set the memory parameters of JVM by passing the JAVA_OPTS environment variable. For example, for a formal tomcat image, we can execute the following command to start a Tomcat instance with a maximum memory of 5 12M.
docker run-RM-e JAVA _ OPTS = '-xmx 5 12m ' Tomcat:8
In the log, we can clearly find that the setting has taken effect "command line parameter: -xmx5 12m".
Automatic stacking
The source code can be obtained from Github. It is based on Docker's official Tomcat image, and its startup script will check the memory limit in CGroup and calculate the maximum heap size passed to Tomcat by JVM. Its code is as follows
#! /bin/bash
Limit _ in _ bytes = $ (cat/sys/fs/cgroup/memory/memory/Denver dino/Tomcat: 8-automatic heap
Through the following command, we can detect from the log that the corresponding JVM parameter has been set to 448MB (5 12-64).
Docker log test
...
02-apr-201614:18: 09.870 info [main] org.apache.catalina.startup.version logger listener.log Command line parameters:-xmx448m.
...
We can also easily adjust the memory of Java applications.
Docker 1. 10 provides the ability to dynamically modify the container resource limit. However, because the JVM is not aware of the modification of container resources, we still need to restart tomcat to change the JVM's memory settings. For example, we can adjust the container memory limit to 1GB by the following command.
Docker update -m 1024m test
Docker restart test
Check the log again, and the corresponding maximum JVM heap size has been set to 960MB.
Docker log test
...
02-apr-201614: 21:07.644info [main] org.apache.catalina.startup.version logger listener.log Command line parameters:-xmx960m.