一 常用选项
-XX:+PrintGC <==> -verbose:gc 打印简要日志信息,独立使用
-XX:+PrintGCDetails 打印详细日志信息,独立使用
-XX:+PrintGCTimeStamps 打印程序启动到GC发生的时间,搭配 -XX:+PrintGCDetails 使用,不可以独立使用
-XX:+PrintGCDateStamps 打印GC发生时的时间戳,搭配 -XX:+PrintGCDetails 使用,不可以独立使用
-XX:+PrintHeapAtGC 打印 GC 前后的堆信息,独立使用
-Xloggc:<file> 输出 GC 到指定路径下的文件中
二 其他选项
-XX:+TraceClassLoading 监控类的加载
-XX:+PrintGCApplicationStoppedTime 打印 GC 时线程的停顿时间
-XX:+PrintGCApplicationConcurrentTime 打印垃圾收集之前应用未中断的执行时间
-XX:+PrintReferenceGC 打印回收了多少种不同引用类型的引用
-XX:+PrintTenuringDistribution 打印 JVM 在每次 MinorGC 后当前使用的 Survivor 中对象的年龄分布
-XX:+UseGCLogFileRotation 启用 GC 日志文件的自动转储
-XX:NumberOfGCLogFiles=1 设置 GC 日志文件的循环数目
-XX:GCLogFileSize=1M 设置 GC 日志文件的大小
三 实战
1 代码
package chapter04;
import java.util.ArrayList;
/**
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -verbose:gc
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGC
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCDetails
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCDateStamps
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintHeapAtGC
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintHeapAtGC -XX:+PrintGCDetails -Xloggc:d:/heaplog.log
* -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCApplicationStoppedTime
*/
public class GCLogTest {
public static void main(String[] args) {
ArrayList<byte[]> list = new ArrayList<>();
for (int i = 0; i < 500; i++) {
byte[] arr = new byte[1024 * 100];// 100kb
list.add(arr);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -verbose:gc 启动
[GC (Allocation Failure) 16294K->14546K(59392K), 0.0039367 secs]
[GC (Allocation Failure) 30894K->30517K(59392K), 0.0055465 secs]
[Full GC (Ergonomics) 30517K->30300K(59392K), 0.0108135 secs]
[Full GC (Ergonomics) 46584K->46402K(59392K), 0.0069119 secs]
3 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGC
[GC (Allocation Failure) 16384K->14378K(59392K), 0.0042745 secs]
[GC (Allocation Failure) 30698K->30688K(59392K), 0.0048966 secs]
[Full GC (Ergonomics) 30688K->30561K(59392K), 0.0122676 secs]
[Full GC (Ergonomics) 46905K->46564K(59392K), 0.0098682 secs]
4 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCDetails
[GC (Allocation Failure) [PSYoungGen: 16384K->2036K(18432K)] 16384K->14422K(59392K), 0.0043724 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
[GC (Allocation Failure) [PSYoungGen: 18356K->2020K(18432K)] 30742K->30672K(59392K), 0.0046265 secs] [Times: user=0.02 sys=0.08, real=0.00 secs]
[Full GC (Ergonomics) [PSYoungGen: 2020K->0K(18432K)] [ParOldGen: 28652K->30561K(40960K)] 30672K->30561K(59392K), [Metaspace: 3726K->3726K(1056768K)], 0.0156724 secs] [Times: user=0.00 sys=0.00, real=0.02 secs]
[Full GC (Ergonomics) [PSYoungGen: 16343K->6100K(18432K)] [ParOldGen: 30561K->40463K(40960K)] 46905K->46564K(59392K), [Metaspace: 3727K->3727K(1056768K)], 0.0097613 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 18432K, used 10679K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 65% used [0x00000000fec00000,0x00000000ff66dc00,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 40463K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 98% used [0x00000000fc400000,0x00000000feb83e40,0x00000000fec00000)
Metaspace used 3733K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 408K, capacity 428K, committed 512K, reserved 1048576K
5 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCTimeStamps
4.396: [GC (Allocation Failure) [PSYoungGen: 16384K->2036K(18432K)] 16384K->14406K(59392K), 0.0047065 secs] [Times: user=0.00 sys=0.00, real=0.00 secs]
9.561: [GC (Allocation Failure) [PSYoungGen: 18357K->1960K(18432K)] 30726K->30740K(59392K), 0.0063010 secs] [Times: user=0.00 sys=0.01, real=0.01 secs]
9.567: [Full GC (Ergonomics) [PSYoungGen: 1960K->0K(18432K)] [ParOldGen: 28780K->30561K(40960K)] 30740K->30561K(59392K), [Metaspace: 3726K->3726K(1056768K)], 0.0168554 secs] [Times: user=0.06 sys=0.00, real=0.02 secs]
14.630: [Full GC (Ergonomics) [PSYoungGen: 16343K->6100K(18432K)] [ParOldGen: 30561K->40463K(40960K)] 46905K->46564K(59392K), [Metaspace: 3726K->3726K(1056768K)], 0.0055275 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
Heap
PSYoungGen total 18432K, used 10679K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 65% used [0x00000000fec00000,0x00000000ff66dc00,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 40463K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 98% used [0x00000000fc400000,0x00000000feb83f18,0x00000000fec00000)
Metaspace used 3732K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 408K, capacity 428K, committed 512K, reserved 1048576K
6 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCDetails -XX:+PrintGCDateStamps
2021-09-06T08:44:49.453+0800: [GC (Allocation Failure) [PSYoungGen: 16384K->2020K(18432K)] 16384K->14426K(59392K), 0.0060231 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-09-06T08:44:54.610+0800: [GC (Allocation Failure) [PSYoungGen: 18341K->1996K(18432K)] 30746K->30712K(59392K), 0.0068602 secs] [Times: user=0.00 sys=0.00, real=0.01 secs]
2021-09-06T08:44:54.617+0800: [Full GC (Ergonomics) [PSYoungGen: 1996K->0K(18432K)] [ParOldGen: 28716K->30565K(40960K)] 30712K->30565K(59392K), [Metaspace: 3725K->3725K(1056768K)], 0.0156272 secs] [Times: user=0.02 sys=0.00, real=0.01 secs]
2021-09-06T08:44:59.793+0800: [Full GC (Ergonomics) [PSYoungGen: 16315K->6000K(18432K)] [ParOldGen: 30565K->40864K(40960K)] 46881K->46864K(59392K), [Metaspace: 3726K->3726K(1056768K)], 0.0149928 secs] [Times: user=0.06 sys=0.02, real=0.02 secs]
Heap
PSYoungGen total 18432K, used 10608K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 64% used [0x00000000fec00000,0x00000000ff65c3c0,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 40864K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 99% used [0x00000000fc400000,0x00000000febe8030,0x00000000fec00000)
Metaspace used 3732K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 408K, capacity 428K, committed 512K, reserved 1048576K
7 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintHeapAtGC
# (full 0) 表示非 Full GC
{Heap before GC invocations=1 (full 0):
PSYoungGen total 18432K, used 16384K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 100% used [0x00000000fec00000,0x00000000ffc00000,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
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 407K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=1 (full 0):
PSYoungGen total 18432K, used 2020K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffc00000)
from space 2048K, 98% used [0x00000000ffc00000,0x00000000ffdf9110,0x00000000ffe00000)
to space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
ParOldGen total 40960K, used 12401K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 30% used [0x00000000fc400000,0x00000000fd01c760,0x00000000fec00000)
Metaspace used 3725K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
}
# (full 0) 表示非 Full GC
{Heap before GC invocations=2 (full 0):
PSYoungGen total 18432K, used 18341K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 99% used [0x00000000fec00000,0x00000000ffbf0310,0x00000000ffc00000)
from space 2048K, 98% used [0x00000000ffc00000,0x00000000ffdf9110,0x00000000ffe00000)
to space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
ParOldGen total 40960K, used 12401K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 30% used [0x00000000fc400000,0x00000000fd01c760,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=2 (full 0):
PSYoungGen total 18432K, used 2020K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffc00000)
from space 2048K, 98% used [0x00000000ffe00000,0x00000000ffff9110,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 28712K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 70% used [0x00000000fc400000,0x00000000fe00a190,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
}
# (full 1) 表示第一次 Full GC
{Heap before GC invocations=3 (full 1):
PSYoungGen total 18432K, used 2020K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffc00000)
from space 2048K, 98% used [0x00000000ffe00000,0x00000000ffff9110,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 28712K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 70% used [0x00000000fc400000,0x00000000fe00a190,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=3 (full 1):
PSYoungGen total 18432K, used 0K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 0% used [0x00000000fec00000,0x00000000fec00000,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 30562K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 74% used [0x00000000fc400000,0x00000000fe1d8918,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
}
# (full 2) 表示第二次 Full GC
{Heap before GC invocations=4 (full 2):
PSYoungGen total 18432K, used 16315K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 99% used [0x00000000fec00000,0x00000000ffbeefa0,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 30562K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 74% used [0x00000000fc400000,0x00000000fe1d8918,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
Heap after GC invocations=4 (full 2):
PSYoungGen total 18432K, used 6000K [0x00000000fec00000, 0x0000000100000000, 0x0000000100000000)
eden space 16384K, 36% used [0x00000000fec00000,0x00000000ff1dc3c0,0x00000000ffc00000)
from space 2048K, 0% used [0x00000000ffe00000,0x00000000ffe00000,0x0000000100000000)
to space 2048K, 0% used [0x00000000ffc00000,0x00000000ffc00000,0x00000000ffe00000)
ParOldGen total 40960K, used 40863K [0x00000000fc400000, 0x00000000fec00000, 0x00000000fec00000)
object space 40960K, 99% used [0x00000000fc400000,0x00000000febe7f58,0x00000000fec00000)
Metaspace used 3726K, capacity 4536K, committed 4864K, reserved 1056768K
class space used 407K, capacity 428K, committed 512K, reserved 1048576K
}
8 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintHeapAtGC -XX:+PrintGCDetails -Xloggc:d:/heaplog.log
9 设置为 -Xms60m -Xmx60m -XX:SurvivorRatio=8 -XX:+PrintGCApplicationStoppedTime
Total time for which application threads were stopped: 0.0001415 seconds, Stopping threads took: 0.0000461 seconds
Total time for which application threads were stopped: 0.0000912 seconds, Stopping threads took: 0.0000316 seconds
Total time for which application threads were stopped: 0.0054704 seconds, Stopping threads took: 0.0000278 seconds
Total time for which application threads were stopped: 0.0208119 seconds, Stopping threads took: 0.0000492 seconds
Total time for which application threads were stopped: 0.0058309 seconds, Stopping threads took: 0.0000393 seconds