How to Quickly Identify and Fix High CPU Usage in Java Services
This guide walks you through a step‑by‑step process using Linux top, jstack, and hex conversion to pinpoint the exact Java thread causing CPU spikes, enabling effective service optimization and cost reduction without unnecessary scaling.
Introduction
When a server runs many Java micro‑services or multiple FastCGI ports, a single method can cause a CPU spike and trigger alerts. Instead of blindly adding more machines, you can locate the root cause by following a few systematic steps.
Step 1 – Identify the most CPU‑intensive process
Tool: top
Command: top -c (press P to sort by CPU usage).
The process with the highest CPU usage appears at the top; note its PID (e.g., 18154).
Step 2 – Find the most CPU‑intensive thread
Tool: top
Command: top -Hp 18154 (press P to sort threads).
The thread PID with the highest CPU usage is recorded (e.g., 15259).
Step 3 – Convert the thread PID to hexadecimal
Use printf or a calculator to convert decimal 15259 to 0x3b9b , because thread IDs in stack traces are shown in hex.
Step 4 – Inspect the stack of the offending thread
Tools: jstack , grep (or pstack ).
Command: jstack -F 15259 | grep '0x3b9b'
This prints the stack trace and reveals the method (e.g., main ) that is consuming CPU.
Step 5 – Save the full stack trace for deeper analysis
Command: jstack -l 15259 > 15259.stack
Search the saved file for 0x3b9b to locate the exact code location.
Conclusion
Systematic CPU‑troubleshooting can dramatically improve service stability and reduce operational costs. Mastering these Linux and Java tools is an essential skill for any “craftsman engineer”.
360 Zhihui Cloud Developer
360 Zhihui Cloud is an enterprise open service platform that aims to "aggregate data value and empower an intelligent future," leveraging 360's extensive product and technology resources to deliver platform services to customers.
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.