Quick revision
The short answer
We write a class that extends jdk.jfr.Event, give it a @Name, a @Label and fields such as orderType, and around each pricing call we create the event, call begin(), do the work, fill in the fields and call commit(), which records the duration for us. When no recording is running, the enabled check fails at once and the JIT can remove almost all of the event code, so the cost is close to zero; we only have to guard expensive field values with shouldCommit().
What to remember
In this answer, we wrote a custom JFR event by extending jdk.jfr.Event with a stable @Name, a @Label and fields for the order type and line count. Around each pricing call we created the event, called begin() and end(), filled in the fields only when shouldCommit() returned true, and called commit(), which records the duration. A threshold, set by annotation or by the recording's settings, keeps only slow calls. When JFR is not recording, the enabled check fails and the JIT can remove both the event code and its allocation, so the only cost left is any work we put outside the shouldCommit() block.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We write a class that extends jdk.jfr.Event, give it a @Name, a @Label and fields such as orderType, and around each pricing call we create the event, call begin(), do the work, fill in the fields and call commit(), which records the duration for us. When no recording is running, the enabled check fails at once and the JIT can remove almost all of the event code, so the cost is close to zero; we only have to guard expensive field values with shouldCommit().
Why this question is asked
Java Flight Recorder, JFR for short, is the event recorder built into the JVM. It already records garbage collections, lock waits, file and socket reads and much more, but it knows nothing about our business. A custom event lets us put our own operations, such as pricing an order, on the same timeline as those JVM events, so that when a pricing call is slow we can see in one recording whether a GC pause, a lock or a slow socket read happened at the same moment. Interviewers ask this to see whether we can add cheap, permanent instrumentation instead of temporary log lines. In this answer, we will recap what a JFR event is, write the event class, use it around the pricing call, add a threshold so only slow calls are kept, and then look closely at what the code costs when JFR is not recording.
1A Quick Recap of JFR Events
JFR records events. An event is a small record with a timestamp, the thread that produced it, an optional stack trace and a few fields. Some events are instant, such as a class being loaded, and some have a duration, such as a GC pause or a socket read. JFR writes events into thread-local buffers and moves them to disk in the background, so the producing thread never waits for I/O.
Every event type has settings that a recording can change: whether it is enabled, a threshold (only keep events that lasted at least this long), whether to capture a stack trace, and for some events a sampling period. Those settings live in configuration files with the .jfc extension.
A custom event is simply a Java class that extends jdk.jfr.Event. Its fields become the event's fields, and annotations describe it.
2Writing the Event Class
We want one event per pricing call, carrying the duration, the order type and, as an extra example, the number of lines in the order. JFR fields can be primitives, String, Thread or Class, so we store the order type as its name rather than as our own enum.
Notice that we do not declare a duration field. Every event already has a start time and a duration, and JFR fills them in for us. The @Name is worth choosing carefully: it is the identifier that .jfc files, command-line settings and queries use, so we treat it like a public API and use a reverse-domain name that will not change when we rename the class.
3Recording Each Pricing Call
Using the event follows a fixed pattern: create it, call begin() just before the work, call end() just after it, set the fields, and call commit(). The commit() call is what actually writes the event. If we forget end(), commit() takes the end time itself, but calling end() explicitly makes the measured duration exactly the work and not the time spent computing fields.
8 messages in order. Play them, or step through them one at a time.
The thread never waits for disk: commit() only copies the event into a buffer.
The finally block matters. If pricing throws, we still want to see that call in the recording, because the slow or failing calls are exactly the ones we will look for after an incident. The shouldCommit() check matters too. It returns true only when the event is enabled in a running recording and the duration passed the threshold. Here the fields are cheap, but if a field needed real work, for example building a summary string of the order, the check makes sure we only pay for it when the event will really be stored.
To see the result, we take a recording and run jfr print --events com.shop.OrderPricing recording.jfr, which shows each call's start time, duration, thread, orderType and lineCount.
4Keeping Only the Slow Calls With a Threshold
A pricing service might handle 2,000 calls per second. Recording every call is possible, but most of them are fast and uninteresting, and they fill the recording's disk budget. A threshold solves this. With a threshold of 50 ms, JFR keeps only calls that took 50 ms or more, and shouldCommit() returns false for the rest, so we do not even fill in their fields.
The @Threshold annotation on the class gives the default, and a custom .jfc settings file overrides it for a whole recording. Recent JDKs can also take event settings directly in the -XX:StartFlightRecording option; check your version for the syntax.
So the decision is made by the recording's settings, not by our code, and an engineer can lower the threshold for a new recording without a deployment.
5What It Costs When JFR Is Not Recording
This is the part interviewers care about most. When no recording is running, or when our event is disabled, begin(), end(), shouldCommit() and commit() each start with a check of whether the event is enabled, and that check fails. The JVM generates and instruments the event's code so that this check is a read of a single flag, and the JIT compiler can treat it as nearly constant. Once the check is known to fail, the rest of each method is dead code.
There is a second saving. The event object is created and used only inside price(). It never escapes the method, so the JIT's escape analysis, the optimisation that notices an object never leaves the method that created it, can remove the allocation entirely. In compiled code, the new OrderPricingEvent() line then allocates nothing at all.
But there is a catch. The JIT can remove the event's own work, not ours. If we compute a field value before shouldCommit(), for example event.summary = order.toString(), that string is built on every call whether or not anyone records it. This is the reason the pattern puts all field computation inside the shouldCommit() block. When a recording is running, the cost per committed event is small, but we still measure it under our own load.
| Situation | What happens | Rough cost |
|---|---|---|
| No recording running | enabled check fails, JIT removes event code and allocation | close to zero |
| Recording running, call below threshold | begin/end take timestamps, shouldCommit() returns false | a few timestamp reads |
| Recording running, call above threshold | fields set, event copied to thread-local buffer | small, measure it |
Field computed before shouldCommit() | our own code runs on every call | whatever that code costs |
shouldCommit() block.6Follow-up questions
Why not just log the duration of each pricing call?
A log line costs formatting, I/O and log storage on every call, and it lives in a separate system from JVM data. A JFR event costs almost nothing when disabled, can be filtered by threshold without code changes, and sits on the same timeline as GC pauses, lock waits and socket reads in the same recording, which is what we need to explain a slow call.
Can we consume these events live instead of from a file?
Yes. Since JDK 14, JFR event streaming lets code in the same JVM subscribe to events with RecordingStream, for example to turn OrderPricing durations into a latency metric. Recent JDKs can also stream from a remote JVM over JMX. Streaming reads the recording's chunks as they are written, so there is a delay of about a second.
7Conclusion
In this answer, we wrote a custom JFR event by extending jdk.jfr.Event with a stable @Name, a @Label and fields for the order type and line count. Around each pricing call we created the event, called begin() and end(), filled in the fields only when shouldCommit() returned true, and called commit(), which records the duration. A threshold, set by annotation or by the recording's settings, keeps only slow calls. When JFR is not recording, the enabled check fails and the JIT can remove both the event code and its allocation, so the only cost left is any work we put outside the shouldCommit() block.
Open with
I would create a class OrderPricingEvent that extends jdk.jfr.Event, annotate it with a stable @Name like com.shop.OrderPricing, a @Label and a @Category, and give it fields such as String orderType and int lineCount.
Then, if they want more
In the pricing method I create the event, call begin, run the pricing in a try block, and in finally call end, then check shouldCommit, set the fields and call commit. I do not declare a duration field, because every event already has start time and duration and commit records them.
For volume, I set a threshold, either with the @Threshold annotation or in a custom .jfc file, so in production only calls above, say, 50 milliseconds are kept.
On cost: when no recording is running, every event method starts with an enabled check that fails, the JIT treats it almost like a constant and removes the rest, and escape analysis can remove the event allocation, so the overhead is close to zero.
The one thing it cannot remove is work I do myself before shouldCommit, which is why I compute field values only inside that check.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a custom JFR event that records the duration of each order pricing call together...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/c6304773bd/