Skip to main content

Java Tuning

-Xfordefault
-Xmsinitial heap size
-Xmxmax heap size25%,最大25G
-XX:flagfor
InitialRAMPercentage初始 堆内存 比例
MaxRAMPercentage最大 堆内存 比例
MinRAMPercentage最小 堆内存 比例
-Xmx12g -Xms12g -Xmn8g -XX:MaxMetaspaceSize=512m -XX:MaxDirectMemorySize=256M  -XX:+UseContainerSupport -XX:+UseG1GC -XX:MaxGCPauseMillis=200
-XX:+UseContainerSupport -XX:InitialRAMPercentage=70.0 -XX:MaxRAMPercentage=70.0 -XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:/home/admin/nas/gc-${POD_IP}-$(date '+%s').log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/home/admin/nas/dump-${POD_IP}-$(date '+%s').hprof

内存限制

java -XshowSettings:vm -version
java -XX:+PrintFlagsFinal -version

java -XX:+PrintFlagsFinal -version | grep -E 'Container|RAM|HeapSize'
java -XX:+PrintFlagsFinal -XX:MaxRAMPercentage=100.0 -version | grep -E 'Container|RAM'

# limits
cat /sys/fs/cgroup/memory/memory.limit_in_bytes

# 新版本 JAVA
java -Xlog:os+container=trace -version
  • UseContainerSupport
    • 默认开启
    • MaxRAMPercentage=25%
limitMaxRAM
< 1GB50%
1GB < 2GB512MB
> 2GB75%
  • -Xcompressedrefs

    • 3-bit shift
    • heap size 25GB 上限
    • 可通过 -XX:MaxRAMPercentage 或者 -Xmx 覆盖
  • -verbose:sizes


  • -Xmx
  • -XX:MaxRAMPercentage, -XX:MinRAMPercentage, -XX:InitialRAMPercentage
    • Java 8 update 191+
  • -XX:MaxRAMFraction, -XX:MinRAMFraction
    • Java 8 update 131 -> Java 8 update 190
java -XX:MaxRAMPercentage=50 -XshowSettings:vm -version

java -XshowSettings:vm -version
java -Xmx512m -XshowSettings:vm -version
java -XX:+UseContainerSupport -XshowSettings:vm -version
# 需要/1024
cat /sys/fs/cgroup/cpu/cpu.shares

GC