Quick revision
The short answer
CPU mode samples only threads that are running on a CPU, so it tells us where CPU time goes; wall-clock mode samples threads in every state, including waiting on I/O, locks or sleeps, so it tells us where elapsed time goes. We use wall-clock when requests are slow but CPU is low, for example because of slow downstream calls or lock contention, and we filter the profile to request-handling threads, because idle pool threads otherwise dominate it.
What to remember
In this answer, we saw that CPU mode samples only threads that are running on a CPU, so it shows where CPU cycles go, while wall-clock mode samples threads in every state, so it shows where elapsed time goes, including waiting on I/O, locks and sleeps. When latency is high but CPU is low, such as with a slow downstream service or lock contention, wall-clock mode finds the cause that CPU mode cannot see. And because idle pool threads dominate an unfiltered wall-clock profile, we split it by thread and look only at the request threads.
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.
CPU mode samples only threads that are running on a CPU, so it tells us where CPU time goes; wall-clock mode samples threads in every state, including waiting on I/O, locks or sleeps, so it tells us where elapsed time goes. We use wall-clock when requests are slow but CPU is low, for example because of slow downstream calls or lock contention, and we filter the profile to request-handling threads, because idle pool threads otherwise dominate it.
Why this question is asked
A profiler answers the question we ask it, and choosing the mode is choosing the question. Many engineers profile a slow service in CPU mode, see a flame graph full of ordinary code, and conclude there is nothing to fix, while the real time is spent waiting on a database. Interviewers ask this question to see whether we understand the difference between time spent working and time spent waiting, which is at the heart of most latency investigations. In this answer, we will recap how a sampling profiler works, see exactly what each mode samples, walk through a case where CPU mode misses the problem, and learn how to read a wall-clock profile without being misled by idle threads.
1A Quick Recap of Sampling Profilers
A sampling profiler does not measure every method call. Instead, at regular intervals, for example every 10 ms, it stops to look at what threads are doing and records their stack traces, the chain of method calls from the thread's entry point down to the method currently running. After thousands of samples, a method that appears in 30 percent of the stacks is responsible for about 30 percent of the time, in whatever sense of time the samples represent.
The results are usually shown as a flame graph. Each box is a method, the width of a box is the share of samples in which that method was on the stack, and the boxes above it are the methods it called. Wide boxes are where time goes. The key question for this answer is what we mean by time, and that depends on which threads get sampled.
2What CPU Mode Samples
In CPU mode, the profiler is driven by CPU time. Tools such as async-profiler use a timer or a hardware counter that fires after a thread has consumed a slice of CPU, so a sample is taken only from a thread that is actually running on a processor. A thread blocked in a socket read, parked waiting for a lock, or sleeping consumes no CPU and therefore produces no samples.
So a CPU profile answers the question of where our CPU cycles go. That is exactly what we want when the service uses a lot of CPU, for example when a container is throttled, when we want to reduce cloud cost, or when a flame graph shows a hot regular expression or JSON serialisation. It is the wrong question when the service is slow but mostly idle.
3What Wall-Clock Mode Samples
In wall-clock mode, the profiler samples threads at regular intervals of real elapsed time, regardless of their state. A thread running code is sampled, but so is a thread waiting for a socket, a thread blocked on a synchronized block, a thread parked in a connection pool waiting for a free connection, and a thread sleeping. Each of them shows up with the stack where it is waiting.
So a wall-clock profile answers the question of where elapsed time goes for each thread. For a request thread, elapsed time is the latency the user sees. If a request takes 800 ms and 700 ms of that is waiting for a downstream HTTP call, the wall-clock profile of that thread shows a wide box under the socket read, while a CPU profile would barely show the request at all.
| CPU mode | Wall-clock mode | |
|---|---|---|
| Samples a thread when | it is running on a CPU | at every interval, in any state |
| Shows waiting on I/O or locks | no | yes, at the waiting stack |
| Answers | where do CPU cycles go? | where does elapsed time go? |
| Use it when | CPU is high, throttling, cost | latency is high but CPU is low |
| Main trap | misses waiting entirely | idle threads dominate the graph |
4A Slow Service With Low CPU
Let us take a concrete case. An order API has a p99 latency of 900 ms, but its pods use only 15 percent of their CPU. We run async-profiler in CPU mode for 30 seconds and in wall-clock mode for 30 seconds under the same traffic.
The CPU profile is not wrong. It honestly shows that the little CPU we use goes to serialisation and Hibernate, but that is not why requests are slow. The wall-clock profile shows that request threads spend most of their time waiting for the pricing service and for a database connection. So the fix lies there: a slow dependency, a missing timeout, a connection pool that is too small, or calls made one after another that could run in parallel.
The same pattern appears with lock contention. If 200 threads serialise through one synchronized method, CPU stays low while latency climbs, and wall-clock mode shows a wide box at the monitor enter. async-profiler also has a lock mode that samples contended lock acquisitions specifically, which is useful once wall-clock has pointed us there.
5Filtering Out Idle Threads
Wall-clock mode has one big trap. It samples every thread, and most threads in a typical service are idle most of the time: Tomcat's pool might have 200 threads with only 20 busy, Kafka consumers wait in poll(), scheduler threads sleep, and GC and JIT helper threads wait for work. In an unfiltered wall-clock flame graph, the widest boxes are often LinkedBlockingQueue.take or Thread.sleep, which tells us nothing about latency.
So we always look at the threads that serve requests. With async-profiler, the -t option splits the profile by thread so each thread name becomes a root of the flame graph, and we zoom into the request threads; recent versions also offer ways to filter which threads are profiled, so check your version's options. The other approach is to read the profile from the request entry point downward: we find the servlet or controller frame and look only at what happens below it, which removes idle threads automatically because they never pass through that frame.
- Is CPU high or throttled?YesStart with CPU modeNoStart with wall-clock mode
- Wall-clock with -tsplit by thread
- Keep only request threadsor read below the controller frame
- Wide box at I/O or a pool?YesLook at dependencies, timeouts, pool sizesNoCheck locks with lock mode
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Can wall-clock mode be more expensive than CPU mode?
Yes, because it samples every thread, including idle ones, at each interval. With thousands of threads that adds up, which is why wall-clock mode usually uses a longer default interval than CPU mode. Limiting it to request threads or using a longer interval keeps it cheap; check your profiler version's defaults.
What about virtual threads?
With virtual threads, a blocked request is unmounted from its carrier thread, so a carrier-level wall-clock profile can miss where the request is waiting. JFR events for socket reads, monitor waits and pinning, and profilers with explicit virtual thread support, help here. Support differs between tools and versions, so check before relying on it.
Does JFR have both modes?
JFR's execution samples cover threads running Java code, which is close to CPU mode, and separate native method samples cover threads in native code such as socket reads. Its duration events, such as socket read, monitor enter and thread park above a threshold, give a precise view of waiting. Recent JDKs also add a CPU-time sampler on Linux; check your version.
7Conclusion
In this answer, we saw that CPU mode samples only threads that are running on a CPU, so it shows where CPU cycles go, while wall-clock mode samples threads in every state, so it shows where elapsed time goes, including waiting on I/O, locks and sleeps. When latency is high but CPU is low, such as with a slow downstream service or lock contention, wall-clock mode finds the cause that CPU mode cannot see. And because idle pool threads dominate an unfiltered wall-clock profile, we split it by thread and look only at the request threads.
Open with
The difference is which threads get sampled. In CPU mode the profiler takes a sample only from a thread that is running on a CPU, so a thread waiting on a socket, a lock or a sleep never shows up.
Then, if they want more
That makes it the right mode when CPU is high, when the container is throttled, or when I want to cut cost, because it shows exactly where cycles go. Wall-clock mode samples all threads at regular real-time intervals, whatever their state, so for a request thread the profile shows where its elapsed time, which is the user's latency, really goes.
I use wall-clock when latency is high but CPU is low.
For example, if an API has a 900 millisecond p99 at 15 percent CPU, a CPU profile just shows ordinary serialisation, while a wall-clock profile of the request threads shows 60 percent of the time in a socket read to a downstream service and 20 percent waiting for a database connection.
The trap is that wall-clock samples idle threads too, so pool threads in queue take or sleep dominate the graph. I split the profile by thread, with async-profiler's -t option, and only look at request threads, or read from the controller frame downward.
About this answer
- Last updated
- 12 November 2026
- 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 "When would you profile a Java service in wall-clock mode rather than CPU mode?": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/ace0244f77/