赞
踩
本文分享自华为云社区《Java应用容器化参数配置最佳实践》,作者:可以交个朋友。
当你在物理机或者虚拟机上配置 JVM 参数时,JVM会默认使用主机上1/4的内存作为堆内存,你也可以选择使用-Xmx/-Xms 来指定 Java 堆内存大小。在容器化环境中,每个容器实例的内存大小由Cgroups配置决定,而低版本JVM对Cgroups的支持是不太友好的。本文主要探讨JVM最佳参数配置。
1.使用jdk 1.8.0_121版本镜像启动容器实例,不指定参数情况下,无法识别Cgroups内存限制,使用主机1/4的内存作为最大堆内存
- [root@172 ~]# free -m
-
- total used free shared buff/cache available
-
- Mem: 7196 2557 299 117 4338 4219
-
- Swap: 0 0 0
-
- [root@172 ~]# docker run -m 512Mi openjdk:8u121 java -XshowSettings:vm -version VM
-
- VM settings:
-
- Max. Heap Size (Estimated): 1.56G
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_121"
-
- OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-1~bpo8+1-b13)
-
- OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
2.使用-Xms和-Xmx指定初始堆内存和最大堆内存,jvm能正常识别
- [root@172 ~]# docker run -m 512Mi openjdk:8u121 java -Xms512m -Xmx512m -XshowSettings:vm -version VM
-
- VM settings:
-
- Min. Heap Size: 512.00M
-
- Max. Heap Size: 512.00M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_121"
-
- OpenJDK Runtime Environment (build 1.8.0_121-8u121-b13-1~bpo8+1-b13)
-
- OpenJDK 64-Bit Server VM (build 25.121-b13, mixed mode)
1.使用jdk 1.8.0_131版本镜像启动容器,不指定参数,无法识别Cgroups内存限制,使用主机1/4的内存作为最大堆内存
- [root@172 ~]# docker run -m 512Mi openjdk:8u131 java -XshowSettings:vm -version VM
-
- VM settings:
-
- Max. Heap Size (Estimated): 1.56G
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_131"
-
- OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2-b11)
-
- OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
2.使用-Xms和-Xmx指定初始堆内存和最大堆内存,jvm能正常识别
- [root@172 ~]# docker run -m 512Mi openjdk:8u131 java -Xms512m -Xmx512m -XshowSettings:vm -version VM
-
- VM settings:
-
- Min. Heap Size: 512.00M
-
- Max. Heap Size: 512.00M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_131"
-
- OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2-b11)
-
- OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
3.在jdk 1.8.0_131版本开始,加入了两个新参数-XX:+UnlockExperimentalVMOptions和-XX:+UseCGroupMemoryLimitForHeap来动态感知容器的Cgroups内存限制,最大堆内存为Cgroups内存限制的1/4
- [root@172 ~]# docker run -m 512Mi openjdk:8u131 java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XshowSettings:vm -version VM
-
- VM settings:
-
- Max. Heap Size (Estimated): 114.00M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_131"
-
- OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2-b11)
-
- OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
4.新参数-XX:+UnlockExperimentalVMOptions和-XX:+UseCGroupMemoryLimitForHeap虽然能动态感知Cgroups内存限制,但是却只能使用1/4,无法修改。此时可以使用另外两个参数-XX:MaxRAMFraction和-XX:MinRAMFraction,参数值必须为整数,取值参考如下表格:
MaxRAMFraction/MinRAMFraction取值 | Cgroups内存限制的百分比 |
---|---|
1 | 90% |
2 | 50% |
3 | 33% |
4 | 25% |
- [root@172 ~]# docker run -m 512Mi openjdk:8u131 java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -XX:MaxRAMFraction=2 -XshowSettings:vm -version VM
-
- VM settings:
-
- Max. Heap Size (Estimated): 228.00M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_131"
-
- OpenJDK Runtime Environment (build 1.8.0_131-8u131-b11-2-b11)
-
- OpenJDK 64-Bit Server VM (build 25.131-b11, mixed mode)
最大堆内存为Cgroups内存限制的50%,符合预期
1.使用jdk 1.8.0_191版本镜像启动容器,不指定参数,jvm能动态感知Cgroups内存限制,最大堆内存为Cgroups内存限制的1/4
- [root@172 ~]# docker run -m 512Mi openjdk:8u191-alpine java -XshowSettings:vm -version VM
-
- VM settings:
-
- Max. Heap Size (Estimated): 123.75M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_191"
-
- OpenJDK Runtime Environment (IcedTea 3.10.0) (Alpine 8.191.12-r0)
-
- OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
2.使用-Xms和-Xmx指定初始堆内存和最大堆内存,符合预期
- [root@172 ~]# docker run -m 512Mi openjdk:8u191-alpine java -Xms512m -Xmx512m -XshowSettings:vm -version VM
-
- VM settings:
-
- Min. Heap Size: 512.00M
-
- Max. Heap Size: 512.00M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_191"
-
- OpenJDK Runtime Environment (IcedTea 3.10.0) (Alpine 8.191.12-r0)
-
- OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
3.jdk 1.8.0_191版本开始,-XX:MaxRAMFraction和-XX:MinRAMFraction被弃用,使用MaxRAMPercentage和MinRAMPercentage来修改堆内存在Cgroups内存限制的占比,参数值是Double类型必须带小数点
- [root@172 ~]# docker run -m 512Mi openjdk:8u191-alpine java -XX:MaxRAMPercentage=50.0 -XX:MinRAMPercentage=50.0 -XshowSettings:vm -version VM
-
- VM settings:
-
- Max. Heap Size (Estimated): 247.50M
-
- Ergonomics Machine Class: server
-
- Using VM: OpenJDK 64-Bit Server VM
-
- openjdk version "1.8.0_191"
-
- OpenJDK Runtime Environment (IcedTea 3.10.0) (Alpine 8.191.12-r0)
-
- OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
以上仅适用于容器采用Cgroups v1版本,在Cgroups v2版本中jdk需要1.8.0_372、11.0.16及更高版本才能动态感知Cgroups的内存限制
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。