Skip to content

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

Key points

  • top -H gives thread IDs.
  • nid in jstack is the same ID, in hex.
  • Take several dumps, seconds apart.
  • High CPU in GC threads is a memory problem.

How to approach it

CPU on a JVM is either your code, or JVM work like garbage collection and JIT compilation. Thread names separate those at once: G1 Conc or GC Thread means memory pressure, and http-nio means request handling.

Common causes

  • Tight loops, or a busy-wait.
  • Regular expression backtracking.
  • GC running constantly because the heap is nearly full.
  • Heavy serialisation or logging.
  • Concurrent HashMap misuse.

Diagnose it

Take the busiest thread's ID, convert it to hex, and find it in the dump.

bash
# 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 PID

Fixes

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.

Frequently asked questions

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.

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.