Why My Java App Hits 100% CPU: Live Infinite Loop Demo & Diagnosis

This article walks through setting up a Vagrant‑based experiment that injects an intentional infinite loop into a simple Spring MVC service, then demonstrates step‑by‑step how to identify the offending process using top, examine JVM heap with jstat, and trace the problematic thread with jstack to resolve a CPU‑100% issue.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why My Java App Hits 100% CPU: Live Infinite Loop Demo & Diagnosis

CPU usage hitting 100% can have many causes; an infinite loop in code is a classic one. To explore this, I created a controlled experiment (not in production) using Vagrant, VirtualBox, and Ansible to provision a simple Spring MVC application that contains an endpoint with an intentional infinite loop.

Environment diagram
Environment diagram

The loop is exposed at http://192.168.88.10:9898/web/loop. After hitting the URL twice, the CPU stays around 120‑130% instead of 200%.

1. Using top to locate the offending process

First I opened a terminal and ran top to see which process was consuming CPU. The request URL was accessed, and the top output showed the Java process with elevated CPU usage.

top output
top output

2. Checking JVM heap with jstat

The issue does not involve heap pressure, so I ran jstat -gcutil 32593 1s (where 32593 is the Java PID) to sample heap usage every second. The output showed normal heap utilization.

jstat output
jstat output

3. Investigating the stack to find the busy thread

Since the heap was fine, I listed the Java threads with top -H -p <java_pid> to see which thread was using CPU.

thread list
thread list

Next I dumped the stack of the suspect thread using jstack -l <thread_pid> >> stack.log (chosen thread PID 3596, which is 0xe0c in hexadecimal).

jstack output
jstack output

4. Summary

Through this step‑by‑step investigation—using top to locate the process, jstat to rule out heap issues, and jstack to pinpoint the looping thread—we demonstrated a practical method for diagnosing a Java application that drives CPU usage to 100%.

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.

JavaJVMCPUtopSpring MVCPerformance debuggingjstackinfinite loop
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.