How to Diagnose and Fix High CPU Usage in Java/Tomcat Applications
This guide walks through identifying a Java process that spikes CPU usage, locating the offending thread with Linux tools, extracting its stack trace, pinpointing the problematic code in a socket utility class, and outlining the steps to resolve the infinite‑loop issue.
Problem Overview
When an application shows unusually high CPU usage, the most common cause—aside from genuine compute‑intensive work—is an infinite loop.
Real‑world Example
In a recent incident a Java process (PID 28555) consumed 200% CPU, causing a service outage.
Using top we identified the process, and ps aux | grep 28555 confirmed it was a Tomcat instance.
Thread‑level Diagnosis
List all threads of the process: ps -mp 28555 -o THREAD,tid,time The thread with the highest CPU time was tid 28802 , running for nearly two hours.
Convert the thread ID to hexadecimal (required by jstack): printf "%x\n" 28802 Dump the stack of that thread: jstack 28555 | grep 0x7042 -A 30 The stack trace revealed the culprit:
ShortSocketIO.readBytes(ShortSocketIO.java:106)
Relevant Code
public byte[] readBytes(int length) throws IOException {
if (this.socket == null || !this.socket.isConnected) {
throw new IOException("++++ attempting to read from closed socket");
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (this.recIndex >= length) {
bos.write(this.recBuf, 0, length);
byte[] newBuf = new byte[this.recBufSize];
if (this.recIndex > length) {
System.arraycopy(this.recBuf, length, newBuf, 0, this.recIndex - length);
}
this.recBuf = newBuf;
this.recIndex -= length;
} else {
int totalread = length;
if (this.recIndex > 0) {
totalread -= this.recIndex;
bos.write(this.recBuf, 0, this.recIndex);
this.recBuf = new byte[this.recBufSize];
this.recIndex = 0;
}
int readCount = 0;
while (totalread > 0) {
if ((readCount = this.in.read(this.recBuf)) > 0) {
if (totalread > readCount) {
bos.write(this.recBuf, 0, readCount);
this.recBuf = new byte[this.recBufSize];
this.recIndex = 0;
} else {
bos.write(this.recBuf, 0, totalread);
byte[] newBuf = new byte[this.recBufSize];
System.arraycopy(this.recBuf, totalread, newBuf, 0, readCount - totalread);
this.recBuf = newBuf;
this.recIndex = (readCount - totalread);
}
totalread -= readCount;
}
}
}
return bos.toByteArray();
}The bug lies in the loop condition: if this.in.read() returns a value ≤ 0 (which can happen during network congestion), the while (totalread > 0) loop never terminates, causing the thread to consume CPU indefinitely.
Fixing the Issue
The corrective action depends on business logic; generally you should break the loop or handle the zero/negative read result appropriately.
Key Tools for CPU Troubleshooting
top : Real‑time CPU usage monitoring.
ps : Inspect process and thread CPU consumption.
jstack : Dump Java thread stacks to locate problematic code.
pstack : Linux utility for native thread stack traces.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
