Scrivio Interview library
ConceptIntermediate7 min read · updated 12 November 2026

How does async-profiler differ from a sampling profiler that uses JVMTI GetAllStackTraces, and which profiling modes does it offer?

A traditional sampler asks the JVM for all stack traces through JVMTI, which can only answer at a safepoint, so its samples cluster at safepoint polls instead of where the time really goes. async-profiler interrupts threads with a signal driven by perf_events or a timer and walks the stack right there, using AsyncGetCallTrace (or its own stack walker in recent versions), so it has no safepoint bias and shows Java, native and kernel frames in one stack. It offers cpu, wall-clock, alloc and lock modes, and writes flame graphs or JFR files.

Why this question is asked

When a profiler points at the wrong method, we waste days optimising code that was never slow. Safepoint bias is the classic way this happens with older Java profilers, and it is why async-profiler became the standard tool for production profiling. Interviewers ask this question to see whether we understand how a profiler collects its data, because that tells us when to trust it. In this answer, we will recap what a safepoint is, see why a JVMTI-based sampler is biased, follow how async-profiler takes a sample instead, and then go through its modes, its output formats and what changes inside containers.

1A Quick Recap of Safepoints

A safepoint is a moment when a Java thread is at a known, well-described position in its code, where the JVM knows exactly which stack slots hold object references. The JVM needs all threads at safepoints for some operations, such as certain GC phases or deoptimisation, because only then can it safely inspect and change thread state.

Compiled code does not reach a safepoint at every instruction. The JIT compiler inserts safepoint polls, small checks that ask whether the JVM wants the thread to stop, at specific places: typically at method returns and at the back edge of loops that are not known to be short. Between polls, a thread runs freely and cannot be stopped for a safepoint operation.

2Why a JVMTI Sampler Is Biased

JVMTI is the JVM's native tool interface. A classic sampling profiler calls its GetAllStackTraces function, or Thread.getAllStackTraces() from Java, every few milliseconds. To answer, the JVM brings the threads to a safepoint and reads their stacks. That has two consequences.

The first is bias. A thread is not sampled where it was when the profiler asked, but where it reached the next safepoint poll. If a method spends most of its time in a tight counted loop without polls, followed by a call that has a poll, the samples land on that later location. So the profile shows hot spots at safepoint locations, not where CPU is really spent. A well-known consequence is a profile blaming a cheap method that happens to sit right after an expensive loop.

The second is cost and scope. Bringing hundreds of threads to a safepoint many times per second pauses the whole application, and the stacks it returns contain only Java frames. A thread inside native code, a system call or the GC shows up as being in some Java method that called into native code, or not at all, so time spent in the kernel or in native libraries is invisible.

Step throughA JVMTI sampler waits for a safepoint
ProfilerJVMApp thread
GetAllStackTraces
please stop at the next safepoint
keeps running the hot loop, no poll here
reaches a poll in the next method
stack at the poll, not in the loop

5 messages in order. Play them, or step through them one at a time.

The sample is taken where the thread could stop, which is not where the time was spent.

3How async-profiler Takes a Sample

async-profiler avoids the safepoint entirely. It arranges for the operating system to deliver a signal to a thread at the moment we want a sample. In CPU mode, it uses Linux perf_events, a kernel facility for counting events such as CPU cycles, configured so that a thread receives a signal after it has consumed, for example, 10 ms of CPU. Where perf_events is not available, it can use timer-based engines instead.

When the signal arrives, the thread is interrupted at whatever instruction it was running, and async-profiler's signal handler runs on that thread. The handler walks the Java part of the stack with AsyncGetCallTrace, a HotSpot function designed to walk a thread's stack at an arbitrary point without a safepoint. Recent versions of async-profiler can also use their own stack walker that reads JVM structures directly; check your version's documentation for the options.

Because the stack is taken at the interrupted instruction, there is no safepoint bias, and no other threads are paused. The handler also collects native frames, and in CPU mode with perf_events it receives the kernel part of the stack. So one sample can show a Java method calling a JNI library calling a system call, all in one stack.

Step throughasync-profiler interrupts the thread where it is
Kernel (perf_events)App threadasync-profiler
signal: 10 ms of CPU used
signal handler runs on this thread
walk Java frames, no safepoint
add native and kernel frames
sample stored, thread continues

5 messages in order. Play them, or step through them one at a time.

4The Profiling Modes

async-profiler offers several modes, chosen with the -e option. Each answers a different question.

