提示: 本材料只做个人学习参考,不作为系统的学习流程,请注意识别!!!
特点:比较稳定,后续版本基本不会变化,以-开头。
各种选项,可以通过java命令查询帮助文档

Hotspot JVM有两种模式,分别是server和client,分别通过-server和-client模式设置。
非标准化参数,功能还是比较稳定的,但官方说后续版本可能会变更,以-X开头。

非标准化参数,使用最多的参数类型,这类选项属于实验性,不确定,以-XX开头。
用于开发和调试JVM
-XX:+< option > 表示启用option属性。
-XX:-< option > 表示禁用option属性。
举例:
-XX:+UseParallelGC 选择垃圾收集器为并行收集器
-XX:+UseG1GC 表示启用G1收集器
-XX:+UseAdaptiveSizePolicy 自动选择年轻代大小和Survivor区比例
说明:因为有的指令默认是开启的,可以选择-关闭。
子类型1:数值型格式-XX:< option >=< number >
number表示数值,可以带上单位,比如:m、M表示兆,k、K表示Kb等。
举例
-XX:NewSize=1024m 表示设置新生代初始化大小为1024兆
-XX:MaxGCPauseMillis=500 表示设置GC停顿时间500毫秒
-XX:GCTimeRatio=19 表示设置吞吐量
-XX:NewRatio=2 表示设置新生代和老年代的比例
子类型2:非数值型格式-XX:< option >=< String >
举例
-XX:HeapDumpPath=/usr/local/heapdump.hprof 用来指定heap转存文件的存储路径。
-XX:PrintFlagsFinal 输出所有参数的名称和默认值,默认不包括Diagnostic和Experimental的参数,可以配合-XX:+UnlockDiagnosticVMOptions和-XX:UnlockExperimentalVMOptions使用
Linux系统可以在tomcat/bin/catalina.sh中添加类似如下配置
JAVA_OPTS=“-Xms512m -Xmx=512m”
永久代
元空间
对OnOutOfMemoryError的运维处理,以部署在Linux系统的/opt/server目录下的Server.jar为例。
- 在run.sh启动脚本中添加jvm参数:
-XX:OnOutOfMemoryError=/opt/server/restart.sh- linux中的restart.sh脚本
#!/bin/bash pid=$(ps -ef | grep Server.jar | awk '{if($8=="java") {print $2}}') kill -9 $pid cd /opt/server/; sh run.sh
- 1
- 2
- 3
- 4
- 5
垃圾回收器回顾

