Diagnosing CPU Spikes in Java Applications
Fast recall
Quick revision
Core answer
Your ability to distinguish between expected CPU spikes (GC, JIT compilation) and pathological ones (hot loops, lock contention), and to use the right tools to diagnose each.
Restarting the application instead of capturing a thread dump or flamegraph while the spike is happening, losing all diagnostic information.
Mention capturing thread dumps during the spike, correlating with GC logs, and using async-profiler wall-clock mode to catch I/O-induced CPU spikes from busy-wait patterns.
Interview-ready guide
Interview answer
When CPU spikes in a Java application, the most important thing is to capture data before restarting — a restart clears the evidence and you're left guessing.
Capture a flamegraph during the spike
./profiler.sh -d 30 -f spike.html PIDcaptures a 30-second CPU flamegraph while the spike is happening — the widest boxes at the top of the flamegraph are the methods consuming the most CPU during the spike- async-profiler doesn't require the application to be stopped or restarted; it's safe to run in production briefly
Common causes visible in flamegraphs
- GC pressure: the flamegraph shows the GC threads consuming a large fraction of CPU — confirm with
jstat -gcutil PID 1000which shows GC pauses and heap usage in real time; if Old Gen usage is climbing toward 100%, a full GC is imminent - Hot locks: many threads piling up on a
synchronizedblock orReentrantLock; the flamegraph shows all threads in the same lock-acquire call path; ajstack PIDat the same time shows threads in BLOCKED state - Compilation: the JVM's JIT compiler compiles hot methods; a cold-start CPU spike often shows
InterpreterandCompilerThreadframes; this is expected and subsides as the JVM warms up - Regex compilation:
Pattern.compile()in a hot path is CPU-intensive; compile patterns at class initialization and store as static final fields
GC-related CPU spikes
- Young GC pauses are frequent but short (< 50ms); high young GC frequency means the application is allocating objects faster than the young gen can be collected; increase Eden size or reduce allocations
- Old Gen full GC is the severe case —
jstat -gcutil PIDshowsOcolumn approaching 100% before the spike; the full GC pause causes a CPU spike as all application threads stop; look for memory leaks or over-caching
Diagnosing CPU Spikes in Java Applications
Direct Answer
Capture a flamegraph and thread dump during the spike; common causes are GC pressure, hot loops, lock contention, or JIT compilation bursts — each has a distinct signature.
Common Mistakes & Pitfalls
Restarting the application instead of capturing a thread dump or flamegraph while the spike is happening, losing all diagnostic information.
Check your understanding
Answer these without looking back at the article.
- How do you distinguish a GC-induced CPU spike from an application CPU spike?
- What is ReDoS and how do you prevent it in Java?
- How do you capture a thread dump in a Kubernetes pod?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.