Why Does My Java Service Hit 100% CPU? 8 Real-World Causes & Fixes
This article examines common reasons behind persistent 100% CPU usage in Java backend systems—ranging from excessive full GC and Kafka auto‑acknowledgment to dead loops, thread‑pool overload, massive file sync, deadlocks, inefficient regex and heavy real‑time calculations—offering practical diagnostics and solutions.
1. Too Much Data at Once
In a restaurant‑related system we subscribed to a Kafka topic that delivered the latest dish updates. After a bug caused the topic to emit the full list of dishes instead of incremental changes, the service fetched massive amounts of data repeatedly, triggering frequent full garbage collections and driving CPU usage to 100%.
2. Kafka Auto Acknowledgment
Initially we used Kafka's automatic acknowledgment to simplify consumer code. As the business grew, the volume of messages increased dramatically, and the auto‑ack approach caused the consumer to reprocess messages under load, leading to sustained CPU spikes. Switching to manual acknowledgment resolved the issue.
3. Infinite Loops
Typical infinite loops—either in while/for/forEach constructs or via uncontrolled recursion—keep the CPU busy. They may also arise from data structures such as HashMap under certain JDK 1.7 multithreaded scenarios, where internal linked‑list loops cause endless processing.
4. Multithreaded Data Import
A colleague refactored a single‑threaded Excel import into a thread‑pool‑based multithreaded version to improve performance. While import speed increased, the large number of threads caused excessive context switching, consuming CPU resources and again pushing usage to 100%.
5. Bulk File Synchronization
When a festive event required updating the style of every game’s static website, we regenerated all HTML files and synchronized them to the web server in one batch. The massive file copy operation overloaded the application server’s CPU.
6. Deadlocks
Using synchronized or Lock to protect shared resources can lead to deadlocks when multiple threads acquire locks in different orders. A classic example is thread A holding lock C and waiting for lock D, while thread B holds lock D and waits for lock C. The resulting deadlock stalls progress and spikes CPU usage.
"pool-4-thread-1" prio=10 tid=0x00007f27bc11a000 nid=0x2ae9 waiting on condition [0x00007f2768ef9000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000090e1d048> (a java.util.concurrent.locks.ReentrantLock$FairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)7. Regex Matching
Poorly written regular expressions can cause catastrophic backtracking in Java's NFA engine, dramatically increasing CPU consumption. For example, a complex URL‑validation pattern that matches protocol, subdomain, and arbitrary characters may backtrack for minutes on malformed input.
^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~/])+$8. Time‑Consuming Calculations
Real‑time calculations such as computing discounted prices or aggregating large data sets can be CPU‑intensive, especially under high concurrency. If these operations are not optimized, they become a major source of sustained high CPU usage.
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.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
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.
