Quick revision
The short answer
I would use sampling profilers that are safe to run in production. Java Flight Recorder is built into the JDK and can run continuously at low overhead: we start it with jcmd <pid> JFR.start and dump a recording when needed. async-profiler produces CPU, allocation and wall-clock flame graphs, also with low overhead. We read flame graphs by width, because wide frames are where the time goes, profile under real or realistic load, and avoid instrumenting profilers that change timing heavily.
What to remember
In this answer, we saw that production profiling needs sampling profilers with low overhead. Java Flight Recorder is built into the JDK, can run continuously and be dumped or started with jcmd; async-profiler gives CPU, allocation and wall-clock flame graphs. We read flame graphs by width, with wide frames showing where time goes, we profile under real or realistic load because idle profiles mislead, and we avoid instrumenting profilers whose overhead changes the timing we want to measure.
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.
I would use sampling profilers that are safe to run in production. Java Flight Recorder is built into the JDK and can run continuously at low overhead: we start it with jcmd <pid> JFR.start and dump a recording when needed. async-profiler produces CPU, allocation and wall-clock flame graphs, also with low overhead. We read flame graphs by width, because wide frames are where the time goes, profile under real or realistic load, and avoid instrumenting profilers that change timing heavily.
Why this question is asked
Performance problems that only appear in production are common: the data is larger, the traffic mix is different and the slow path only runs at peak. Guessing from code is slow and often wrong, so the right move is to look at where the CPU and allocations actually go. Interviewers ask this to see whether we know the tools that are safe in production and how to read what they produce. In this answer, we will recap sampling and instrumenting profilers, use JFR and async-profiler on a running Spring Boot application, learn to read a flame graph and see why load matters.
1A Quick Recap: Sampling Versus Instrumenting
A profiler tells us where a program spends its time or allocates its memory. There are two ways to do it. An instrumenting profiler adds timing code around methods, so it records every call exactly. That sounds precise, but the added code changes the timing it measures, especially for small methods called millions of times, and the overhead can be large enough to slow production badly.
A sampling profiler instead looks at the threads at regular intervals, for example every 10 or 20 milliseconds, and records which stack each thread is in. A method that appears in 30 percent of the samples is using about 30 percent of the time. The overhead is small and predictable, which is why sampling is the right choice in production.
2Java Flight Recorder: Built In and Always Available
Java Flight Recorder, JFR, is part of the JDK. It records events such as method samples, allocations, garbage collection, locks and I/O into a compact binary file, with an overhead usually around 1 percent with the default settings. We can start it when the application starts and keep the last few hours in a rolling buffer, or start it on demand with jcmd, the JDK's command tool for a running JVM.
The profile settings collect more detail than default at a somewhat higher cost, so we use them for short targeted recordings. For a full investigation we open the file in JDK Mission Control, which shows hot methods, allocation pressure, GC pauses and lock contention side by side. Notice that the always-on recording means we can dump the last hours after an incident, without having to reproduce it.
3Async-profiler and Flame Graphs
async-profiler is an open-source sampling profiler for the JVM that attaches to a running process and produces flame graphs directly. It can sample CPU, allocations, locks and wall-clock time, which includes time threads spend waiting on I/O and locks. It also avoids safepoint bias, a distortion where some profilers can only sample threads at certain points in the code and so blame the wrong methods.
In containers it may need permission to use the kernel's performance events, or a fallback sampling mode; check the version and the environment's security settings before an incident, not during one. Older releases used a script called profiler.sh instead of asprof.
4Reading a Flame Graph
A flame graph shows sampled stacks as stacked boxes. Each box is a method, the box above it is a method it called, and the width of a box is the share of samples in which that method was on the stack. The x-axis is not time; boxes are sorted alphabetically, so left and right mean nothing.
So we read by width. A wide box near the top, with nothing above it, is a method burning CPU itself. A wide box lower down with many narrow boxes above it is a method whose callees together cost a lot. In a Spring Boot application, the bottom is Tomcat's thread and the filter chain, the middle is our controllers and services, and the interesting part is usually a wide plateau like Jackson serialization, a regular expression or Hibernate's flush.
| Level | Frame | Width (share of samples) |
|---|---|---|
| top | Pattern.matcher / Matcher.find | 38% <- burning CPU itself |
OrderValidator.validateNotes | 40% | |
OrderService.create | 55% | |
OrderController.post | 60% | |
DispatcherServlet / filter chain | 62% | |
| bottom | Tomcat worker thread | 100% of request samples |
5Profile Under Real Load
An idle application's profile is misleading. With no traffic, the samples show the scheduler, metrics collection and background threads, and the slow path we care about never runs. So we profile when the problem happens, during peak traffic in production or under a realistic load test. That is exactly why low overhead matters: we need a tool we are allowed to run at the worst moment.
We also profile for long enough, 30 seconds to a few minutes, so that the sample count is high and rare paths show up. And we compare: a flame graph from today next to one from last week's version often shows the regression at a glance.
6Follow-up questions
What is the difference between a CPU profile and a wall-clock profile?
A CPU profile only samples threads that are running on a CPU, so it shows what burns processor time. A wall-clock profile samples threads whatever they are doing, including waiting for a database, a lock or a connection from a pool. For a slow endpoint whose CPU is low, the wall-clock profile is the one that shows where the time goes.
Why profile allocations if CPU looks fine?
Because high allocation rates cause frequent garbage collection, which costs CPU and pauses spread over every request. An allocation profile shows which code creates the most garbage, for example building large strings for logging that is turned off, so we can remove it.
Is it safe to leave JFR on all the time?
With the default settings the overhead is typically around 1 percent, and many teams run it continuously with a size or age limit on the recording. The profile settings and very detailed custom settings cost more, so we use those for short recordings.
7Conclusion
In this answer, we saw that production profiling needs sampling profilers with low overhead. Java Flight Recorder is built into the JDK, can run continuously and be dumped or started with jcmd; async-profiler gives CPU, allocation and wall-clock flame graphs. We read flame graphs by width, with wide frames showing where time goes, we profile under real or realistic load because idle profiles mislead, and we avoid instrumenting profilers whose overhead changes the timing we want to measure.
Open with
In production I would only use sampling profilers, because instrumenting profilers add timing code to every method and distort exactly what I am measuring. My first tool is Java Flight Recorder, since it is in the JDK.
Then, if they want more
I can run it continuously with StartFlightRecording and a six-hour rolling buffer at around one percent overhead, and dump it with jcmd JFR.dump after an incident, or start a short recording with jcmd JFR.start using the profile settings. Then I look at hot methods and allocations with jfr view or in Mission Control.
For flame graphs I use async-profiler: asprof with event cpu for CPU, alloc for allocations and wall for wall-clock time, which shows threads waiting on I/O, locks or pools. It also avoids safepoint bias.
Reading a flame graph, the width of a frame is its share of samples and the x-axis is not time, so I look for the widest frames, especially a wide frame with nothing above it, which is burning CPU itself.
And I profile under real load, at peak or in a realistic load test, because an idle application's profile only shows background threads.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. 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 "How would you profile CPU usage and allocations of a Spring Boot application running in...": https://scrivio-interview-library.netlify.app/spring-boot/service-performance/ed965c7830/