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.
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.
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.
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.
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.
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).
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%.
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.
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!
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.
