GC日志相关选项记录

128 阅读18分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第1天,点击查看活动详情

JVM基础参数

-Xms60m -Xmx60m -XX:SurvivorRatio=8

测试代码

public class GCTest {
    public static void main(String[] args) {
        ArrayList<byte[]> list = new ArrayList<>();
        for (int i = 0; i < 25; i++) {
            byte[] arr = new byte[1024 * 1024 * 2];
            list.add(arr);
            try {
                Thread.sleep(5);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

常用参数

-verbose:gc

  1. 输出gc日志信息,默认输出到标准输出
  2. 可以独立使用
[GC (Allocation Failure)  16374K->13108K(59392K), 0.0052206 secs]
[GC (Allocation Failure)  29482K->29145K(59392K), 0.0066966 secs]
[Full GC (Ergonomics)  29145K->28563K(59392K), 0.0060716 secs]
[Full GC (Ergonomics)  44902K->44707K(59392K), 0.0100425 secs]

-XX:+PrintGC

  1. 等同于-verbose:gc,表示打开简化的GC日志
  2. 可以独立使用
[GC (Allocation Failure)  16365K->13615K(59392K), 0.0033030 secs]
[GC (Allocation Failure)  28269K->27534K(59392K), 0.0044260 secs]
[Full GC (Ergonomics)  27534K->27340K(59392K), 0.0069424 secs]
[Full GC (Ergonomics)  41979K->41676K(59392K), 0.0054463 secs]

-XX:+PrintGCDetails

  1. 在发生垃圾回收时打印内存回收详细的日志,并在进程退出时输出当前内存各区域分配情况
  2. 可独立使用
[GC (Allocation Failure) [PSYoungGen: 16365K->1287K(18432K)] 16365K->13583K(59392K), 0.0041228 secs] [Times: user=0.02 sys=0.01, real=0.00 secs] 
[GC (Allocation Failure) [PSYoungGen: 15941K->910K(18432K)] 28237K->27550K(59392K), 0.0043277 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
[Full GC (Ergonomics) [PSYoungGen: 910K->0K(18432K)] [ParOldGen: 26640K->27340K(40960K)] 27550K->27340K(59392K), [Metaspace: 3149K->3149K(1056768K)], 0.0071445 secs] [Times: user=0.05 sys=0.00, real=0.00 secs] 
[Full GC (Ergonomics) [PSYoungGen: 14639K->2048K(18432K)] [ParOldGen: 27340K->39628K(40960K)] 41979K->41676K(59392K), [Metaspace: 3149K->3149K(1056768K)], 0.0059041 secs] [Times: user=0.02 sys=0.02, real=0.00 secs] 
Heap
 PSYoungGen      total 18432K, used 12909K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)  eden space 16384K, 78% used [0x00000007bec00000,0x00000007bf89b710,0x00000007bfc00000)  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000) ParOldGen       total 40960K, used 39628K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)  object space 40960K, 96% used [0x00000007bc400000,0x00000007beab30b0,0x00000007bec00000) Metaspace       used 3155K, capacity 4496K, committed 4864K, reserved 1056768K  class space    used 341K, capacity 388K, committed 512K, reserved 1048576K

-XX:+PrintGCTimeStamps

  1. 输出GC发生时的时间戳
  2. 不可以独立使用,需要配合-XX:+PrintGCDetails使用
0.221: [GC (Allocation Failure) [PSYoungGen: 16365K->1319K(18432K)] 16365K->13615K(59392K), 0.0050943 secs] [Times: user=0.02 sys=0.01, real=0.00 secs] 
0.268: [GC (Allocation Failure) [PSYoungGen: 15973K->910K(18432K)] 28269K->27550K(59392K), 0.0049497 secs] [Times: user=0.01 sys=0.02, real=0.01 secs] 
0.273: [Full GC (Ergonomics) [PSYoungGen: 910K->0K(18432K)] [ParOldGen: 26640K->27340K(40960K)] 27550K->27340K(59392K), [Metaspace: 3149K->3149K(1056768K)], 0.0082724 secs] [Times: user=0.06 sys=0.00, real=0.01 secs] 
0.323: [Full GC (Ergonomics) [PSYoungGen: 14639K->2048K(18432K)] [ParOldGen: 27340K->39628K(40960K)] 41979K->41676K(59392K), [Metaspace: 3149K->3149K(1056768K)], 0.0064552 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
Heap
 PSYoungGen      total 18432K, used 12909K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 78% used [0x00000007bec00000,0x00000007bf89b708,0x00000007bfc00000)
  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 39628K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 96% used [0x00000007bc400000,0x00000007beab30b0,0x00000007bec00000)
 Metaspace       used 3155K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 341K, capacity 388K, committed 512K, reserved 1048576K

-XX:+PrintGCDateStamps

  1. 输出GC发生时的时间戳,以日期的形式
  2. 不可以独立使用,需要配合-XX:+PrintGCDetails使用
2022-05-26T14:47:30.064-0800: [GC (Allocation Failure) [PSYoungGen: 16365K->1255K(18432K)] 16365K->13551K(59392K), 0.0045343 secs] [Times: user=0.02 sys=0.02, real=0.00 secs] 
2022-05-26T14:47:30.112-0800: [GC (Allocation Failure) [PSYoungGen: 15909K->830K(18432K)] 28205K->27470K(59392K), 0.0051726 secs] [Times: user=0.02 sys=0.01, real=0.00 secs] 
2022-05-26T14:47:30.117-0800: [Full GC (Ergonomics) [PSYoungGen: 830K->0K(18432K)] [ParOldGen: 26640K->27340K(40960K)] 27470K->27340K(59392K), [Metaspace: 3149K->3149K(1056768K)], 0.0073781 secs] [Times: user=0.06 sys=0.01, real=0.01 secs] 
2022-05-26T14:47:30.165-0800: [Full GC (Ergonomics) [PSYoungGen: 14967K->2048K(18432K)] [ParOldGen: 27340K->39627K(40960K)] 42307K->41675K(59392K), [Metaspace: 3149K->3149K(1056768K)], 0.0059815 secs] [Times: user=0.02 sys=0.02, real=0.01 secs] 
Heap
 PSYoungGen      total 18432K, used 12743K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 77% used [0x00000007bec00000,0x00000007bf871f40,0x00000007bfc00000)
  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 39627K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 96% used [0x00000007bc400000,0x00000007beab2fb8,0x00000007bec00000)
 Metaspace       used 3155K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 341K, capacity 388K, committed 512K, reserved 1048576K

-XX:+PrintHeapAtGC

  1. 每一次GC前和GC后,都打印堆信息
  2. 可以独立使用
{Heap before GC invocations=1 (full 0):
 PSYoungGen      total 18432K, used 16365K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 99% used [0x00000007bec00000,0x00000007bfbfb768,0x00000007bfc00000)
  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 0K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 0% used [0x00000007bc400000,0x00000007bc400000,0x00000007bec00000)
 Metaspace       used 3144K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
Heap after GC invocations=1 (full 0):
 PSYoungGen      total 18432K, used 1287K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bfc00000)
  from space 2048K, 62% used [0x00000007bfc00000,0x00000007bfd41f90,0x00000007bfe00000)
  to   space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
 ParOldGen       total 40960K, used 12296K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 30% used [0x00000007bc400000,0x00000007bd002060,0x00000007bec00000)
 Metaspace       used 3144K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=2 (full 0):
 PSYoungGen      total 18432K, used 15941K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 89% used [0x00000007bec00000,0x00000007bfa4f5e8,0x00000007bfc00000)
  from space 2048K, 62% used [0x00000007bfc00000,0x00000007bfd41f90,0x00000007bfe00000)
  to   space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
 ParOldGen       total 40960K, used 12296K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 30% used [0x00000007bc400000,0x00000007bd002060,0x00000007bec00000)
 Metaspace       used 3149K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
Heap after GC invocations=2 (full 0):
 PSYoungGen      total 18432K, used 974K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bfc00000)
  from space 2048K, 47% used [0x00000007bfe00000,0x00000007bfef3908,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 26640K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 65% used [0x00000007bc400000,0x00000007bde040d0,0x00000007bec00000)
 Metaspace       used 3149K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=3 (full 1):
 PSYoungGen      total 18432K, used 974K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bfc00000)
  from space 2048K, 47% used [0x00000007bfe00000,0x00000007bfef3908,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 26640K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 65% used [0x00000007bc400000,0x00000007bde040d0,0x00000007bec00000)
 Metaspace       used 3149K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
Heap after GC invocations=3 (full 1):
 PSYoungGen      total 18432K, used 0K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 0% used [0x00000007bec00000,0x00000007bec00000,0x00000007bfc00000)
  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 27340K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 66% used [0x00000007bc400000,0x00000007bdeb30a0,0x00000007bec00000)
 Metaspace       used 3149K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
}
{Heap before GC invocations=4 (full 2):
 PSYoungGen      total 18432K, used 14967K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 91% used [0x00000007bec00000,0x00000007bfa9ddc0,0x00000007bfc00000)
  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 27340K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 66% used [0x00000007bc400000,0x00000007bdeb30a0,0x00000007bec00000)
 Metaspace       used 3149K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
Heap after GC invocations=4 (full 2):
 PSYoungGen      total 18432K, used 2048K [0x00000007bec00000, 0x00000007c0000000, 0x00000007c0000000)
  eden space 16384K, 12% used [0x00000007bec00000,0x00000007bee00010,0x00000007bfc00000)
  from space 2048K, 0% used [0x00000007bfe00000,0x00000007bfe00000,0x00000007c0000000)
  to   space 2048K, 0% used [0x00000007bfc00000,0x00000007bfc00000,0x00000007bfe00000)
 ParOldGen       total 40960K, used 39628K [0x00000007bc400000, 0x00000007bec00000, 0x00000007bec00000)
  object space 40960K, 96% used [0x00000007bc400000,0x00000007beab3048,0x00000007bec00000)
 Metaspace       used 3149K, capacity 4496K, committed 4864K, reserved 1056768K
  class space    used 340K, capacity 388K, committed 512K, reserved 1048576K
}

-Xloggc:file_path

  1. 把GC日志写入到一个文件中去,而不是打印到标准输出中
-Xloggc:/Users/youngxw/Desktop/gc.log

其他参数

-XX:+TraceClassLoading

  1. 监控类的加载
[Opened /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre/lib/rt.jar]
...
[Loaded sun.nio.cs.US_ASCII$Decoder from /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.lang.Shutdown from /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.lang.Shutdown$Lock from /Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home/jre/lib/rt.jar]

-XX:+PrintGCApplicationStoppedTime

  1. 打印GC时线程的停顿时间
Total time for which application threads were stopped: 0.0043283 seconds, Stopping threads took: 0.0000120 seconds
Total time for which application threads were stopped: 0.0120537 seconds, Stopping threads took: 0.0000140 seconds
Total time for which application threads were stopped: 0.0059392 seconds, Stopping threads took: 0.0000127 seconds

-XX:+PrintGCApplicationConcurrentTime

  1. 垃圾收集之前打印出应用未中断的执行时间
Application time: 0.0416186 seconds
Application time: 0.0434134 seconds
Application time: 0.0434637 seconds
Application time: 0.0306922 seconds

-XX:+PrintReferenceGC

  1. 记录回收了多少种不同引用类型的引用

-XX:+PrintTenuringDistribution

  1. 让JVM在每次MinorGC后打印出当前使用的Survivor中对象的年龄分布
Desired survivor size 2097152 bytes, new threshold 7 (max 15)

Desired survivor size 2097152 bytes, new threshold 7 (max 15)

-XX:+UserGCLogFileRotation

  1. 启用GC日志文件的自动转储

-XX:NumberOfGClogFiles=1

  1. GC日志文件的循环数目

-XX:GCLogFileSize=1M

  1. 控制GC日志文件的大小