Text
asprof -e cpu -d 30 -f cpu.html <pid> # where CPU cycles go
asprof -e wall -t -d 30 -f wall.html <pid> # where elapsed time goes, all thread states
asprof -e alloc -d 30 -f alloc.html <pid> # which code allocates the most bytes
asprof -e lock -d 30 -f lock.html <pid> # where threads wait for contended locks
asprof -e cpu -d 30 -o jfr -f cpu.jfr <pid> # same data, written as a JFR file
async-profiler modes
ModeSamplesUse it for
cputhreads running on a CPUhigh CPU, throttling, cost
wallall threads, every statelatency with low CPU, waiting
allocallocation buffer eventsGC pressure, garbage sources
lockcontended lock acquisitionsthreads blocked on monitors or locks

CPU mode samples threads that are running and is the default. Wall-clock mode samples all threads, whatever their state, so it is the one for slow requests with low CPU. Allocation mode records sampled allocations, using JVM hooks that fire when a thread needs a new allocation buffer, so it shows which code creates garbage without instrumenting every new. Lock mode records contended monitor and lock acquisitions with the time spent waiting. It can also count hardware events such as cache misses, where the kernel allows it.

For output, the HTML flame graph is the usual choice, but it also writes collapsed stacks for other tools, call trees, and JFR files that open in JDK Mission Control alongside other JFR data.

5What Changes in Containers

perf_events needs permission from the kernel. In containers, the default seccomp profile of Docker blocks the perf_event_open system call, and the host's kernel.perf_event_paranoid setting may also forbid it. In that situation CPU mode with perf_events fails or cannot see kernel frames.

The answer is the timer-based engine. async-profiler can use itimer or ctimer, which rely on ordinary POSIX timers rather than perf_events, so they work without extra privileges. We lose kernel stacks and hardware counters, but Java and native frames are still sampled without safepoint bias, which is what matters most. Recent versions fall back to a timer-based engine automatically when perf_events is not available; check your version. Wall, alloc and lock modes do not depend on perf_events at all. The alternative is to grant the container the needed capability or adjust the seccomp profile, which is a security decision for the platform team, not something to change casually.

6Follow-up questions

Does JFR have safepoint bias?

JFR's method sampler does not wait for a global safepoint; it suspends threads and walks their stacks, so it avoids the classic bias, though it records only Java frames for running Java code and samples native code separately. Recent JDKs also add a CPU-time based sampler on Linux; check your version.

Why is AsyncGetCallTrace sometimes unable to walk a stack?

Because the thread may be interrupted at an awkward instruction, for example in the middle of building a frame or inside VM internal code, where the stack cannot be walked reliably. AsyncGetCallTrace then returns an error code, and async-profiler records the sample with a marker frame instead of a real stack. A small percentage of such samples is normal.

Is it safe to attach async-profiler to production?

It is widely used in production because the overhead at the default interval is low, typically a few percent at most in CPU mode, and it can be attached and detached at runtime. We still measure it on our own services first, and we control who may attach it, since profiling is a privileged action.

7Conclusion

In this answer, we saw that a JVMTI sampler using GetAllStackTraces can only sample threads at safepoints, so its results cluster at safepoint polls and it sees only Java frames. async-profiler uses signals from perf_events or timers to interrupt threads wherever they are and walks the stack with AsyncGetCallTrace or its own walker, so it has no safepoint bias and shows Java, native and kernel frames together. It offers cpu, wall, alloc and lock modes, writes flame graphs and JFR files, and in restricted containers uses a timer-based engine instead of perf_events.

What to say in the interviewAbout 80 seconds aloud

Open with

The key difference is safepoint bias. A classic sampler calls JVMTI GetAllStackTraces, and the JVM can only answer at a safepoint, so each thread is sampled where it reaches the next safepoint poll, typically a method return or a loop back edge, not where it was actually spending time.

Then, if they want more

That clusters samples in the wrong places, pauses all threads for every sample, and only shows Java frames. async-profiler instead asks the kernel to deliver a signal to a thread, in CPU mode through perf_events after the thread used a slice of CPU, or through a timer.

The signal handler runs on the interrupted thread and walks the stack right there with AsyncGetCallTrace, or in newer versions its own walker, so there is no safepoint bias and no global pause. It also adds native frames and, with perf_events, kernel frames, so I can see a JNI call or a system call in the same stack.

The modes are cpu, wall for all thread states, alloc for allocation hot spots, and lock for contended locks, and it outputs flame graphs, collapsed stacks or JFR files. In containers perf_events is often blocked by seccomp or perf_event_paranoid, so I use the itimer or ctimer engine, which loses kernel frames but keeps unbiased Java samples.

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 "How does async-profiler differ from a sampling profiler that uses JVMTI...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/90020bb952/