Sub topic 10
Reading flame graphs
This chapter assumes you can record a profile with Java Flight Recorder (JFR) and that you know what a stack trace is. A flame graph turns thousands of sampled stack traces into one picture. It shows where the program spends its time.
What the picture means
A flame graph is built from stack samples. Each sample is the call stack of a thread at one moment. The tool merges identical stack prefixes and draws them as boxes.
- The vertical axis is stack depth. The bottom box is the entry point, such as
main. Each box above it is a method called by the box below. - The width of a box is the share of samples that included that method. A wide box means that method, or something it called, was on the stack often.
- The horizontal order has no meaning. Boxes are usually sorted alphabetically, so left to right is not time.
- Colors usually carry no meaning either. Check the legend, because some tools color by package or by Java versus native code.
The key point is that width includes callees. A wide box near the bottom is often just a caller. The code that burns CPU is the box with a wide top edge, meaning few or no boxes sit above it.
Finding the hot spot
- Look for the widest boxes at the top of the graph, not the bottom.
- Compare a box's width to the box directly under it. If the child nearly fills the parent, the parent adds little. The time is in the child.
- Follow the widest path upward until it ends. That leaf is where the CPU time goes.
- Read the names. A leaf like
Arrays.copyOfunderString.concatpoints at the string copying in theSlowprogram from earlier in this tutorial.
In that program, the repeated s += i creates a new string each time. The graph would show a wide tower from main into string concatenation and array copying. That tells you to replace the loop concatenation with a StringBuilder.
Getting a flame graph from JFR
JDK Mission Control (JMC) can show a flame graph view of the Method Profiling Samples. Alternatively, convert the recording with a tool such as async-profiler's converter, or use JFR's own jfr print --events jdk.ExecutionSample app.jfr to inspect raw samples.
jfr print --events jdk.ExecutionSample app.jfr | head -40Each event lists a stack trace, which is the raw material for the graph.
Common reading errors
- Treating width as time spent in that method alone. Width is inclusive of everything it calls.
- Reading left to right as a timeline. Use a time-based view for that.
- Ignoring the sample count. A graph from 50 samples is noisy. Record long enough to collect thousands.
- Mixing event types. A CPU graph and an allocation graph answer different questions. Check which event the graph uses.
- Trusting a biased profiler. Safepoint bias, covered in the previous chapter, can shift width onto the wrong methods.
Exercise
In a flame graph, the box for process() is 80% wide, and the box directly above it, compress(), is 78% wide. Where should you look for the problem, and why?
Show answer
Look at compress() and what sits above it. Width includes callees, so process() is wide only because compress() is. Only about 2% of the samples were spent in process() itself, so it adds little work of its own.