First buffer allocation sets up JFR's metadata repository (~40 ms) even when nothing is recording
Expected behavior
A process that never starts a JFR recording should not pay for JFR's event registration. -Dio.netty.jfr.enabled=false should keep Netty away from jdk.jfr entirely.
Actual behavior
On any JDK with JFR, isJfrEnabled() is true in every process, because it defaults to FlightRecorder.isAvailable() (PlatformDependent.java#L268-L282). So the first chunk (AdaptivePoolingAllocator.java#L1618-L1627) initialises AllocateChunkEvent. HotSpot adds a FlightRecorder.register(Class) call to the <clinit> of every concrete event class not annotated @Registered(false) (jfrEventClassTransformer.cpp#L260-L279). The first registration builds JFR's MetadataRepository: on JDK 25 the first allocation loads 114 jdk.jfr classes and takes about 40 ms longer.
Also, -Dio.netty.jfr.enabled=false still calls isAvailable() (the default is computed before the property is read), loading 6 jdk.jfr classes on JDK 25 and 16 on JDK 21.
Minimal reproducer
public class FirstAlloc {
public static void main(String[] args) {
long t0 = System.nanoTime();
io.netty.buffer.ByteBuf buf = io.netty.buffer.ByteBufAllocator.DEFAULT.directBuffer(256);
buf.writeLong(42L);
buf.release();
System.out.printf("first-alloc-ms=%.3f%n", (System.nanoTime() - t0) / 1e6);
}
}Class path: netty-common and netty-buffer only. Run with and without -Dio.netty.jfr.enabled=false; class counts from -Xlog:class+load:
| JDK | Median first allocation, default → off | Paired diff [95% CI] | jdk.jfr classes |
|---|---|---|---|
| 25.0.4.1 | 111.5 → 70.9 ms | −40.0 ms [−43.3, −36.2] | 114 → 6 |
| 21.0.12.1 | 87.6 → 47.2 ms | −39.6 ms [−41.0, −38.7] | 94 → 16 |
Method: n=30 pairs per JDK, interleaved in random order after 3 warm-up rounds; median of paired differences, 10,000-resample bootstrap CI. Host load was 4–8, so only paired differences count.
Versions
Netty 4.2.18.Final (same code on 4.2 at cd967a41); Homebrew OpenJDK 25.0.4.1 and 21.0.12.1; macOS 26.6.2, Apple M4 Pro (arm64, 12 CPUs).
How we found it
Found while profiling micronaut-runner with its benchmark-large sample. -XX:LogClassLoadingCauseFor=jdk.jfr.internal.MetadataRepository shows, on the event loop in the first request: FlightRecorder.register ← AllocateChunkEvent.<clinit> ← AdaptivePoolingAllocator$Chunk.<init> ← … ← NioByteUnsafe.read.
There, Micronaut's request event is the next trigger (a Netty-only gate saved −2.5 ms [−6.0, +1.9], n=50; Micronaut side: micronaut-projects/micronaut-core#13386). With both gated by a bytecode-patched prototype of option A (fat JAR, Micronaut 5.1.15, JDK 25, n=25 pairs each), spawn to first HTTP 200 improved by −29.6 ms [−36.2, −20.1] uncached and −21.6 ms [−27.5, −12.1] with a JDK AOT cache. Details: https://github.com/alvarosanchez/micronaut-runner/issues/153
Proposed change: which option do you prefer?
In all options, events are registered only once a Flight Recorder exists. A and B also read io.netty.jfr.enabled before touching jdk.jfr. No event is lost: every recording is created through FlightRecorder.getFlightRecorder(), which sets initialized and notifies listeners before it returns (FlightRecorder.java#L155-L186).
A flag computed once in <clinit> is not enough: in the prototype, a jcmd JFR.start after startup then got 0 Netty allocator events instead of 35.
- A. Dynamic
isJfrEnabled():JFR && FlightRecorder.isInitialized(), withtruecached in a plain field. Until a recorder exists, every guarded allocate, reallocate and free does a volatile read that C2 cannot fold. - B. Listener-flipped flag: a
FlightRecorderListenersets a plain static boolean; the hot path does one plain read. The listener must be a separate class: as an anonymous class inPlatformDependent, verification loadsFlightRecorderListenerand throwsNoClassDefFoundErrorwithoutjdk.jfr. - C.
@Registered(false)onAbstractAllocatorEvent(the annotation is@Inherited), plus a listener that registers the five event classes. The hot path is unchanged. The cost: registration runs insidesynchronized (PlatformRecorder.class), the area of JDK-8368670 (#15742). Today it runs on an allocating thread in every process.
With no recording, today's guard can fold to false in C2 (JFR and INSTANCE are static final, isEnabled() is uninstrumented); #15397 kept JFR static final for that. Only C keeps this.
Each option with the reproducer on JDK 25 (n=30, same method; A and B against the unmodified PlatformDependent compiled the same way):
| Option | First allocation | jdk.jfr classes |
|---|---|---|
| A | −40.3 ms [−41.6, −38.9] | 1 |
| B | −41.5 ms [−43.7, −38.7] | 8 |
| C | −38.0 ms [−40.7, −36.8] | 9 |
With each option, a late RecordingStream and a -XX:StartFlightRecording recording got the same event counts as today, and the reproducer runs under --limit-modules java.base,java.logging.
// A and B: PlatformDependent.<clinit>
JFR = SystemPropertyUtil.getBoolean("io.netty.jfr.enabled", true);
// C: PlatformDependent unchanged as measured (JFR still defaults to FlightRecorder.isAvailable(), which C needs). Reading the property first would be JFR = getBoolean("io.netty.jfr.enabled", true) && isAvailable() in try/catch (not measured).
// A
public static boolean isJfrEnabled() { return JFR && isJfrRecorderInitialized(); }
private static boolean jfrRecorderSeen, jfrApiMissing; // plain: `initialized` never reverts
private static boolean isJfrRecorderInitialized() {
if (jfrRecorderSeen) return true;
if (jfrApiMissing) return false;
try {
if (FlightRecorder.isInitialized()) { jfrRecorderSeen = true; return true; }
} catch (Throwable t) { jfrApiMissing = true; } // no jdk.jfr
return false;
}
// B: <clinit> calls a separate holder class's FlightRecorder.addListener(...) in try/catch;
// the listener sets PlatformDependent.jfrRecorderInitialized = true
// C: netty-buffer; initialised only after isJfrEnabled() returned true
@Enabled(false) @Registered(false) @Category("Netty")
abstract class AbstractAllocatorEvent extends Event {
static {
FlightRecorder.addListener(new FlightRecorderListener() {
public void recorderInitialized(FlightRecorder r) {
FlightRecorder.register(AllocateChunkEvent.class); // + the other four
}
});
}
}Compatibility
- A and B change
isJfrEnabled()from "JFR is supported" to "a recorder exists". FlightRecorderTest.java#L38-L41 asserts the old meaning. If the method must keep it, the 10 buffer call sites can use a new internal method instead. jfr-stubneedsisInitialized()(A),addListenerandFlightRecorderListener(B, C), andregisterandRegistered(C). All are JDK 9+ and 8u262+ API.
How to verify
- Forked-JVM test: allocate with no recorder, assert no
MetadataRepositoryis loaded, then start aRecordingStreamand expectio.netty.AllocateChunk.JfrEventsTestshould pass unchanged. - With the flag off, no
jdk.jfrclass is loaded (A and B, or C with the property read first). JfrBenchmarkand the allocator benchmarks, before and after, on x86_64 and AArch64; we will provide these for the chosen option.- Native image: the #15397 case without JFR, and a
--enable-monitoring=jfrbuild whose startup recording still gets allocator events.
We are happy to open the PR once you pick an option.
Source: netty/netty