打印GC日志
-XX:+PrintGC
jdk9之后建议使用 -Xlog:gc
[GC (Allocation Failure) 4676K->2628K(9728K), 0.0004307 secs]
[GC (Allocation Failure) 4676K->2628K(9728K), 0.0004717 secs]
[GC (Allocation Failure) 4676K->2628K(9728K), 0.0006033 secs]
[GC (Allocation Failure) 4676K->2628K(9728K), 0.0005774 secs]
堆空间使用量约为4MB,在GC后,堆空间使用量为2628KB,当前可用的堆空间总和约为10MB
-XX:+PrintGCDetails
jdk9之后使用 -Xlog:gc*
GC详细信息
[GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4200K->2152K(9728K), 0.0008739 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4200K->2152K(9728K), 0.0004044 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
Heap
PSYoungGen total 2560K, used 935K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 45% used [0x00000000ffd00000,0x00000000ffde9d38,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 2152K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 30% used [0x00000000ff600000,0x00000000ff81a060,0x00000000ffd00000)
Metaspace used 3988K, capacity 4572K, committed 4864K, reserved 1056768K
class space used 435K, capacity 460K, committed 512K, reserved 1048576K
最后一次GC是新生代GC,新生代从回收前的2047KB降到0K,整个堆从4200KB降到2152KB
堆信息:新生代总大小2560K,已使用935K,其后的3个16进制表示新生代的下界、当前上界和上界
[0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
使用上界减去下界就能得到当前堆空间的最大值,使用当前上界减去下界,就是当前虚拟机已经为程序分配的空间。如果当前上界等于下界,说明当前的堆空间已经没有扩大可能性。
(0x0000000100000000-0x00000000ffd00000)/1024=3072K
这块空间正好是eden+from+to的总和
-XX:+PrintHeapAtGC
jdk9之后删除词参数
在每次GC前、后分别打印堆的信息
{Heap before GC invocations=1156 (full 0):
PSYoungGen total 2560K, used 2047K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 99% used [0x00000000ffd00000,0x00000000ffeffff8,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
to space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
ParOldGen total 7168K, used 2608K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 36% used [0x00000000ff600000,0x00000000ff88c038,0x00000000ffd00000)
Metaspace used 3979K, capacity 4572K, committed 4864K, reserved 1056768K
class space used 434K, capacity 460K, committed 512K, reserved 1048576K
Heap after GC invocations=1156 (full 0):
PSYoungGen total 2560K, used 0K [0x00000000ffd00000, 0x0000000100000000, 0x0000000100000000)
eden space 2048K, 0% used [0x00000000ffd00000,0x00000000ffd00000,0x00000000fff00000)
from space 512K, 0% used [0x00000000fff80000,0x00000000fff80000,0x0000000100000000)
to space 512K, 0% used [0x00000000fff00000,0x00000000fff00000,0x00000000fff80000)
ParOldGen total 7168K, used 2608K [0x00000000ff600000, 0x00000000ffd00000, 0x00000000ffd00000)
object space 7168K, 36% used [0x00000000ff600000,0x00000000ff88c038,0x00000000ffd00000)
Metaspace used 3979K, capacity 4572K, committed 4864K, reserved 1056768K
class space used 434K, capacity 460K, committed 512K, reserved 1048576K
}
-XX:+PrintGCTimeStamps
打印GC发生的时间,jdk9之后使用-Xlog:gc*
默认打印时间
1.751: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4512K->2464K(9728K), 0.0003170 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.752: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4512K->2464K(9728K), 0.0003336 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.753: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4512K->2464K(9728K), 0.0002719 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.755: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4512K->2464K(9728K), 0.0003494 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.756: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4512K->2464K(9728K), 0.0002822 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.757: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4512K->2464K(9728K), 0.0003964 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
-XX:+PrintGCApplicationConcurrentTime
打印应用程序的执行命令
1.786: [GC (Allocation Failure) [PSYoungGen: 2047K->0K(2560K)] 4668K->2620K(9728K), 0.0003586 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
1.788: Application time: 0.0008788 seconds
-XX:+PrintGCApplicationStoppedTime
打印应用程序由于GC而产生的停顿时间
0.177: [GC (Allocation Failure) [PSYoungGen: 2029K->503K(2560K)] 2029K->950K(9728K), 0.0008365 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
0.178: Total time for which application threads were stopped: 0.0009772 seconds, Stopping threads took: 0.0000150 seconds
-XX:+PrintReferenceGC
打印软引用、弱引用、虚引用和Finallize队列信息 jdk9之后删除此参数
1.850: [GC (Allocation Failure) 1.851: [SoftReference, 0 refs, 0.0000321 secs]1.851: [WeakReference, 0 refs, 0.0000108 secs]1.851: [FinalReference, 0 refs, 0.0000257 secs]1.851: [PhantomReference, 0 refs, 0 refs, 0.0000116 secs]1.851: [JNI Weak Reference, 0.0000073 secs][PSYoungGen: 2047K->0K(2560K)] 4624K->2576K(9728K), 0.0008096 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
类加载/卸载的跟踪
-verbose:class -XX:+TraceClassLoading -XX:+TraceClassUnloading
在jdk9之后使用-Xlog:class+load=info -Xlog:class+unload=info
追踪加载/卸载以class文件的形式加载到虚拟机的类,还有动态代理、AOP等产生的动态类
查看系统参数
-XX:+PrintVMOptions
打印虚拟机接收到的命令行显式参数
VM option '+PrintVMOptions'
-XX:+PrintCommandLineFlags
打印传递给虚拟机的显式和隐式参数
-XX:InitialHeapSize=266121280 -XX:MaxHeapSize=4257940480 -XX:+PrintCommandLineFlags -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:-UseLargePagesIndividualAllocation -XX:+UseParallelGC
5
-XX:+PrintFlagsFinal
打印所有的系统参数的值
堆
-Xms -Xmx
-Xms初始堆空间的大小,如果初始堆的空间不足耗尽,虚拟机将会对堆空间进行扩展,其扩展上限为最大堆空间,最大堆空间可以使用参数-Xmx指定,一般情况下在HotSpot中初始堆的大小和最大堆大小设置成一致。
-Xms20M -Xmx20M
-XX:SurvivorRatio
设置新生代eden区和from/to区的比例
-Xms20m -Xmx20m -Xmn6m -XX:SuvivorRatio=2 -XX:+PrintGCDetails
Heap
PSYoungGen total 4608K, used 3667K [0x00000000ffa00000, 0x0000000100000000, 0x0000000100000000)
eden space 3072K, 70% used [0x00000000ffa00000,0x00000000ffc1ce80,0x00000000ffd00000)
from space 1536K, 97% used [0x00000000ffd00000,0x00000000ffe78010,0x00000000ffe80000)
to space 1536K, 0% used [0x00000000ffe80000,0x00000000ffe80000,0x0000000100000000)
ParOldGen total 14336K, used 572K [0x00000000fec00000, 0x00000000ffa00000, 0x00000000ffa00000)
object space 14336K, 3% used [0x00000000fec00000,0x00000000fec8f060,0x00000000ffa00000)
Metaspace used 3417K, capacity 4496K, committed 4864K, reserved 1056768K
class space used 372K, capacity 388K, committed 512K, reserved 1048576K
-Xmn
-Xmn设置新生代的绝对大小
-XX:NewRatio
设置新生代和老年代的比例
-XX:+HeapDumpOnOutOfMemoryError
在内存溢出时导出整个堆的 信息
-XX:HeapDumpPath
指定导出堆的存放路径,和-XX:+HeapDumpOnOutOfMemoryError一起使用
-Xms20m -Xmx20m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=d:/a.dump
方法区
-XX:PermSize -XX:MaxPermSize
jdk1.6 .17等版本 -XX:PermSize表示永久区大小 -XX:MaxPermSize表示最大永久区大小
-XX:MaxMetaspaceSize
jdk1.8及以后 指定元数据区的最大可用值
栈
-Xss
指定线程的栈大小
-XX:+DoEscapeAnalysis
启用逃逸分析
-XX:+EliminateAllocations
开启标量替换
直接内存
-XX:MaxDirectMemorySize
最大可用直接内存 如果不设置,默认值为最大堆空间即-Xmx值,当直接内存使用量达到-XX:MaxDirectMemorySize时,就会触发GC,如果GC不能有效释放空间,直接内存溢出依然会引起系统的OOM。