Scrivio Interview library
DebuggingFoundations7 min read · updated 12 November 2026

A JVM process on Linux is using 100 percent CPU. How do you find which Java thread and which code is responsible?

We run top -H -p <pid> to see CPU per thread and note the busiest thread ids, convert each id to hexadecimal, and find the thread with the matching nid in a thread dump from jcmd <pid> Thread.print. Taking several dumps a few seconds apart shows whether the thread stays in the same code. If the busy threads turn out to be GC or JIT compiler threads, the cause is memory pressure or warmup rather than our code, and an async-profiler CPU profile gives a fuller picture than individual dumps.

Why this question is asked

A JVM pinned at 100 percent CPU is one of the most common production alerts, and the first instinct, restarting the service, throws away the chance to find out why. With two standard Linux and JDK commands we can usually identify the guilty thread and the exact line of code in a few minutes. Interviewers ask this question to see whether we know that practical routine and whether we can tell application problems apart from JVM ones. In this answer, we will see how Linux shows per-thread CPU, how to connect a Linux thread to a Java thread, why one dump is not enough, how to recognise GC and JIT threads, and when to reach for a profiler instead.

1A Quick Recap: Java Threads Are Linux Threads

In HotSpot on Linux, every Java platform thread is backed by one operating system thread. The kernel schedules them and counts their CPU time individually, and each has a thread id, often called a TID or LWP, light-weight process id. The JVM also runs its own threads in the same process: garbage collector worker threads, JIT compiler threads that turn bytecode into machine code, and a few others.

So the question which thread uses the CPU can be answered by the operating system, and the question what code that thread is running can be answered by the JVM. The trick is connecting the two, and the thread id is the link.

2Find the Busy Threads With top -H

Normal top shows processes. With -H it shows threads, and with -p it limits the view to one process.

Properties
top -H -p 4121
# PID USER %CPU COMMAND
# 12345 app 98.7 http-nio-8080-e <- one thread using a whole core
# 12346 app 97.9 http-nio-8080-e
# 12301 app 1.2 C2 CompilerThre
# 12290 app 0.3 GC Thread#0

In thread mode, the PID column holds the thread id. Here two request threads each use almost a full core. Linux truncates thread names to 15 characters, so http-nio-8080-exec-7 appears as http-nio-8080-e, which is why we still need the JVM to tell us exactly which thread it is. In a container where top is missing, ps -L -o tid,pcpu,comm -p <pid> gives the same information.

3Match the Thread in a Thread Dump

A thread dump lists every Java thread with its name, state and current stack trace. Each entry also has an nid, the native thread id, written in hexadecimal. So we convert our decimal thread id to hex and search for it.

YAML
printf '%x\n' 12345 # 3039
jcmd 4121 Thread.print > dump1.txt
grep -A 12 'nid=0x3039' dump1.txt
# "http-nio-8080-exec-7" #41 daemon prio=5 os_prio=0 tid=0x00007f... nid=0x3039 runnable
# java.lang.Thread.State: RUNNABLE
# at com.shop.pricing.DiscountRules.applyAll(DiscountRules.java:214)
# at com.shop.pricing.DiscountRules.applyAll(DiscountRules.java:220)
# at com.shop.pricing.PriceService.price(PriceService.java:87)

Now we know the thread is http-nio-8080-exec-7 and it is running DiscountRules.applyAll. jcmd is the recommended tool for this in current JDKs; jstack prints the same dump. Notice that the dump itself pauses the JVM briefly at a safepoint, which is fine even in production, but we keep it in mind if we take many.

4Take Several Dumps to See a Pattern

One dump is a single moment. A thread that happens to be in some method at that instant might have been somewhere else a millisecond earlier. So we take three to five dumps a few seconds apart and compare the busy thread's stack in each.

Java
for i in 1 2 3 4 5; do jcmd 4121 Thread.print > dump$i.txt; sleep 3; done
grep -A 3 'nid=0x3039' dump*.txt | grep ' at ' | sort | uniq -c
# 5 at com.shop.pricing.DiscountRules.applyAll(DiscountRules.java:214) <- every dump
Step throughFrom 100 percent CPU to the guilty code
  1. top -H -p <pid>busiest thread ids
  2. Are the busy threads GC or compiler threads?
    YesLook at memory pressure or warmupNoContinue with that thread id
  3. Convert the id to hex12345 -> 0x3039
  4. jcmd <pid> Thread.print, 3 to 5 timesfind nid=0x3039
  5. Same stack in every dump?
    YesHot spot found: fix that codeNoProfile with async-profiler in CPU mode

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

If the thread is in the same method, or the same loop, in every dump, we have found a hot spot, often an infinite or very long loop, a pathological regular expression, or an algorithm that is quadratic on large input. Here applyAll calls itself repeatedly, which suggests runaway recursion over a rule set, perhaps a cycle in the discount rules. If the stack is different in every dump, the thread is busy but not stuck in one place, and a profiler will describe it better than dumps.

5When the Busy Threads Belong to the JVM

Sometimes top -H shows that the busiest threads are not ours. Threads named GC Thread#0, G1 Conc#0 or similar are garbage collector threads; C2 CompilerThread0 and C1 CompilerThread0 are JIT compiler threads. Each points to a different story.

Busy GC threads mean the collector is working hard, usually because the heap is nearly full of live data, so collections run back to back and free little. The fix is in memory, a leak or a heap that is too small, and the evidence is in GC logs, not thread dumps. Busy compiler threads are normal for the first minutes after startup, when the JIT compiles the hot methods; if they stay busy, the code cache might be full or code is being repeatedly deoptimised and recompiled. In both cases, digging into application stacks would be a waste of time.

6When to Use a Profiler Instead

Thread dumps are a quick tool that needs nothing installed. But they show a few moments, only Java frames, and they cannot tell us how CPU is spread across many methods. When the CPU use is spread out, or we want numbers, a CPU profile is better. async-profiler samples stacks many times per second without safepoint bias and draws a flame graph showing which code uses what share of CPU, including native and JVM frames.

Text
asprof -e cpu -d 30 -f /tmp/cpu.html 4121 # 30 seconds, flame graph in cpu.html

In practice, we often start with top and a few dumps because they are always available and answer the stuck-in-a-loop case in minutes, and we move to a profile when the answer is not obvious.

7Follow-up questions

Why does the thread dump show the thread as RUNNABLE when it might be waiting on a socket?

RUNNABLE is the Java state, and a thread reading from a socket in native code is also reported as RUNNABLE even though it uses no CPU. That is why we pick threads by their CPU in top first, and only then read their stacks; a RUNNABLE state alone does not prove a thread is burning CPU.

What about virtual threads?

Virtual threads run on a small pool of carrier threads, so top shows CPU on the carriers, named ForkJoinPool workers, not on individual virtual threads. jcmd Thread.print does not list virtual threads; jcmd Thread.dump_to_file can write a dump that includes them. A CPU profiler is usually the better tool when virtual threads are in use.

8Conclusion

In this answer, we found the source of 100 percent CPU in a JVM. top -H -p shows CPU per Linux thread, and since each Java platform thread is a Linux thread, converting the busy thread's id to hexadecimal lets us find it by its nid in a jcmd Thread.print dump. Several dumps a few seconds apart show whether it is stuck in the same code. If the busy threads are GC or JIT compiler threads, we look at memory pressure or warmup instead, and for spread-out CPU use an async-profiler CPU profile gives the full picture.

What to say in the interviewAbout 80 seconds aloud

Open with

On Linux each Java platform thread is a native thread, so I start with top -H -p and the process id, which shows CPU per thread, and note the ids of the busiest ones, say 12345 at 98 percent.

Then, if they want more

Then I convert that id to hex, 12345 is 0x3039, take a thread dump with jcmd pid Thread.print, and search for nid=0x3039. That gives me the Java thread name, like http-nio-8080-exec-7, and its stack, for example DiscountRules.applyAll.

One dump is a single instant, so I take three to five a few seconds apart; if the thread is in the same method every time, I have found a hot loop, runaway recursion or a pathological regex.

Before going into application code I check the names of the busy threads: if they are GC threads, the problem is memory pressure, and I go to GC logs and heap usage; if they are C2 or C1 compiler threads, it is JIT warmup, or something like a full code cache if it lasts.

And if the CPU is spread across many methods, thread dumps are too coarse, so I take a 30 second async-profiler CPU profile and read the flame graph, which also shows native and JVM frames.

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 "A JVM process on Linux is using 100 percent CPU. How do you find which Java thread and...": https://scrivio-interview-library.netlify.app/java/jvm-diagnostics/0abc2930e0/