Java and Spring Level 3 This level · 23 of 56
Hard java spring

CPU is high but request volume is flat. How do you investigate?

The 30 second answer

First I find which threads are burning CPU. I take a few thread dumps a few seconds apart and look for the same stacks running, then record a short Java Flight Recorder profile to see hot methods and GC activity. With flat traffic the usual causes are GC thrashing as the heap fills, a busy loop or retry storm, a regex or serialization hot spot, or a background job.

Could you say this in an interview?

First I find which threads burn CPU. jcmd <pid> Thread.print gives thread dumps; I take a few, a few seconds apart, and look for the same stacks running. Then I record a JFR profile (jcmd <pid> JFR.start duration=60s filename=cpu.jfr) and look at hot methods and GC activity. Usual causes with flat traffic: GC thrashing as the heap fills, a busy loop or retry storm, a regex or serialization hot spot, or a background job. Follow-up: How do you tell GC from app code? (GC events and pause time in JFR, or jcmd <pid> GC.heap_info.) Editor's note: on Linux, top -H -p <pid> shows per-thread CPU; convert the thread ID to hex to match the nid in the dump.

Alternate interview scenarios

"A Java application has 200 threads, but only a few are processing requests. How do you find out what the rest are doing?"

This is the opposite of a CPU problem, so I read thread states rather than hot stacks. I take two or three dumps a few seconds apart and group threads by state and by top frame. WAITING or TIMED_WAITING on a queue take or poll usually means idle pool workers, which is normal. Many threads WAITING on the same lock or condition, or BLOCKED on the same monitor, means contention, and the dump shows which thread holds that lock. RUNNABLE in a socket read with no CPU use means threads stuck on slow downstream I/O, often a missing timeout. Threads parked inside the connection pool's borrow call mean the pool is exhausted. I compare the same thread names across dumps to confirm they really are stuck, and not briefly waiting as part of normal work.

"CPU and latency spike right after every deployment, then settle after a few minutes. Why?"
Usually JIT warm-up. HotSpot starts by interpreting bytecode, then compiles hot methods. With tiered compilation, which Oracle's VM guide says was introduced in Java SE 7 and which current HotSpot builds use by default, the client compiler first produces quick compiled code that also collects profiles, and the server compiler later recompiles the hottest methods with full optimisation. Until that finishes, each request runs slower and the compiler threads themselves use CPU. Class loading and filling caches and connection pools add to it.

  • Confirm it: CPU on the JIT compiler threads in a thread dump or profiler, -XX:+PrintCompilation output during the spike, and latency that improves without any change in traffic.
  • Mitigate: mark the instance ready only after a warm-up, for example a few synthetic requests through the main code paths, and ramp traffic gradually with canary or slow-start load balancing. Class Data Sharing archives cut startup time and class-loading work.
  • Code cache: compiled code lives in the code cache, 240 MB by default with tiered compilation according to the java command reference. If it fills, HotSpot warns that the code cache is full and can stop compiling new methods, so performance drops; size it with -XX:ReservedCodeCacheSize.
  • Benchmarks: the same effect makes naive micro-benchmarks wrong. Use JMH, the OpenJDK benchmark harness, with its warm-up iterations before measurement.
"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 (10)
  1. jcmddocs.oracle.com
  2. JDK Flight Recorder APIdocs.oracle.com
  3. Java 21 Thread.State Javadoc (BLOCKED, WAITING, TIMED_WAITING)docs.oracle.com
  4. jstack (Java 21)docs.oracle.com
  5. Troubleshooting Guide: Troubleshoot Performance Issues Using Flight Recorder (Java 21)docs.oracle.com
  6. async-profiler: lock profilinggithub.com
  7. Java HotSpot VM performance enhancements (Java 21): Tiered compilationdocs.oracle.com
  8. The java command (Java 21): -XX:ReservedCodeCacheSize and compilation optionsdocs.oracle.com
  9. Java 21 VM guide: Class Data Sharingdocs.oracle.com
  10. OpenJDK Code Tools: JMHopenjdk.org
Previous
Next