Java core learning path Level 3 This level · 8 of 19
Hard java java-21

How do you troubleshoot high CPU in a Java service?

The 30 second answer

First I confirm it's the Java process and check whether garbage collection is burning the CPU, using the GC logs. If not, I find the hot threads: take a few thread dumps a few seconds apart and see which stacks stay busy. Then I record a short Flight Recorder profile to find the hot methods. Typical causes are busy loops, retry storms, heavy regex or serialization, and excessive logging.

Could you say this in an interview?
  1. Confirm the source. Is the JVM using the CPU, and is it GC? Check GC logs (-Xlog:gc*) or jstat -gcutil <pid> 1s. Constant GC usually means memory pressure, so switch to the GC-pause playbook below.
  2. Find hot threads. On Linux, top -H -p <pid> shows per-thread CPU. Convert the busy thread ID to hex and find it as nid=0x... in a thread dump from jcmd <pid> Thread.print. Take 3 to 5 dumps a few seconds apart; stacks that stay RUNNABLE in the same code are the suspects.
  3. Profile. Record with JDK Flight Recorder, which has low overhead and is designed for production use:
   jcmd <pid> JFR.start duration=60s filename=cpu.jfr

Open the file in JDK Mission Control and look at hot methods and threads.

  1. Fix and verify. Common causes: tight loops, retry storms without backoff, catastrophic regex backtracking, serializing huge objects, logging in hot paths, lock spinning. Measure again after the fix.

Alternate interview scenarios

"How do you diagnose thread contention and lock bottlenecks in production?"

High CPU and lock contention can look similar on a dashboard, so I separate them with dumps and a contention profile. I take several thread dumps with jcmd <pid> Thread.print, or jstack on older setups (the Java 21 jstack man page marks that command experimental and unsupported), and I look for many threads in BLOCKED state waiting on the same monitor, or parked on the same lock. The dump shows which thread owns the monitor. JDK Flight Recorder records jdk.JavaMonitorEnter when a thread is stalled entering a synchronized monitor, and jdk.ThreadPark when a thread is parked, which is how LockSupport and most java.util.concurrent locks wait. Oracle's Java 21 troubleshooting guide lists both as thread-stalling events to inspect for bottlenecks, and notes that important threads spending a lot of time blocked means a critical section has become single-threaded. By default those events are recorded only when they last longer than 20 ms, so if I already suspect short, hot contention I take a short recording with a lower threshold and watch the overhead. async-profiler's lock mode (-e lock) measures time spent entering Java monitors; the top frame is the lock class and the counter is nanoseconds to enter. Then I shrink the critical section, use a finer-grained lock or a concurrent collection, or switch to a ReadWriteLock where reads dominate.

Sources (7)
  1. jcmddocs.oracle.com
  2. Troubleshooting Guide: Diagnostic Tools (JFR)docs.oracle.com
  3. java command: -Xlogdocs.oracle.com
  4. jstatdocs.oracle.com
  5. jstack (Java 21)docs.oracle.com
  6. Troubleshooting Guide: Troubleshoot Performance Issues Using Flight Recorder (Java 21)docs.oracle.com
  7. async-profiler: lock profilinggithub.com
Previous
Next