High Java CPU Usage Problems
High Java CPU Usage Problems
theserverside.com/answer/How-to-fix-high-Java-CPU-usage-problems
One of the first indicators of a runtime performance problem is a high Java CPU usage
report from a JVM profiler or Java monitoring tool. Unfortunately, high Java CPU
utilization problems on Windows and Linux are not always easy to resolve, as this metric
is often a red herring for a problem that is peripheral to the CPU.
Blocked threads with contention issues can also cause JVM profiler tools to report 100%
Java CPU utilization. Concurrency problems and deadlocks aren't really a processor issue,
but instead a problem with how threads are allocated, and the methods they access are
synchronized or blocked.
When a CPU is in an idle state, it reports its status as being unused, as it's not doing any
work. However, when threads are blocked, they will put the CPU in a wait state. The CPU
doesn't perform any logic when it's in the wait state, but it reports back to JVM profiling
tools that it's busy, despite the fact that it's doing nothing.
Furthermore, just because your hardware reports 100% CPU utilization, don't assume
that it's the JVM causing it. CPU usage might skyrocket when your application is under
load, but that spike might be attributable to a system process or a misconfiguration of the
software stack. If a server's virtual memory is misconfigured, page file thrashing will
1/4
consume a majority of the CPU cycles. A virtual memory problem needs to be solved by
the DevOps team or system admins. It's not a problem that's attributable to your app or
how you've tuned the performance of the JVM.
JDK Mission Control can help isolate errant code that triggers high Java CPU usage.
Another misleading source of high Java CPU usage is a poorly designed RESTful API that
makes too many network calls to other services. Chatty applications with a large number
of HTTP requests, along with the associated overhead of parsing JSON and XML on each
request-response cycle, will often trigger 100% Java CPU usage reports. This problem has
become increasingly common in modern enterprise architectures, as developers re-
architect software monoliths into microservices.
When you troubleshoot high Java CPU usage problems, the first step is to eliminate the
various red herrings mentioned above. To review, these peripherally related issues
include:
2/4
Only after root-cause analysis eliminates these issues as a potential cause of the high Java
CPU usage problem should time be taken to troubleshoot potential issues in the code.
Learn more about Java Flight Recorder and Java Mission Control
Current Time 0:00
Duration 46:15
Loaded: 0.36%
What could possibly be the culprit when your Java code is putting too much stress on the
CPU? The most common, directly attributable causes of high Java CPU usage problems
include:
Infinite loops
Be it a fencepost error or just sloppy development, it's not unheard of for a programmer to
start a loop and incorrectly code the condition that breaks out of it. An infinite loop that
does nothing but consume clock cycles is the result. If multiple threads hit this line of
code, you have a multi-threaded application doing nothing but meaningless iterations.
Eliminate the infinite loops and CPU usage should go back to normal.
The CPU executes logic. If an application includes poorly written workflows, and the code
is wired together like a plate of spaghetti, then your CPU will devour needless clock cycles.
Update commonly invoked workflows and rework poorly performing algorithms to get the
most out of your CPU.
Recursive logic
While some programming languages are optimized for recursive logic, Java isn't one of
them. Recursive algorithms create threads that are hard to break out of, allocate object
that are not easily reclaimed by garbage collection algorithms, and they create a tower of
3/4
Java stack frames that are difficult to unwind. Throw in the looming threat of a
StackOverflowError, and the case for iterative over recursive algorithms isn't a difficult
one to make.
List processing lies at the heart of most enterprise applications. As such, developers have
many collection classes to choose from. If a developer chooses to use a LinkedList instead
of an ArrayList on a large data set, CPU utilization will go through the roof. Similarly, if a
developer chooses to use the older Hashtable over a HashMap, synchronization may
needlessly consume clock cycles. Choose the wrong Java collection class, and application
performance will suffer. Choose the correct collection classes, and your high Java CPU
usage problems will disappear.
It's not uncommon for a given value to be calculated numerous times throughout an
application. If this is the case, hold the result of the first calculation in a variable and
reference that variable on all future interactions. Small changes like this can have a
significant impact on application performance, especially if cryptography, graphics
manipulation or other CPU-intensive operations are involved.
With a good JVM profiler like Java Flight Recorder, and an analytics tool like JDK
Mission Control tool available to inspect the results, identifying the culprit responsible for
high Java CPU usage problems shouldn't be a problem. And once identified, finding a fix
is just a matter of implementing new software routines and testing the results until the fix
is in.
4/4