Troubleshooting guide
Java high CPU: how to find the thread with a thread dump
To find CPU trouble on the JVM, match the operating system's busiest thread to its Java stack. It takes two commands and one number conversion.
3 min read10 sectionsUpdated
On this page
# Busiest threads in the JVM
top -H -b -n1 -p PID | head -20
# Convert the thread ID to hex (e.g. 12345 -> 0x3039)
printf '0x%x\n' 12345
# Stack for that thread
jstack PID > /tmp/dump1.txt
grep -A 25 'nid=0x3039' /tmp/dump1.txt
# Sampling profile with low overhead (async-profiler)
asprof -d 30 -f /tmp/cpu.html PIDFixes
The stack names the method. For tight loops, add proper waiting, or a condition. For regexes, simplify, or add input limits. For GC, follow the memory path: see GC overhead limit exceeded. Replace unsafe HashMap use across threads with ConcurrentHashMap. Reduce per-request logging or serialisation hot spots shown by the profile.
Why several dumps matter
One thread dump is a single moment, and the stack you see could be just passing through. Take three dumps five seconds apart. A thread that is RUNNABLE in the same method each time is the one to investigate. Remember RUNNABLE includes threads blocked in native I/O, such as socketRead0, which use no CPU. For anything more detailed, use a flame graph: async-profiler samples with low overhead and does not suffer from the safepoint bias that makes some Java profilers misleading.
Catch it automatically
Continuous JVM monitoring charts process and GC CPU separately. Link spikes to the responsible endpoint with APM, and compare against a deployment marker.
Does jstack pause the JVM?
Briefly, at a safepoint. It is generally safe in production.
What is the difference between nid and tid?
nid is the native OS thread ID, and tid is an internal JVM address.
Why are threads RUNNABLE but CPU is low?
They are probably waiting in native network I/O.
Related reading
- PostgreSQL high CPU usage: how to find the cause: Diagnose PostgreSQL high CPU: use pg_stat_statements to rank queries by total time, spot missing indexes and stale statistics, and read EXPLAIN properly.
- High iowait on Linux: how to find the cause: Diagnose high iowait on Linux with iostat and iotop: read await and %util correctly, find the process doing the I/O, and fix slow queries and swap.
- Prometheus high cardinality: how to find and fix it: Find and fix Prometheus high cardinality: use the TSDB status page and promtool, identify runaway labels and drop them with relabeling rules before ingestion.
- MongoDB slow queries: how to find and fix them: Find and fix slow MongoDB queries: use the profiler and explain, spot COLLSCAN and in-memory sorts, and follow the ESR rule when designing compound indexes.
- What is a flame graph?: Flame graphs explained: how stack samples become a flame graph, how to read width and depth, and the tools to produce one in each language.
- How to monitor DigitalOcean: How to monitor apps on DigitalOcean: droplet CPU, memory and disk, App Platform services, managed databases, load balancers, uptime checks and alerts.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.