Sub topic 7
Sharing data with @State
This chapter assumes you can write a basic @Benchmark method, build the JMH jar with Maven, and read annotations. It covers how JMH holds the data your benchmarks use.
Why state needs its own object
A benchmark method runs many times, on one or more threads, in several forks. If you create the input inside the method, the benchmark measures the setup too. If you keep it in a static field, you lose control over who shares it. JMH solves this with state objects. A state object is a class annotated with @State. JMH creates it, injects it, and manages its lifetime.
You can annotate the benchmark class itself, as SumBenchmark does. You can also write a separate state class and take it as a method parameter.
The three scopes
The scope decides how many instances exist and who sees them.
| Scope | Instances | Typical use |
|---|---|---|
| `Scope.Benchmark` | One, shared by all threads | Read-only data, or a structure you test for contention |
| `Scope.Thread` | One per thread | Mutable data that each thread changes on its own |
| `Scope.Group` | One per thread group | Asymmetric benchmarks that use `@Group` |
Scope.Benchmark is the usual choice for read-only input such as the array in SumBenchmark. Use Scope.Thread when the benchmark changes the data, because shared mutable state across threads gives wrong results and measures cache contention.
A separate state class
A state class needs a public no-argument constructor and must be a public class. Nested state classes must be static.
package org.sample;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
public class SharedStateBenchmark {
@State(Scope.Benchmark)
public static class Numbers {
public int[] data = new int[1_000];
public Numbers() {
for (int i = 0; i < data.length; i++) {
data[i] = i;
}
}
}
@State(Scope.Thread)
public static class Counter {
public long count;
}
@Benchmark
public long sum(Numbers numbers) {
long total = 0;
for (int value : numbers.data) {
total += value;
}
return total;
}
@Benchmark
public long increment(Counter counter) {
counter.count++;
return counter.count;
}
}JMH sees the Numbers and Counter parameters and supplies instances. Both are created before measurement starts, so their construction time is not counted.
You can request several state objects in one method. Each one follows its own scope.
Fields, not constants
State fields also affect what the JIT can assume. A value read from a field of a state object is not a compile-time constant, so the JIT cannot fold the computation away. That is why the earlier examples read x from a field instead of writing Math.PI inline. The next chapters on dead-code elimination and constant folding build on this.
Common mistakes
- A non-public state class or a non-static nested class. JMH fails at build time or reports a generation error.
- Initializing in the constructor when the work depends on a
@Paramvalue. Parameters are injected after construction, so use a@Setupmethod instead. The next chapter covers it. - Using
Scope.Benchmarkwith@Threadsgreater than 1 and then changing the shared object.
Exercise
A benchmark increments a counter field on its state object, and you run it with two threads. Which scope should the state class use so that each thread updates its own counter, and why?
Show answer
Use Scope.Thread. JMH then creates one instance per thread, so the threads do not share the counter. With Scope.Benchmark both threads would update one object, which causes data races and contention that distort the result.