How to Enable JVM GC Logging: -Xlog and JDK 8 Flags

The -Xlog:gc* line to use in production, rotation and decorators, turning logging on in a running JVM with jcmd VM.log, and which JDK 8 flags now stop the JVM.

By · · 11 min read

Illustration for How to Enable JVM GC Logging: -Xlog and JDK 8 Flags

The JVM is slow or dying and nobody turned on GC logging, or the JDK 8 flags in the start script stopped the JVM after an upgrade. On JDK 9 and later, add this to the java command line and leave it there:

-Xlog:gc*:file=/var/log/app/gc.log:time,uptime,level,tags:filecount=5,filesize=20m

That logs every message carrying the gc tag at info level to a rotating set of files, with wall-clock and uptime stamps. A JVM that is already running can start logging without a restart through jcmd <pid> VM.log. Everything here was run on OpenJDK 17.0.21.1, and the syntax shown is JDK 17's; once the log exists, the Java GC Log Analyzer turns it into pause percentiles and a heap trend in your browser.

The -Xlog syntax, piece by piece

The JVM prints its own grammar with java -Xlog:help:

-Xlog Usage: -Xlog[:[selections][:[output][:[decorators][:output-options]]]]
	 where 'selections' are combinations of tags and levels of the form tag1[+tag2...][*][=level][,...]
	 NOTE: Unless wildcard (*) is specified, only log messages tagged with exactly the tags specified will be matched.

Four colon-separated fields, each optional:

  • Selections. gc matches messages tagged exactly gc, one line per collection: JEP 271, which rebuilt GC logging on this framework, says -Xlog:gc "should be similar to what -XX:+PrintGC did". gc* matches every tag set that includes gc (gc,heap, gc,phases, gc,cpu, gc,metaspace and so on), the equivalent of PrintGCDetails. gc+heap=debug means exactly the tags gc and heap, at debug. Combine with commas: gc*,safepoint adds the safepoint pause lines. Levels run off, trace, debug, info, warning, error; the default is info.
  • Output. stdout, stderr or file=<path>. The help states that %p and %t in a file name expand to the pid and the JVM's start time: gc-%p-%t.log produced gc-50074-2026-11-23_00-20-09.log.
  • Decorators. The prefix on each line: time (t), utctime (utc), uptime (u), timemillis, uptimemillis, timenanos, uptimenanos, hostname, pid, tid, level (l), tags (tg), or none. The default is uptime,level,tags.
  • Output options. filecount= and filesize= for rotation (below); the help also documents -Xlog:async, which writes through a buffer and a separate thread so that logging never blocks the thread that logs.

Quote the whole option in shell scripts. zsh refused -Xlog:gc*:file=... with no matches found, because it tried to expand gc* as a file name; bash passes an unmatched pattern through, but quoting costs nothing.

What one collection looks like under the recommended line, trimmed to five of its fifteen lines:

[2026-11-23T00:28:51.839-0800][10.270s][info][gc,start    ] GC(10) Pause Young (Normal) (G1 Evacuation Pause)
[2026-11-23T00:28:51.844-0800][10.275s][info][gc,phases   ] GC(10)   Evacuate Collection Set: 4.41ms
[2026-11-23T00:28:51.844-0800][10.275s][info][gc,heap     ] GC(10) Old regions: 34->38
[2026-11-23T00:28:51.844-0800][10.275s][info][gc          ] GC(10) Pause Young (Normal) (G1 Evacuation Pause) 103M->47M(128M) 4.936ms
[2026-11-23T00:28:51.844-0800][10.275s][info][gc,cpu      ] GC(10) User=0.01s Sys=0.00s Real=0.01s

The [gc] line is the one -Xlog:gc alone would print: cause, heap before and after, heap size in brackets, pause.

Rotation, restarts and file names

The JDK 17 java manual says files rotate by default "with up to 5 rotated files of target size 20 MB", and jcmd <pid> VM.log list on a JVM given only file= showed exactly filecount=5,filesize=20480K. So -Xlog:gc*:file=gc.log already rotates; writing the numbers out documents the intent. Tested behaviour:

SettingWhat happened
filecount=3,filesize=20k, long rungc.log plus gc.log.0, gc.log.1, gc.log.2, each about 20 KB; filecount does not count the active file
JVM restarted with the same file namethe old gc.log was rotated to a numbered file, not overwritten
filecount=0rotation off; a restart overwrote the existing file, as the manual warns
file=nodir/gc.log, directory missingError opening log file 'nodir/gc.log': No such file or directory, then Invalid -Xlog option, and the JVM did not start

