Sub topic 8
Finding CPU hot spots
This chapter assumes you can start a JFR recording (previous chapter), open it in JDK Mission Control, and read a stack trace. It also assumes you have a measurable goal, such as "reduce the CPU time of this batch job by half."
What a hot spot is
A CPU hot spot is a method, or a call path, where the program spends a large share of its running time. A sampling profiler finds it by capturing thread stacks at regular intervals. Methods that appear in many samples are where the CPU time goes.
JFR's jdk.ExecutionSample event records these samples. With the profile settings it samples Java threads about every 10 ms. Read the results as proportions, not exact timings.
A program with an obvious hot spot
Use the slow program from the spine. The inner loop does s += i, which copies the whole string on every iteration.
import java.util.ArrayList;
import java.util.List;
public class Slow {
public static void main(String[] args) {
long start = System.nanoTime();
for (int round = 0; round < 200; round++) {
String s = "";
for (int i = 0; i < 5_000; i++) {
s += i;
}
if (s.length() == 0) {
System.out.println("unreachable");
}
}
System.out.println((System.nanoTime() - start) / 1_000_000 + " ms");
}
}Compile it and record it:
javac Slow.java
java -XX:StartFlightRecording=duration=60s,filename=app.jfr,settings=profile SlowThe program ends before 60 seconds. JFR still writes app.jfr when the JVM exits.
Reading the profile
Open app.jfr in JDK Mission Control and go to the Java Application page, then the Method Profiling view. Two views matter here.
- Hot methods (top frames). Group samples by the method at the top of the stack. This shows where the CPU is executing right now. For
Slow, expect string-building and array-copy methods near the top. - Call tree or stack trace view. Group samples by the full stack. This shows who called the hot method. Here the path leads back to
Slow.main.
The top frame tells you what is expensive. The callers tell you which of your own code to change. A hot JDK method is rarely the thing to fix. Look for the nearest frame that is your code.
Common errors
- Reading a short recording. Few samples give noisy percentages. Record long enough to collect thousands of samples, and under realistic load.
- Profiling an idle application. If threads wait on I/O or locks, they do not show up as CPU samples. A low CPU profile can mean the problem is blocking, which a later chapter covers.
- Fixing the biggest bar regardless of the goal. A method with 30% of samples can only save 30% at most. Check that its share is worth the effort.
- Ignoring warmup. Early samples include interpreted and compiling code. Skip the first seconds of a recording when you compare runs.
Exercise
A JFR profile shows 40% of samples in java.util.Arrays.copyOf, called from Slow.main. Which method do you change, and what is the most you can save?
Show answer
Change Slow.main, the nearest application code that causes the copying, for example by using a StringBuilder. Removing all of the copyOf time would save at most about 40% of the CPU time, so the real gain is bounded by that share.