How I Diagnosed and Fixed a Java Process’s CPU Spike with jstack

This article walks through a real‑world Java CPU‑usage issue, showing how top, thread ID conversion, and jstack were used to pinpoint a faulty loop in LoguUtil, then verifies the fix restores normal CPU consumption while emphasizing the importance of performance testing.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
How I Diagnosed and Fixed a Java Process’s CPU Spike with jstack

1. Background

During development we logged into the OSS pre‑release console and noticed that a Java process was consuming an unusually high amount of CPU, which was unexpected for a machine used only for internal development and product testing.

2. Investigation

First, we ran top -Hp 15057 to view thread‑level CPU usage. The screenshot shows that thread 15393 was responsible for most of the CPU time, so we focused on that thread.

We converted the thread ID to hexadecimal with printf "%x\n" 15393, obtaining 0x3c21.

Next, we executed sudo -u www jstack 15057 to dump the stack traces of all threads in the process. By matching the hexadecimal ID 0x3c21, we identified the stack belonging to the CPU‑intensive thread.

The stack trace pointed to line 255 in LoguUtil. That line contains a loop that calls poll() on a blocking queue. Because poll() returns null when the queue is empty instead of blocking, the loop became a tight, non‑terminating loop.

The issue was not caught during testing because logs usually prevent execution from reaching line 255; we discovered it only when logging in late at night when no logs were generated.

3. Verification

After fixing the code and redeploying, we checked the CPU usage again and confirmed that the Java process returned to normal levels.

4. Summary

Beyond functional testing, developers should also monitor performance impact and address problems promptly.

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.

BackendJavaCPUjstack
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

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.