- docker中安装vim工具
apt-get update
apt-get install vim
- 修改tomcat容器的catalina.sh的配置
#docker exec -it waiqin-web /bin/bash #进入docker容器内
#cd /usr/local/tomcat/bin #进入docker容器内
vim catalina.sh
在cygwin=false 下一行增加
JAVA_OPTS="-server -Xms4096m -Xmx4096m -XX:MaxMetaspaceSize=512M -XX:MetaspaceSize=512M -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicationStoppedTime -XX:+PrintGCDateStamps -XX:ErrorFile=/var/app/gc/hs_err_pid%p.log -XX:HeapDumpPath=/var/app/gc -Xloggc:/var/app/gc/gc%t.log"
image.png
2021年9月29日16:28:04 更新
宿主机内存如下,
[root@localhost WEB-INF]# cat /proc/meminfo
MemTotal: 16266740 kB #共16G
MemFree: 1086588 kB
MemAvailable: 1961628 kB
Buffers: 0 kB
Cached: 1162852 kB
默认情况下,JVM的Max Heap Size是系统内存的1/4,那么JVM将的默认Heap≈4G。
参考这篇文章被kill问题之2:Docker环境下Java应用的JVM设置(容器中的JVM资源该如何被安全的限制)
让 JVM 感知 cgroup 限制
前文提到还有另外一种方法解决 JVM 内存超限的问题,这种方法可以让 JVM 自动感知 docker 容器的 cgroup 限制,从而动态的调整堆内存大小,感觉挺不错的。我们也来试一下这种方法,看看效果如何 ; )
查看下当前tomcat是否支持该参数
-XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap
root@8cf35b633895:/usr/local/tomcat/bin# java -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal
root@cd50041ea188:/usr/local/tomcat# java -XX:+UnlockDiagnosticVMOptions -XX:+UnlockExperimentalVMOptions -XX:+PrintFlagsFinal
[Global flags]
bool AbortVMOnCompilationFailure = false {diagnostic}
bool AbortVMOnSafepointTimeout = false {diagnostic}
intx ActiveProcessorCount = -1 {product}
uintx AdaptiveSizeDecrementScaleFactor = 4 {product}
uintx AdaptiveSizeMajorGCDecayTimeScale = 10 {product}
uintx AdaptiveSizePausePolicy = 0 {product}
uintx AdaptiveSizePolicyCollectionCostMargin = 50 {product}
uintx AdaptiveSizePolicyInitializingSteps = 20 {product}
uintx AdaptiveSizePolicyOutputInterval = 0 {product}
uintx AdaptiveSizePolicyWeight = 10 {product}
uintx AdaptiveSizeThroughPutPolicy = 0 {product}
uintx AdaptiveTimeWeight = 25 {product}
bool AdjustConcurrency = false {product}
bool AggressiveHeap = false {product}
bool AggressiveOpts = false {product}
往下翻,找到了要配置的参数,表示当前jvm支持该参数,最终参数如下,/var/jvm/log目录记得创建
JAVA_OPTS="-server -XX:+UseG1GC -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintHeapAtGC -XX:+PrintGCApplicatio
nStoppedTime -XX:+PrintGCDateStamps -XX:ErrorFile=/var/jvm/log/hs_err_pid%p.log -XX:HeapDumpPath=/var/jvm/log -Xloggc:/var/jvm/log/gc%t.log"
加到Catalina.sh中
把这个容器制作一个本地镜像,
[root@localhost webapps]# docker commit -a "fengjian" -m "tomcat8+jdk8+jvm参数优化" cd50041ea188 tomcat:8.5.70-jdk8-jvmG1
使用jmap 命令的坑
进入到容器之后,想查看容器使用的内存情况,报错如下
root@8cf35b633895:/usr/local/tomcat# jmap -heap 1
Attaching to process ID 1, please wait...
ERROR: ptrace(PTRACE_ATTACH, ..) failed for 1: Operation not permitted
Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 1: Operation not permitted
sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 1: Operation not permitted
参考Docker 中无法使用 JDK jmap之 Can't attach to the process: ptrace(PTRACE_ATTACH问题
在启动容器时增加参数
docker run --cap-add=SYS_PTRACE
完整启动如下
docker run --name central-kitchen-kxg -d -p 9093:8080 -v /home/dockerms/data/central-kitchen-kxg/webapps:/usr/local/tomcat/webapps -e TZ=Asia/Shanghai --cap-add=SYS_PTRACE tomcat:8.5.70-jdk8-jvmG1