Diagnosing and Resolving 900% CPU Spikes in MySQL and Java Processes
This guide explains how to identify and fix extreme CPU usage—up to 900%—in MySQL and Java processes, covering diagnostic commands, SQL indexing, caching strategies, thread analysis, and code adjustments to stabilize production systems.
This article describes common scenarios where CPU usage exceeds 200% in production, focusing on MySQL and Java processes that can spike to 900%.
MySQL scenario : High concurrency with poorly indexed queries or enabled slow‑log can drive CPU to 900%. The diagnostic steps include using top to confirm mysqld is the culprit, running SHOW PROCESSLIST to locate heavy sessions, and examining execution plans for missing indexes.
Remediation involves killing offending threads, adding appropriate indexes, limiting connection counts, and disabling slow‑log during high load. A real‑world case shows that adding an index and moving frequent reads to Redis reduced CPU from >900% to 70‑80%.
Java scenario : CPU spikes often stem from empty loops, excessive garbage collection, or selector spin‑loops. The troubleshooting flow uses top to find the Java PID, top -Hp PID to locate hot threads, converting thread IDs to hex, and inspecting stack traces with jstack .
Typical fixes include inserting Thread.sleep or locks to break empty loops, reducing object creation or using object pools to curb GC, and rebuilding Netty selectors to avoid spin‑loops.
Code example of a problematic loop:
while (isRunning) {
if (dataQueue.isEmpty()) {
continue;
}
byte[] buffer = device.getMinicap().dataQueue.poll();
// processing logic
}Replacing the non‑blocking poll() with the blocking take() prevents the empty‑loop CPU burn:
while (isRunning) {
try {
byte[] buffer = device.getMinicap().dataQueue.take();
// processing logic
} catch (InterruptedException e) {
e.printStackTrace();
}
}After applying these changes, the Java process CPU dropped below 10% and remained stable.
The article also includes practical tips such as avoiding enabling slow‑log during high CPU, leveraging Redis caching, and tuning memory parameters.
Code Ape Tech Column
Former Ant Group P8 engineer, pure technologist, sharing full‑stack Java, job interview and career advice through a column. Site: java-family.cn
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.