Which (java) thread is consuming maximum CPU?

Problem Statement

Your java application it utilizing very high cpu (eg: 100%), you do not know which java thread is causing this. In Java Web Application which is a multi-threaded environment, it becomes important to know this. Thread Dumps tell us what the threads are doing, but they do not tell us which thread is utilizing the maximum CPU.

Solution

On Linux, following commands will help us:

top command can tell us which (java or any) process is utilizing how much CPU

top -H command gives similar output, but this time by Linux Threads (instead of Processes), so from here we can determine the thread id that is using large cpu. But remember this thread id is decimal notation, while jstack output (thread dump) has thread id in hexadecimal so a conversion from decimal to hexadecimal is needed, before you can locate the offending thread in thread dump (the ‘nid’ field in thread dump corresponds to it).

On Linux you can convert decimal to hex as follows:

$ printf '%x' 65332
7541

Here is what your a contents of Thread Dump looks like (see the nid=0x7541 below):

'Busy' prio=10 tid=0x8f3cf400 nid=0x7541 runnable [0x3f25b120]
java.lang.Thread.State: RUNNABLE
at java.something.do(Whatever.java:114)

Other Unix Flavors:

Ref:
http://www.javacodegeeks.com/2012/08/which-java-thread-consumes-my-cpu.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: