Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

High CPU Utilization in java application - why?

I have a Java Application (web-based) that at times shows very high CPU Utilization (almost 90%) for several hours. Linux TOP command shows this. On application restart, the problem goes away.

So to investigate:

I take Thread Dump to find what threads are doing. Several Threads are found in 'RUNNABLE' state, some in few other states. On taking repeated Thread Dumps, i do see some threads that are always present in 'RUNNABLE' state. So, they appear to be the culprit.

But I am unable to tell for sure, which Thread is hogging the CPU or has gone into a infinite loop (thereby causing high CPU util).

Logs don't necessarily help, as the offending code may not be logging anything.

How do I investigate - What part of the application or what-thread is causing High CPU Utilization? - Any other ideas?

like image 532
Jasper Avatar asked Apr 04 '13 12:04

Jasper


People also ask

What causes high CPU usage in Java application?

Peripheral causes of high Java CPU usagebad JVM memory management; poorly configured Java GC; issues more correctly attributable to the software stack; thread synchronization, contention and deadlock issues; and.

What causes the CPU usage to be high?

You can expect high CPU utilization when playing some games, running a video-editing or streaming application, performing an antivirus scan, or juggling many browser tabs.

How can I reduce CPU usage in Java?

You can set the priority of your whole Java application process to be lower than other applications; that's what the "nice" or "renice" commands do for your on Unix (or Mac OS X). Alternatively, and I think nicer, you can start a reduced-priority thread within your application, to do the CPU-intensive work.


2 Answers

If a profiler is not applicable in your setup, you may try to identify the thread following steps in this post.

Basically, there are three steps:

  1. run top -H and get PID of the thread with highest CPU.
  2. convert the PID to hex.
  3. look for thread with the matching HEX PID in your thread dump.
like image 102
ericson Avatar answered Oct 07 '22 18:10

ericson


You may be victim of a garbage collection problem.

When your application requires memory and it's getting low on what it's configured to use the garbage collector will run often which consume a lot of CPU cycles. If it can't collect anything your memory will stay low so it will be run again and again. When you redeploy your application the memory is cleared and the garbage collection won't happen more than required so the CPU utilization stays low until it's full again.

You should check that there is no possible memory leak in your application and that it's well configured for memory (check the -Xmx parameter, see What does Java option -Xmx stand for?)

Also, what are you using as web framework? JSF relies a lot on sessions and consumes a lot of memory, consider being stateless at most!

like image 34
Alexandre Jacob Avatar answered Oct 07 '22 18:10

Alexandre Jacob