Sub topic 16
Explaining results with profilers
This chapter assumes you can run a JMH benchmark jar and read the score, error and units. It uses the SumBenchmark project from earlier chapters.
Why a score is not an explanation
JMH tells you that one benchmark is slower than another. It does not tell you why. Profilers answer the why. A profiler attaches to the forked benchmark JVM, collects extra data during the measurement iterations, and prints it after the normal result.
Listing the available profilers
Run the jar with -lprof to see which profilers your JVM and operating system support.
java -jar target/benchmarks.jar -lprofCommon choices:
| Profiler | Option | What it shows |
|---|---|---|
| GC | `-prof gc` | Allocation rate and garbage collection counts |
| Stack | `-prof stack` | Where threads spend time, sampled from stack traces |
| Classloading | `-prof cl` | Classes loaded and unloaded |
| Compiler | `-prof comp` | Time spent in JIT compilation |
Some profilers, such as perfasm, need Linux perf and extra setup. Start with gc and stack, which work everywhere.
Using the GC profiler
The GC profiler is the one to try first. Allocation costs time, and a benchmark that creates many objects often measures the garbage collector as much as your code.
This benchmark allocates a new array on every call. It returns the array so the JIT cannot prove the allocation is unused.
package org.sample;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Benchmark)
public class AllocBenchmark {
@Param({"100", "10000"})
public int size;
@Benchmark
public int[] allocate() {
return new int[size];
}
}Run it with the profiler:
java -jar target/benchmarks.jar AllocBenchmark -wi 3 -i 5 -f 1 -prof gcEach benchmark result now has extra secondary lines:
gc.alloc.rateis the allocation rate in MB/sec.gc.alloc.rate.normis the bytes allocated per operation (B/op).gc.countis the number of collections during measurement.gc.timeis the total time spent in those collections, in milliseconds.
gc.alloc.rate.norm is the most useful of these because it does not depend on how fast the benchmark ran. For size = 100, expect about 416 B/op on a typical 64-bit HotSpot JVM. That is 400 bytes of int data plus a 16-byte array header. If your number is far from your estimate, the JIT has changed your code, either by removing an allocation or by adding one you did not expect.
Interpreting what you see
- A low score with
gc.alloc.rate.normnear zero means the code allocates nothing, so garbage collection is not the cause. - A high B/op that you did not expect points to boxing, temporary strings or iterators.
- A rising
gc.countwith a high allocation rate means collection time is part of your score.
Exercise
A benchmark runs at 80 ns/op. With -prof gc it shows gc.alloc.rate.norm of 0.001 B/op. Is garbage collection a likely cause of the 80 ns?
Show answer
No. Almost nothing is allocated per operation, so collection cannot account for the time. Look elsewhere, for example with -prof stack, or check the algorithm itself.