Skip to main content
InterviewHubPRO
Section Overview
Q27

Question 27 of 50

Part 3: JVM Internals, Low-Latency Systems & Production Tuning (25 - 35)

54%
Java Core & JVM (50 Qs)•Part 3: JVM Internals, Low-Latency Systems & Production Tuning (25 - 35)

What causes 100% CPU spikes in Java applications and how do you diagnose them using Thread Dumps?

senior LevelCode ExamplejavaJVM Internals

In Simple Words: 100% CPU spikes in Java are typically caused by:

  1. GC Thrashing: When the heap is 99% full, GC threads run continuously in an infinite loop trying to free bytes before crashing with OutOfMemoryError: GC overhead limit exceeded.
  2. Infinite Loops or Busy-Waiting: Threads spinning in lock-free CAS loops or bad while loops without yielding.
  3. High Lock Contention: Thousands of threads battling for synchronized locks causing massive kernel context switching.

Fast Production Diagnosis via Linux:

bash
# 1. Identify the high-CPU OS thread ID:
top -H -p <pid> # Find thread ID (e.g., 4210)

# 2. Convert Thread ID to Hexadecimal:
printf '%x
' 4210 # Output: 1072

# 3. Capture thread dump and grep for the nid (native thread id):
jcmd <pid> Thread.dump | grep -A 30 "nid=0x1072"

This pinpointed stack trace reveals the exact line of code consuming the CPU cores.