Video in progress
How do you turn on the GC log, and what do you read from it first?
In Java 9+ with a unified logging flag like -Xlog:gc*:file=gc.log. In the log I first look at the pause duration, how much the heap shrinks after each GC, and whether there is a Full GC.
📖 Answer
Java 8 had flags like -XX:+PrintGCDetails. Since Java 9 they have been replaced by unified logging:
java -Xlog:gc*:file=/logs/gc.log:time,uptime,level,tags:filecount=5,filesize=20m \
-jar app.jar
The file rotates (5 files, 20 MB each), so it won't fill the disk. The overhead is very small, and leaving it on all the time in prod is common practice.
A normal line looks like this:
[2024-05-02T10:15:03.120+0500][gc] GC(87) Pause Young (Normal) (G1 Evacuation Pause) 620M->140M(1024M) 11.204ms
What you read from it:
11.204msis the pause. Does it fit your latency requirement?620M->140Mis before and after the GC. Is the second number growing over time?(1024M)is the current heap size;Pause Fulllines should not exist at all.
If it is hard to read by hand, you can load the file into a tool like GCeasy or GCViewer and look at it as graphs.
Sign up to mark studied questions and save your progress.