The last row matters in containers: a log path on a volume that is not mounted keeps the service from starting at all. Prefer a fixed name such as gc.log over gc-%p.log on hosts where the JVM restarts: the fixed name keeps the previous run's log through the restart rotation, while a per-pid name leaves files from dead processes that rotation never removes.

How much disk that is: on an allocation stress test that ran 792 collections, the recommended decorators produced 1,305,749 bytes for gc* and 126,889 for gc, about 1,649 and 160 bytes per collection. Six files of 20 MiB therefore hold roughly 76,000 collections of gc* detail, days for most services and minutes for a JVM collecting hundreds of times a second.

The line for production, and for containers

On a VM or bare host:

-Xlog:gc*,safepoint:file=/var/log/app/gc.log:time,uptime,level,tags:filecount=5,filesize=20m

time gives the wall clock to line up with application logs and incidents, uptime gives seconds since JVM start, the stamp other JVM logs carry by default, and level,tags let you filter. Adding safepoint records every stop-the-world pause, GC or not, the replacement the java manual's conversion table gives for PrintGCApplicationStoppedTime.

In a container, either keep the file on a mounted volume, so it survives the restart that follows a crash, or log to standard output and let the platform collect it:

-Xlog:gc*:stdout:utctime,level,tags

utctime printed [2026-11-23T08:40:06.684+0000], which keeps a node's local time zone out of the comparison. Stdout means GC lines interleave with application logs and are lost if the log pipeline drops them; the file on a volume is the more reliable record, stdout the more convenient one. When stdout can block (a slow log driver), add -Xlog:async. Flags reach a container without editing the image through JAVA_TOOL_OPTIONS, which the JVM announces as Picked up JAVA_TOOL_OPTIONS on start.

The JDK 17 default, with no -Xlog at all, is the manual's -Xlog:all=warning:stdout:uptime,level,tags, and VM.log list confirmed #0: stdout all=warning. JEP 158 had specified warnings and errors on stderr; the shipped JDK 17 uses stdout, and this guide follows the manual and the running VM.

Turn it on in a running JVM: jcmd VM.log

No restart is needed. With the 17 binary's help VM.log listing output, output_options, what, decorators, disable, list and rotate:

jcmd 56880 VM.log output=/var/log/app/gc-live.log output_options=filecount=3,filesize=5m 'what=gc*=info' decorators=time,uptime,level,tags
jcmd 56880 VM.log list
jcmd 56880 VM.log rotate
jcmd 56880 VM.log output='#2' what=all=off

The first command answered Command executed successfully and list then showed the new output as #2: file=.../gc-live.log all=off,gc*=info time,uptime,level,tags filecount=3,filesize=5120K,async=false (reconfigured). rotate moved the live file to gc-live.log.0. what=all=off on an output removes it again (what=disable is rejected as an invalid tag), and a bare VM.log disable switches off all logging, warnings included. Two limits: a log started at runtime begins at the next collection, in this test GC(6), so the history before it is gone; and the old flags cannot be set this way, because jcmd <pid> VM.set_flag PrintGCDetails true answers only 'writeable' flags can be set.

JDK 8 flags, and which ones now stop the JVM

JDK 8 logs with separate flags:

-Xloggc:/var/log/app/gc-%t.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps
-XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=20M

The JDK 8 java manual documents -Xloggc, PrintGCDetails, PrintGCDateStamps and PrintGCTimeStamps. The rotation flags are in the JDK 8u flag table rather than the manual: UseGCLogFileRotation "requires -Xloggc", NumberOfGCLogFiles defaults to 0 (no rotation) and GCLogFileSize to 8K. The 8u log-name code expands %p to pid plus the number and %t to a timestamp in -Xloggc names.

Started on JDK 17, those flags split into two groups:

JDK 8 flagOn JDK 17-Xlog equivalent (java manual table)
-XX:+PrintGCwarning, then -Xlog:gc-Xlog:gc
-XX:+PrintGCDetailswarning, then -Xlog:gc*-Xlog:gc*
-Xloggc:old.logwarning, then -Xlog:gc:old.logfile= output
-XX:+PrintGCDateStampsUnrecognized VM option, JVM exitstime decorator
-XX:+PrintGCTimeStampsUnrecognized VM option, JVM exitsuptime decorator
-XX:+UseGCLogFileRotationUnrecognized VM option, JVM exitsrotation is built in
-XX:NumberOfGCLogFiles, -XX:GCLogFileSizeUnrecognized VM option, JVM exitsfilecount=, filesize=
-XX:+PrintGCApplicationStoppedTimeUnrecognized VM option, JVM exits-Xlog:safepoint
-XX:+PrintTenuringDistributionUnrecognized VM option, JVM exits-Xlog:gc+age*=debug
-XX:+PrintHeapAtGCUnrecognized VM option, JVM exits-Xlog:gc+heap=trace
-XX:+PrintAdaptiveSizePolicyUnrecognized VM option, JVM exits-Xlog:gc+ergo*=debug

