Turning on GC logs
35 min · Intermediate
Need help? Ask a tutor on WhatsApp (opens in a new tab)A GC log records every collection: when it happened, why, how much memory it freed and how long the program stopped. Since Java 9 one option, -Xlog, turns it on, picks the detail level and sends it to the screen or to a file.
Unified logging: -Xlog:gc
Unified logging is the single logging system of the JVM, added in Java 9. The -Xlog option chooses which messages, such as GC events, are written, and where.
Unified logging is the one logging system for all JVM messages, added in Java 9. You control it with a single option, -Xlog. -Xlog:gc turns on the basic GC log: one line when the JVM picks its collector, and one line for each collection.
Example 1 · A batch job that makes lots of short-lived garbage
import java.util.ArrayList;import java.util.List;
public class MarksheetBatchDemo { public static void main(String[] args) { int passed = 0; long totalChars = 0;
for (int student = 1; student <= 2_000_000; student++) { List<Integer> marks = new ArrayList<>(); // boxed marks: lots of small objects for (int subject = 0; subject < 6; subject++) { marks.add((student * 7 + subject * 13) % 101); } int total = 0; for (int m : marks) { total += m; } String sheet = "Student " + student + " total " + total + "/600"; totalChars += sheet.length(); if (total >= 240) { passed++; } }
System.out.println("Marksheets built: 2000000"); System.out.println("Passed: " + passed); System.out.println("Characters in marksheets: " + totalChars); }}Marksheets built: 2000000 Passed: 1762377 Characters in marksheets: 56888896
What this code does
- Each marksheet builds a small
ArrayListof boxedIntegermarks and aString, then drops them, so the program creates millions of short-lived objects. - The output never changes, whatever logging you turn on: GC logging adds lines about memory, not about your results. The runs below use this program.
Example 2 · The basic GC log on the screen
javac MarksheetBatchDemo.javajava -Xlog:gc MarksheetBatchDemo[0.013s][info][gc] Using G1 [0.167s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->0M(118M) 2.573ms [0.198s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 37M->0M(118M) 1.772ms [0.250s][info][gc] GC(2) Pause Young (Normal) (G1 Evacuation Pause) 69M->0M(118M) 1.574ms [0.287s][info][gc] GC(3) Pause Young (Normal) (G1 Evacuation Pause) 69M->0M(118M) 1.375ms [0.330s][info][gc] GC(4) Pause Young (Normal) (G1 Evacuation Pause) 69M->0M(153M) 2.164ms Marksheets built: 2000000 Passed: 1762377 Characters in marksheets: 56888896
What this code does
- The first line names the collector. Each
GC(n)line is one young collection: the heap went from 69 MB to 0 MB in about 1.5 ms, and the number in brackets is the heap size at that moment (118 MB, later 153 MB). [0.167s]is the time since the JVM started,[info]the level and[gc]the tag. Your timings will differ.
Each message has a tag that says what it is about, such as gc, gc+heap or gc+init, and a level: error, warning, info, debug or trace. -Xlog:gc means "messages tagged exactly gc, at level info or more important". By default they go to the screen (standard output), each line starting with three decorators: the time since the JVM started, the level and the tags. The basic log is cheap, a few lines per second even for a busy service, so many teams keep it on all the time.
More detail: -Xlog:gc*
*`-Xlog:gc** turns on every tag set that starts with gc: start-up settings (gc,init), the phases of each pause (gc,phases), region counts (gc,heap`), metaspace use, CPU time and a heap summary at exit.
Example 3 · Everything about GC with -Xlog:gc*
java -Xmx64m "-Xlog:gc*" MarksheetBatchDemo[0.009s][info][gc,init] CardTable entry size: 512 [0.009s][info][gc ] Using G1 ... [0.010s][info][gc,init] Heap Region Size: 1M [0.010s][info][gc,init] Heap Min Capacity: 8M [0.010s][info][gc,init] Heap Initial Capacity: 64M [0.010s][info][gc,init] Heap Max Capacity: 64M ... [0.113s][info][gc,start ] GC(0) Pause Young (Normal) (G1 Evacuation Pause) [0.113s][info][gc,task ] GC(0) Using 2 workers of 10 for evacuation [0.115s][info][gc,phases ] GC(0) Pre Evacuate Collection Set: 0.1ms [0.115s][info][gc,phases ] GC(0) Merge Heap Roots: 0.0ms [0.115s][info][gc,phases ] GC(0) Evacuate Collection Set: 1.1ms [0.115s][info][gc,phases ] GC(0) Post Evacuate Collection Set: 0.3ms [0.115s][info][gc,phases ] GC(0) Other: 0.2ms [0.115s][info][gc,heap ] GC(0) Eden regions: 23->0(35) [0.115s][info][gc,heap ] GC(0) Survivor regions: 0->1(3) [0.115s][info][gc,heap ] GC(0) Old regions: 0->0 [0.115s][info][gc,heap ] GC(0) Humongous regions: 0->0 [0.115s][info][gc,metaspace] GC(0) Metaspace: 277K(448K)->277K(448K) NonClass: 266K(320K)->266K(320K) Class: 10K(128K)->10K(128K) [0.115s][info][gc ] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->0M(64M) 2.009ms [0.115s][info][gc,cpu ] GC(0) User=0.00s Sys=0.00s Real=0.00s ... Marksheets built: 2000000 Passed: 1762377 Characters in marksheets: 56888896 [0.297s][info][gc,heap,exit] Heap [0.297s][info][gc,heap,exit] garbage-first heap total 65536K, used 33463K [0x00000000fc000000, 0x0000000100000000) [0.297s][info][gc,heap,exit] region size 1024K, 33 young (33792K), 1 survivors (1024K) [0.298s][info][gc,heap,exit] Metaspace used 331K, committed 512K, reserved 1114112K [0.298s][info][gc,heap,exit] class space used 17K, committed 128K, reserved 1048576K
What this code does
- The
gc,initlines show the start-up settings, then each pause has a start line, its phases, region counts before and after, metaspace use, a summary line and CPU time. - The
gc,heap,exitblock at the end shows the heap when the program finished. Lines marked...were trimmed: this short run printed 140 lines.
It is the detail you need to explain why a pause was long, and it is still at the info level. Put quotes around it on the command line: "-Xlog:gc*". Some shells, such as bash on Linux and macOS, try to treat * as a file name pattern. You can go deeper with a level, such as -Xlog:gc+heap=debug, but that is for chasing one specific problem, not for everyday use.
Writing to a file: -Xlog:gc*:file=gc.log
*`-Xlog:gc:file=gc.log** sends the GC log to a file instead of the screen, so it does not mix with your program's own output and survives after the program ends. The full form has four parts separated by colons: -Xlog:<what>:<output>:<decorators>:<output options>`.
Example 4 · Sending the detailed log to a file
java -Xmx64m "-Xlog:gc*:file=gc.log" MarksheetBatchDemohead -3 gc.loggrep "gc ] GC" gc.logMarksheets built: 2000000 Passed: 1762377 Characters in marksheets: 56888896 [0.013s][info][gc,init] CardTable entry size: 512 [0.013s][info][gc ] Using G1 [0.015s][info][gc,init] Version: 21.0.7+9-b895.130 (release) [0.171s][info][gc ] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->0M(64M) 2.914ms [0.207s][info][gc ] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 35M->0M(64M) 2.097ms ...
What this code does
- With
file=gc.log, the screen shows only the program's own output; the GC lines are ingc.login the current folder. grepwith the padded taggc ]keeps just the one-line summary of each pause. On Windows, usetype gc.logor PowerShell'sGet-Content gc.log -TotalCount 3instead ofhead.
Example 5 · Date stamps, rotation and a file per process
java -Xmx64m "-Xlog:gc:file=gc-%p.log:time,uptime,level,tags:filecount=5,filesize=10m" MarksheetBatchDemolshead -3 gc-<pid>.logMarksheets built: 2000000 Passed: 1762377 Characters in marksheets: 56888896 MarksheetBatchDemo.class gc-<pid>.log gc.log [2026-10-16T00:05:22.068+0530][0.017s][info][gc] Using G1 [2026-10-16T00:05:22.242+0530][0.191s][info][gc] GC(0) Pause Young (Normal) (G1 Evacuation Pause) 23M->0M(64M) 2.101ms [2026-10-16T00:05:22.284+0530][0.233s][info][gc] GC(1) Pause Young (Normal) (G1 Evacuation Pause) 35M->0M(64M) 1.938ms
What this code does
time,uptime,level,tagsadds the wall-clock date and time (with the time zone, +0530 for India) in front of the uptime.%pbecame the process id, shown here as<pid>;filecount=5,filesize=10mkeeps at most 5 old files of 10 MB besides the current one.
The decorators part lets you add a wall-clock date and time (time) next to the uptime, which helps when you compare the log with an incident at 10:02 am. Output options control rotation: filecount=5,filesize=10m keeps a few files of at most 10 MB each, so the log can never fill the disk. File output rotates by default too, with 5 files of 20 MB (checked with jcmd <pid> VM.log list on Java 21). In the file name, %p becomes the process id and %t the start date and time, so two runs or two JVMs on one server do not overwrite each other's log.
Java 8 flags and what replaced them
Before Java 9, GC logging used separate flags such as -XX:+PrintGCDetails, -XX:+PrintGCDateStamps and -Xloggc:gc.log. You still find them in old scripts and tutorials.
Example 6 · Old Java 8 flags on newer JVMs
java -Xmx64m -XX:+PrintGCDetails MarksheetBatchDemo # Java 21java -Xmx64m -Xloggc:gc-old.log MarksheetBatchDemo # Java 21java -XX:+PrintGCDateStamps MarksheetBatchDemo # Java 19[0.006s][warning][gc] -XX:+PrintGCDetails is deprecated. Will use -Xlog:gc* instead. [0.014s][info ][gc,init] CardTable entry size: 512 [0.014s][info ][gc ] Using G1 ... [0.007s][warning][gc] -Xloggc is deprecated. Will use -Xlog:gc:gc-old.log instead. Marksheets built: 2000000 ... Unrecognized VM option 'PrintGCDateStamps' Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
What this code does
-XX:+PrintGCDetailsand-Xloggcstill work on Java 21 with a warning, and the warning even tells you the-Xlogoption to use instead.-XX:+PrintGCDateStampswas removed in Java 9, so a standard JDK refuses to start (this run used Java 19). Some vendor builds are set to ignore unknown options, so never rely on an old flag doing anything.
-XX:+PrintGC→-Xlog:gc-XX:+PrintGCDetails→-Xlog:gc*-Xloggc:gc.log→-Xlog:gc:file=gc.log(orgc*for details)-XX:+PrintGCDateStamps/-XX:+PrintGCTimeStamps→ thetimeanduptimedecorators
On Java 9 and later (checked on 19 and 21), three of them still work but print a deprecation warning and are mapped to -Xlog. The time-stamp flags were removed: the JVM refuses to start with "Unrecognized VM option". When you move an old start script to Java 17 or 21, replace them all:
Next step
Check your understanding
12 quick questions