-XX:+PrintCommandLineFlags:查看命令行相关参数(包含使用的垃圾收集器)
使用命令行指令: jinfo -flag 垃圾回收器参数 pid
以上两种方式都可以查看默认使用的垃圾回收器,第1种方式更加直观,但是需要程序的支持;第2种方式需要去尝试,如果使用了,返回的值中有+号,否则就是-号。
Serial收集器作为HotSpot中Client模式下的默认新生代垃圾收集器。Serial old是运行在Client模式下默认的老年代的垃圾回收器。
小结
Parallel回收器主打吞吐量,而CMS和G1主打低延迟,如果主打吞吐量,那么就不应该限制最大停顿时间,所以-XX:MaxGCPauseMills不应该设置。
-XX:MaxGCPauseMills中的调整堆大小通过默认开启的-XX:+UseAdaptiveSizePolicy来实现。
-XX:GCTimeRatio用来衡量吞吐量,并且和-XX:MaxGCPauseMills矛盾,因此不会同时使用。
-XX:+UseConcMarkSweepGC:手动指定使用CMS收集器执行内存回收任务。
-XX:CMSInitiatingOccupanyFraction:设置堆内存使用率的阈值,一旦达到该阈值,便开始进回收。
-XX:+UseCMSCompactAtFullCollection:用于指定在执行完Full GC后对内存空间进行压缩整理以此避免内存碎片的产生。不过由于内存压缩整理过程无法并发执行,所带来的问题就是停顿时间变得更长了。
-XX:CMSFullGCsBeforeCompaction:设置在执行多少次Full GC后对内存空间进行压缩整理。
-XX:ParallelCMSThreads:设置CMS的线程数量。
CMS默认启动的线程数是(ParallelGCThreads+3)/4,ParallelGCThreads是年轻代并行收集器的线程数。当CPU资源比较紧张时,受到CMS收集器线程的影响,应用程序的性能在垃圾回收阶段可能会非常糟糕。
补充参数
如果使用G1垃圾收集器,不建议设置-Xmn和-XX:NewRatio,毕竟可能影响G1的自动调节
Mixed GC调优参数
常用参数
-XX:+PrintGCDetails包含了 -XX:+PrintGC
######################## -XX:+PrintGC 或者 -verbose:gc #################################
[GC (Allocation Failure) 15335K->13461K(58880K), 0.0039121 secs]
[GC (Allocation Failure) 28752K->28672K(58880K), 0.0142382 secs]
[Full GC (Ergonomics) 28672K->28553K(58880K), 0.0196018 secs]
[Full GC (Ergonomics) 43856K->43456K(58880K), 0.0167212 secs]
######################## -XX:+PrintGCDetails #################################
[GC (Allocation Failure) [PSYoungGen: 15335K->2548K(17920K)] 15335K->13458K(58880K), 0.0200941 secs] [Times: user=0.01 sys=0.02, real=0.02 secs]
[GC (Allocation Failure) [PSYoungGen: 17838K->2536K(17920K)] 28748K->28644K(58880K), 0.0053322 secs] [Times: user=0.03 sys=0.03, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 2536K->0K(17920K)] [ParOldGen: 26108K->28554K(40960K)] 28644K->28554K(58880K), [Metaspace: 3727K->3727K(1056768K)], 0.0095833 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
[Full GC (Ergonomics) [PSYoungGen: 15296K->3000K(17920K)] [ParOldGen: 28554K->40756K(40960K)] 43850K->43756K(58880K), [Metaspace: 3727K->3727K(1056768K)], 0.0188909 secs] [Times: user=0.08 sys=0.00, real=0.02 secs]
Heap
PSYoungGen total 17920K, used 10758K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 70% used [0x00000000fec00000,0x00000000ff681a28,0x00000000ffb00000)
from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 40756K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 99% used [0x00000000fc400000,0x00000000febcd010,0x00000000fec00000)
Metaspace used 3734K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 410K, capacity 428K, committed 512K, reserved 1048576K
######################## -XX:+PrintGCDetails -XX:+PrintGCTimeStamps #################################
2.769: [GC (Allocation Failure) [PSYoungGen: 15335K->2544K(17920K)] 15335K->13421K(58880K), 0.0046001 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
5.895: [GC (Allocation Failure) [PSYoungGen: 17834K->2540K(17920K)] 28712K->28664K(58880K), 0.0046225 secs] [Times: user=0.02 sys=0.05, real=0.00 secs]
5.900: [Full GC (Ergonomics) [PSYoungGen: 2540K->0K(17920K)] [ParOldGen: 26124K->28553K(40960K)] 28664K->28553K(58880K), [Metaspace: 3733K->3733K(1056768K)], 0.0091404 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
8.966: [Full GC (Ergonomics) [PSYoungGen: 15303K->2500K(17920K)] [ParOldGen: 28553K->40955K(40960K)] 43856K->43456K(58880K), [Metaspace: 3733K->3733K(1056768K)], 0.0061868 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 17920K, used 10249K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 66% used [0x00000000fec00000,0x00000000ff602488,0x00000000ffb00000)
from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 40955K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 99% used [0x00000000fc400000,0x00000000febfef80,0x00000000fec00000)
Metaspace used 3739K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 410K, capacity 428K, committed 512K, reserved 1048576K
######################## -XX:+PrintHeapAtGC #################################
{Heap before GC invocations=1 (full 0):
PSYoungGen total 17920K, used 15335K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 99% used [0x00000000fec00000,0x00000000ffaf9e20,0x00000000ffb00000)
from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 0K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 0% used [0x00000000fc400000,0x00000000fc400000,0x00000000fec00000)
Metaspace used 3725K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=1 (full 0):
PSYoungGen total 17920K, used 2532K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffb00000)
from space 2560K, 98% used [0x00000000ffb00000,0x00000000ffd79140,0x00000000ffd80000)
to space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
ParOldGen total 40960K, used 10897K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 26% used [0x00000000fc400000,0x00000000fcea46a0,0x00000000fec00000)
Metaspace used 3725K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=2 (full 0):
PSYoungGen total 17920K, used 17822K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 99% used [0x00000000fec00000,0x00000000ffaeea58,0x00000000ffb00000)
from space 2560K, 98% used [0x00000000ffb00000,0x00000000ffd79140,0x00000000ffd80000)
to space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
ParOldGen total 40960K, used 10897K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 26% used [0x00000000fc400000,0x00000000fcea46a0,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=2 (full 0):
PSYoungGen total 17920K, used 2532K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffb00000)
from space 2560K, 98% used [0x00000000ffd80000,0x00000000ffff9160,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 26108K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 63% used [0x00000000fc400000,0x00000000fdd7f020,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=3 (full 1):
PSYoungGen total 17920K, used 2532K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffb00000)
from space 2560K, 98% used [0x00000000ffd80000,0x00000000ffff9160,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 26108K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 63% used [0x00000000fc400000,0x00000000fdd7f020,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=3 (full 1):
PSYoungGen total 17920K, used 0K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffb00000)
from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 28553K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 69% used [0x00000000fc400000,0x00000000fdfe2588,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=4 (full 2):
PSYoungGen total 17920K, used 15303K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 99% used [0x00000000fec00000,0x00000000ffaf1dc0,0x00000000ffb00000)
from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 28553K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 69% used [0x00000000fc400000,0x00000000fdfe2588,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=4 (full 2):
PSYoungGen total 17920K, used 2500K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 15360K, 16% used [0x00000000fec00000,0x00000000fee71190,0x00000000ffb00000)
from space 2560K, 0% used [0x00000000ffd80000,0x00000000ffd80000,0x0000000100000000)
to space 2560K, 0% used [0x00000000ffb00000,0x00000000ffb00000,0x00000000ffd80000)
ParOldGen total 40960K, used 40955K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 99% used [0x00000000fc400000,0x00000000febfef50,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 409K, capacity 428K, committed 512K, reserved 1048576K
}
其他参数
public class MemoryMonitor {
public static void main(String[] args) {
MemoryMXBean memorymbean = ManagementFactory.getMemoryMXBean();
MemoryUsage usage = memorymbean.getHeapMemoryUsage();
System.out.println("INIT HEAP: " + usage.getInit() / 1024 / 1024 + "m");
System.out.println("MAX HEAP: " + usage.getMax() / 1024 / 1024 + "m");
System.out.println("USE HEAP: " + usage.getUsed() / 1024 / 1024 + "m");
System.out.println("\nFull Information:");
System.out.println("Heap Memory Usage: " + memorymbean.getHeapMemoryUsage());
System.out.println("Non-Heap Memory Usage: " + memorymbean.getNonHeapMemoryUsage());
System.out.println("=======================通过java来获取相关系统状态============================ ");
System.out.println("当前堆内存大小totalMemory " + (int) Runtime.getRuntime().totalMemory() / 1024 / 1024 + "m");// 当前堆内存大小
System.out.println("空闲堆内存大小freeMemory " + (int) Runtime.getRuntime().freeMemory() / 1024 / 1024 + "m");// 空闲堆内存大小
System.out.println("最大可用总堆内存maxMemory " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "m");// 最大可用总堆内存大小
}
}