容器内Tomcat的JVM内存配置

1、
JVM内存分配机制

编写测试Demo

1
2
3
4
5
6
7
8
9
10
docker run -itd -p 8080:8080 tomcat:8.5.29
docker cp demo/ cas233af:/usr/local/tomcat/webapps
docker restart cas233af
curl http://127.0.0.1:8080/demo/jvm/hello
{"totalMemory":"124 M","maxMemory":"405 M","freeMemory":"72 M"}

[root@localhost tomcatjvm]# free -m
total used free shared buff/cache available
Mem: 1823 408 1028 8 386 1198
Swap: 2047 0 2047

3、测试
基于社区的Tomcat进行测试

4、修改
容器启动时,获取该容器的内存,并进行修改JAVA的最大可用内存,下图为tomcat的内存配置

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
limit_in_bytes=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)
echo $limit_in_bytes
# If not default limit_in_bytes in cgroup
if [ "$limit_in_bytes" -ne "9223372036854771712" ]
then
limit_in_megabytes=`expr ${limit_in_bytes} / 1048576`
echo $limit_in_megabytes
heap_size=$limit_in_megabytes
export JAVA_OPTS="-Xms${heap_size}m -Xmx${heap_size}m $JAVA_OPTS"
fi
exec /usr/local/tomcat/bin/catalina.sh run