How to Tame 900% CPU Spikes in MySQL and Java Processes

This guide explains why MySQL and Java processes can exceed 900% CPU usage in production, and provides step‑by‑step methods—using top, show processlist, jstack, index tuning, caching, and code adjustments—to diagnose, mitigate, and prevent such spikes.

Open Source Linux
Open Source Linux
Open Source Linux
How to Tame 900% CPU Spikes in MySQL and Java Processes

Why CPU spikes above 200% happen

In production environments CPU usage can suddenly jump above 200%, especially for MySQL and Java processes.

Scenario 1: MySQL process CPU reaches 900%

Diagnosis steps:

Use top to confirm mysqld is the culprit.

Run show processlist to find resource‑heavy SQL statements.

Check execution plans, missing indexes, or huge data volumes.

Remediation:

Kill offending threads and observe CPU drop.

Add missing indexes, rewrite SQL, or adjust memory parameters.

Introduce caching (e.g., Redis) to offload frequent queries.

Real case: A poorly indexed query caused MySQL CPU to hit 900%+. After adding the proper index and disabling the slow‑log, CPU stabilized around 70‑80%.

Scenario 2: Java process CPU reaches 900%

Diagnosis steps:

Identify high‑CPU Java PID with top.

List threads using top -Hp PID.

Convert thread ID to hex and locate it in a thread dump.

Use jstack to view the stack trace and pinpoint the problematic code.

Common causes & solutions:

Empty loops or spin‑waits – insert Thread.sleep or proper locking.

Excessive object creation leading to frequent GC – reduce allocations or use object pools.

Selector busy‑polling – rebuild selector as in Netty source.

Java CPU 700% optimization case

After locating the hot thread (PID 29706) with top -Hp and jstack, the issue was a busy loop reading from a LinkedBlockingQueue using poll() when the queue was empty.

Fix: replace poll() with take() so the thread blocks instead of spinning.

while (isRunning) {
    try {
        byte[] buffer = dataQueue.take();
        // process buffer
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

After the change, CPU usage dropped below 10%.

Key takeaways

Avoid enabling slow‑log during high‑load periods; it can exacerbate CPU usage. show processlist quickly reveals problematic SQL statements (often missing indexes or large scans).

Use caching layers to reduce direct MySQL traffic.

For Java, identify hot threads with top -Hp and jstack, then eliminate empty loops, reduce object churn, and prefer blocking queue operations.

Memory tuning and proper thread management are essential for stable CPU usage.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaCPU optimizationperformance tuninglinuxmysql
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.