The warning group still logs. Run together, -Xloggc:both.log -XX:+PrintGCDetails printed both warnings, the first naming -Xlog:gc:both.log, yet the file received the full gc* detail (its third line was [gc,init]), so the old pair keeps working until you convert it. -XX:+IgnoreUnrecognizedVMOptions makes the exiting group start again, as it did here, but it also silences genuine typos in every other flag, so convert the script instead. JEP 271 says plainly that existing GC log parsers were not guaranteed to work on the new format, which is why an analyzer has to read both; the GC Log Analyzer does.

What logging costs

On a stress test collecting about 300 times a second, five interleaved pairs of runs took 1,924 to 3,054 ms without logging and 2,347 to 2,782 ms with gc* to a file: the difference sat inside the run-to-run noise, at a collection rate far above any healthy service. The cost that matters is disk, sized above, and the blocking risk of a slow stdout, which -Xlog:async removes.

Worked example: a log from a JVM on its way out

The demo service from the heap dump guide, a cache that never evicts, ran under the production line with -Xmx128m until it died 30 seconds later. Its log in the GC Log Analyzer, warnings trimmed to their first clause:

285 collections (28 full) over 30.0s  ·  G1  ·  throughput 98.28%
pauses: p50 0.6 ms, p90 7.1 ms, p99 14.7 ms, max 22.9 ms  ·  total 0.52s
WARNING: To-space exhausted: evacuation failed at least once
WARNING: 28 Full GCs in 30s

Throughput of 98.28% looks healthy, and that is the lesson: averages hide a dying JVM. The heap after each collection tells the truth. The lowest post-collection heap in each three-second window climbed from 2 MiB to 20, 34, 47, 58, 73, 84, 95, 107 and 119 MiB, while collections per window went from 4 to 126 as headroom vanished.

Bar chart of the lowest heap after GC in each 3-second window of the example log, climbing from 2 MiB to 119 MiB against a 128 MiB maximum, with collections per window rising from 4 to 126 and 28 full GCs in the last window
Bar chart of the lowest heap after GC in each 3-second window of the example log, climbing from 2 MiB to 119 MiB against a 128 MiB maximum, with collections per window rising from 4 to 126 and 28 full GCs in the last window

A floor that climbs every window is a leak or a live set outgrowing the heap; the first To-space exhausted at 27.383 s uptime and the full-GC burst after it are the end stage. That is the moment for a heap dump, or better, the HeapDumpOnOutOfMemoryError flag set in advance.

Tools for this job

  • Java GC Log Analyzer: drop the log from any of the lines above, unified or JDK 8 format; pause percentiles by cause, throughput and the heap trend.
  • Java Heap Dump Analyzer: when the floor climbs, the dump shows which class is holding the memory.

For reading the numbers, the GC log reading guide covers throughput targets and full-GC causes, and the heap and thread dump analysis guide picks up once a dump exists. If the JVM is stuck rather than collecting, take a thread dump instead.

Frequently asked questions

Is -Xlog:gc enough, or do I need gc*?

Use gc* in production. The plain gc selector prints one line per collection, enough for pause counts and heap before and after. The wildcard adds phases, region counts, metaspace and CPU time, about ten times the bytes, which is what you need to explain a pause rather than only count it.

Why does my JVM refuse to start after a Java upgrade?

Most likely a JDK 8 logging flag such as PrintGCDateStamps or UseGCLogFileRotation. JDK 17 reports Unrecognized VM option and exits, while PrintGCDetails and Xloggc only warn. Replace the whole set with one -Xlog line; rotation and timestamps are built into the decorators and output options.

Can I write the GC log to stdout and a file at once?

Yes. Repeat the option with a different output, for example one -Xlog:gc*:file=gc.log line and one -Xlog:gc:stdout line. Each -Xlog creates its own output with its own selection and decorators, and jcmd VM.log list shows them numbered.

Does GC logging work the same for ZGC and Shenandoah?

The syntax is the same for every collector, since unified logging belongs to the JVM rather than to one collector. The content differs: each collector logs its own phases and pause names, so a gc* log from ZGC reads differently from G1's. Analyzers have to recognise each collector's lines.

Sources and further reading

Browse all tools