一IDE
Eclipse 和IDEA中配置简单,在 Run Configurations 中 VM Options 中配置即可,大同小异。
二 运行 jar 包
java -Xms100m -Xmx100m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar demo.jar
三 Tomcat 运行 war 包
# Linux 下tomcat/bin/catalina.sh 添加
JAVA_OPTS=“-Xms512M -Xmx1024M”
# windows下 catalina.bat 添加
set “JAVA_OPTS=-Xms512M -Xmx1024M”
四 程序运行过程中
# 设置Boolean类型参数
jinfo -flag [+|-]
# 设置非Boolean类型参数
jinfo -flag =
五实战——运行过程中,哪些参数选项可以被修改
E:JVMDemo3>jps
7408 Jps
7300 RemoteMavenServer36
8356 Launcher
10364 OOMTest
7356
# 查看某个数值型参数
E:JVMDemo3>jinfo -flag InitialHeapSize 10364
-XX:InitialHeapSize=251658240
# 运行时,InitialHeapSize 参数不能被修改
E:JVMDemo3>jinfo -flag InitialHeapSize=10000 10364
Exception in thread "main" com.sun.tools.attach.AttachOperationFailedException: flag 'InitialHeapSize' cannot be changed
at sun.tools.attach.WindowsVirtualMachine.execute(WindowsVirtualMachine.java:117)
at sun.tools.attach.HotSpotVirtualMachine.executeCommand(HotSpotVirtualMachine.java:261)
at sun.tools.attach.HotSpotVirtualMachine.setFlag(HotSpotVirtualMachine.java:234)
at sun.tools.jinfo.JInfo.flag(JInfo.java:134)
at sun.tools.jinfo.JInfo.main(JInfo.java:81)
# 查看某个布尔型参数
E:JVMDemo3>jinfo -flag UseG1GC 10364
-XX:-UseG1GC
# 运行时,UseG1GC 参数不能被修改
E:JVMDemo3>jinfo -flag +UseG1GC 10364
Exception in thread "main" com.sun.tools.attach.AttachOperationFailedException: flag 'UseG1GC' cannot be changed
at sun.tools.attach.WindowsVirtualMachine.execute(WindowsVirtualMachine.java:117)
at sun.tools.attach.HotSpotVirtualMachine.executeCommand(HotSpotVirtualMachine.java:261)
at sun.tools.attach.HotSpotVirtualMachine.setFlag(HotSpotVirtualMachine.java:234)
at sun.tools.jinfo.JInfo.flag(JInfo.java:140)
at sun.tools.jinfo.JInfo.main(JInfo.java:81)
# 运行中能被修改的参数很少,仅下面这些
E:JVMDemo3>java -XX:+PrintFlagsFinal -version|find "manageable"
intx CMSAbortablePrecleanWaitMillis = 100 {manageable}
intx CMSTriggerInterval = -1 {manageable}
intx CMSWaitDuration = 2000 {manageable}
bool HeapDumpAfterFullGC = false {manageable}
bool HeapDumpBeforeFullGC = false {manageable}
bool HeapDumpOnOutOfMemoryError = false {manageable}
ccstr HeapDumpPath = {manageable}
uintx MaxHeapFreeRatio = 100 {manageable}
uintx MinHeapFreeRatio = 0 {manageable}
bool PrintClassHistogram = false {manageable}
bool PrintClassHistogramAfterFullGC = false {manageable}
bool PrintClassHistogramBeforeFullGC = false {manageable}
bool PrintConcurrentLocks = false {manageable}
bool PrintGC = false {manageable}
bool PrintGCDateStamps = false {manageable}
bool PrintGCDetails = false {manageable}
bool PrintGCID = false {manageable}
bool PrintGCTimeStamps = false {manageable}
java version "1.8.0_251"
Java(TM) SE Runtime Environment (build 1.8.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 25.251-b08, mixed mode)
# 运行中,HeapDumpAfterFullGC 可以被修改
E:JVMDemo3>jinfo -flag HeapDumpAfterFullGC 10364
-XX:-HeapDumpAfterFullGC
E:JVMDemo3>jinfo -flag +HeapDumpAfterFullGC 10364
E:JVMDemo3>jinfo -flag HeapDumpAfterFullGC 10364
-XX:+HeapDumpAfterFullGC
# 运行中,HeapDumpPath 可以被修改
E:JVMDemo3>jinfo -flag HeapDumpPath 10364
-XX:HeapDumpPath=
E:JVMDemo3>jinfo -flag HeapDumpPath=d:/ 10364
E:JVMDemo3>jinfo -flag HeapDumpPath 10364
-XX:HeapDumpPath=d:/