Async-Profiler: A Practical JVM Profiling Guide

Choose CPU, allocation, lock, or live-object profiling, then compare JVM profiles under a repeatable workload without guessing at overhead.

published: reading time: 6 min read author: Geek Workbench updated: November 5, 2026
Quick Summary

Choose CPU, allocation, lock, or live-object profiling, then compare JVM profiles under a repeatable workload without guessing at overhead.

Async-Profiler: A Practical JVM Profiling Guide

This guide is for Java developers who can find a running JVM process and run the same workload twice. It uses async-profiler’s command-line tool, asprof, to investigate CPU use, allocations, lock contention, and sampled objects that remain live.

A profile points to code paths that need investigation. To tell whether a code change helped the service, compare it under the same build conditions and workload before and after.

Choose a mode from the symptom

What you observe Mode What the profile can show
A service spends too much time on CPU cpu Frequently sampled Java, native, JVM, and sometimes kernel call paths
Allocation rate or GC pressure is high alloc Sampled call sites that create heap objects and the associated allocation pressure
Threads wait to enter Java monitors lock Contended Java locks and the paths that reach them
Heap usage grows and you want allocation sites for objects still around alloc with --live Allocation samples whose objects remain live at the end of the capture

If the symptom is unclear, start with CPU for a CPU-bound service, or with allocation when GC activity or allocation rate is the concern. Capture lock data when threads are waiting on Java monitors. A live allocation profile can help investigate heap growth, but it is not a heap dump or a complete object-retention graph.

Check the version and target process

The commands below were checked against async-profiler 4.6 documentation on 2026-11-05. Version 4.6 was released on 2026-10-19. The project lists binaries for Linux x64, Linux arm64, and macOS arm64/x64 in its download section. Use the archive for your operating system and architecture, and verify available events against the process you plan to profile:

PID=12345 # Replace this with the target JVM's process ID
asprof list "$PID"

These examples assume a HotSpot-based JVM. Record the profiler release, the target JVM version, the operating system, and the architecture with every capture. The installed JDK, JVM vendor, permissions, container namespace, and operating system can affect which events are available and whether attachment works. Check the local release notes and profiler options before using commands with an older version.

Capture a first profile

Use the target Java process ID for PID. Keep the profiler and target process in a context where that PID is visible and attachment is permitted.

For a CPU profile:

asprof -e cpu -d 30 -f cpu.html "$PID"

Open cpu.html in a browser. In a flame graph, a wider frame accounts for more of the recorded sample weight. The horizontal axis arranges stacks; it is not a timeline. Start at the bottom of a stack and follow it upward to see the calls that led to a sampled method.

For other symptoms, change the event and output name:

# Heap allocation sites
asprof -e alloc -d 60 -f allocations.html "$PID"

# Contended Java monitors; record the thread dimension
asprof -e lock --lock 5ms -t -d 60 -f locks.html "$PID"

# Allocation samples whose objects remain live at the end of the capture
asprof -e alloc --live -d 60 -f live-allocations.html "$PID"

Allocation mode samples where heap objects are created; its result is not a count of every allocation. The --live option keeps allocation samples for objects that have not been collected by the end of the session. It can point to allocation sites worth investigating, but it does not show every retained object and cannot establish a leak by itself. Confirm suspected growth with heap measurements over time or a heap-analysis tool.

The commands use the -e, -d, and -f options documented for asprof. Lock profiling can use --lock to set a contention threshold. Check the current profiling modes guide and options reference for details and platform-specific constraints.

Compare before and after under the same load

The following is a measurement procedure, not a benchmark result. No overhead percentage is reported here. Replace the example URL with a representative route in a test or staging service, and use the same endpoint and request data for both builds. Install wrk and async-profiler 4.6 for the host before running it.

First record the environment while the service is running:

java -version 2>&1
jcmd "$PID" VM.version
uname -srmo

Also record the async-profiler release archive, application build identifier, JVM flags, heap settings, and wrk --version. The process version from jcmd is more useful than the shell’s java -version if the service was started with a different JDK.

In one terminal, run a fixed workload against the baseline build:

wrk -t4 -c32 -d5m --latency http://127.0.0.1:8080/api/report

While that load is running, capture a 60-second CPU profile from another terminal:

asprof -e cpu -i 10ms -d 60 -f baseline.html "$PID"

Repeat with the candidate build, the same runtime and settings, and the same workload. Save the capture as candidate.html. Warm each build the same way before measuring. Run several repetitions and compare the same wrk measurements, such as requests per second and p95 latency, alongside the profiles. Treat a flame graph difference as a lead to investigate; it is not a substitute for a service-level measurement.

To measure profiler overhead, keep one build and repeat the same workload with profiling disabled and enabled. Compare the load-test results across repeated runs. Report the measured difference with the exact JDK, operating system, architecture, workload, event, interval, capture length, and measurement method. Overhead varies with those conditions, so a percentage from one setup should not be presented as a general property of async-profiler.

Read the profile, then test one change

For a CPU profile, follow wide stacks to the code that owns the work. A broad framework frame may be the caller of a costly operation, so inspect the frames beneath it before changing code. If compilation or garbage-collection threads dominate, first check whether the capture included warm-up or an unusual GC event.

For allocation profiles, find the paths that create many or large objects. High allocation volume and retained heap are different problems: short-lived objects can create GC work without causing steady heap growth. For lock profiles, look at the contended lock and the waiting call paths before replacing a synchronized block or collection.

Make one change, then repeat the same workload and capture settings. If request latency or throughput improves but the profile moves somewhere else, the next bottleneck may only have become visible after the first one was reduced.

Further reading

Category

Related Posts

CMS and G1 Collectors: Low-Latency Garbage Collection

Understand G1 for current HotSpot deployments and CMS as a historical collector removed in JDK 14, with version-labeled examples and GC logging.

#jvm #garbage-collection #cms

Deoptimization Debugging: When JIT Compiled Code Reverts

Learn what causes the JVM to deoptimize JIT-compiled code, how to detect deoptimization events, and how to fix the underlying issues.

#jvm #jit #deoptimization

Execution Engine: Interpreter, JIT Compiler, and Garbage Collector

Deep dive into the JVM Execution Engine covering bytecode interpretation, JIT compilation, and Garbage Collector architecture and algorithms.

#jvm #jit #gc