Why Does My Java Service Hit 100% CPU? 8 Real-World Causes and Fixes
This article examines eight common reasons why Java backend services experience 100% CPU usage—such as fetching excessive data, automatic Kafka acknowledgments, infinite loops, multithreaded imports, massive file syncs, deadlocks, inefficient regular expressions, and heavy real‑time calculations—and provides practical mitigation strategies.
1. Too Much Data at Once
In a restaurant ordering system, the service subscribed to a Kafka topic to receive updated dish data. Initially it worked fine, but a bug caused the system to receive the full dataset on each update instead of incremental changes, leading to massive data processing, frequent full GC, and CPU spikes.
2. Kafka Automatic Acknowledgment
The downstream service originally used Kafka's automatic acknowledgment to simplify code. As message volume grew, the automatic commits caused the consumer to process messages faster than it could handle, eventually driving CPU usage to 100%. Switching to manual acknowledgment resolved the issue.
3. Infinite Loops
Both classic while/for loops and unbounded recursion can keep the JVM busy, storing loop counters or recursion depth in registers and consuming CPU continuously. In JDK 1.7, certain multithreaded HashMap operations could also create hidden infinite loops, further increasing CPU load.
4. Multithreaded Data Import
A supplier Excel import was refactored from single‑threaded processing to a thread‑pool based multithreaded approach, dramatically speeding up the import. However, the increased number of threads caused excessive context switching, which heavily taxed the CPU.
5. Bulk File Synchronization
When a new festive template was applied to dozens of game websites, all generated static HTML files had to be synchronized to the web server in one batch. The massive file copy operation overwhelmed the application server’s CPU.
6. Deadlocks
Improper use of synchronized or Lock can lead to situations where two threads each hold a lock the other needs, causing a deadlock and a CPU spike. Example thread dump:
"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)When such a deadlock occurs, the CPU usage remains high because the threads are stuck waiting.
7. Inefficient Regular Expressions
A poorly written regex can cause massive backtracking in Java’s NFA engine, leading to severe CPU consumption. Example pattern:
^([hH][tT]{2}[pP]:(//|[hH][tT]{2}[pP][sS]://))(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~/])+$Minimizing backtracking and simplifying the expression helps keep CPU usage low.
8. Heavy Real‑Time Calculations
Real‑time business logic such as calculating discounted prices or aggregating large data sets under high concurrency can become CPU‑intensive. If the computation is not optimized, it can continuously consume CPU cycles and cause spikes.
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.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
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.
