我要评分
获取效率
正确性
完整性
易理解

Java Garbage Collector and Log Levels

Currently, Concurrent Mark Sweep (CMS) and Garbage-First (G1) garbage collector logs can be parsed. Java 8 supports CMS and G1, and Java 9 and later versions support only G1.

If no data is obtained on the garbage collection (GC) log page, check whether the corresponding log level is enabled. That is, add parameters when starting the Java process. After the log level is enabled, you can view the log data. Table 1 describe the log levels.

  • If Java 8 is used, you need to specify a garbage collector when starting the process.

    G1: -XX:+UseG1GC; CMS: -XX:+UseConcMarkSweepGC

  • When starting Java 8, add the log printing parameter -verbose:gc -XX:+PrintHeapAtGC -Xloggc:/filepath/filename.log or -XX:+PrintGCDetails -XX:+PrintHeapAtGC -Xloggc:/filepath/filename.log.

    filepath indicates the log storage path, and filename indicates the log name. The log name supports dynamic parameters %p (PID) and %t (system timestamp).

  • For details about the log printing parameter format in Java 9 and later, visit JEP 158: Unified JVM Logging.
Table 1 Log levels

Java Version

Garbage Collector

Log Level

Data Available

Java 8

G1

-verbose:gc

No data is available in the linearity, GC phases, eden space, survivor space, and metaspace.

-XX:+PrintGCDetails

All data can be displayed. Full GC events are recorded only when they exist in the metaspace.

(Optional) -XX:+PrintHeapAtGC

All data about memory usage changes can be displayed.

CMS

-verbose:gc

No data is available in the linearity, GC phases, old generation, young generation, eden space, survivor space, and metaspace.

-XX:+PrintGCDetails

All data except that in the eden and survivor spaces can be displayed. Full GC events are recorded only when they exist in the metaspace.

(Optional) -XX:+PrintHeapAtGC

All data about memory usage changes can be displayed.

Java 9 and later

G1

-Xlog:gc*=info

The GC tab page displays GC pause and heap change information.

The GC and CPU tab pages display the linearity information.

The GC and Marking tab pages display the parallel phase information.

-Xlog:gc*=debug

The GC and Heap tab pages display the memory changes of the eden space, survivor space, and metaspace.

The Phases tab page displays details about the GC pauses.