8 Code Patterns That Can Spike Your CPU to 100%

The article enumerates eight common coding practices—such as fetching massive data at once, using Kafka auto‑acknowledgement, infinite loops, aggressive multithreaded imports, bulk file synchronization, deadlocks, poorly designed regexes, and heavy real‑time calculations—that can cause Java services to hit 100% CPU usage, explaining the underlying mechanisms like full GC, thread‑context switching, and NFA backtracking.

Programmer XiaoFu
Programmer XiaoFu
Programmer XiaoFu
8 Code Patterns That Can Spike Your CPU to 100%

1. Fetching Too Much Data at Once

In a restaurant‑related system, a bug caused the consumer to receive the full list of dishes instead of incremental updates. Frequent full data reads triggered continuous full gc, driving CPU usage to 100%.

2. Kafka Auto‑Acknowledgement

The downstream service originally used Kafka's auto‑acknowledge mode for convenience. As message volume grew, the auto‑acknowledge approach caused the consumer to process messages faster than it could handle, leading to sustained CPU spikes. Switching to manual acknowledgement resolved the issue.

3. Infinite Loops

Two kinds of infinite loops were described: loops that never terminate ( while, for, forEach) and unbounded recursion. Both keep the CPU busy by continuously updating a register with the iteration count or recursion depth. In JDK 1.7, certain multithreaded HashMap insertions could also create hidden loops, further increasing CPU load.

4. Multithreaded Data Import

A supplier‑excel import feature was refactored from single‑threaded to a thread‑pool implementation to improve throughput. While import speed increased, the large number of threads caused excessive context switches, consuming significant CPU resources and causing usage to climb.

5. Bulk File Synchronization

When a game platform regenerated static HTML pages for many games and synchronized all files to a web server in one batch, the massive I/O operation saturated the application server's CPU.

6. Deadlocks

Using synchronized or Lock can lead to deadlocks when multiple threads each hold a lock needed by the other. The article illustrated a classic deadlock scenario where thread A holds lock c and waits for lock d, while thread B holds lock d and waits for lock c, causing both threads to stall and CPU usage to rise. A thread dump example was provided:

"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 <0x0000000090e1d048> (a java.util.concurrent.locks.ReentrantLock$FairSync)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)

7. Regex Backtracking

A poorly written regular expression used to validate URLs caused the Java NFA engine to perform extensive backtracking. The backtracking can take minutes or hours depending on pattern complexity, dramatically increasing CPU consumption. The article showed an example regex and broke it into three logical parts.

8. Expensive Real‑Time Calculations

Real‑time price calculations or aggregations that are both CPU‑intensive and highly concurrent can keep the CPU busy for extended periods, leading to sustained high usage.

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.

deadlockKafkaJava performancemultithreadingregexfull GCCPU usage
Programmer XiaoFu
Written by

Programmer XiaoFu

xiaofucode.com – a programmer learning guide driven by the pursuit of profit

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.