How To Troubleshoot a Java program with high cpu usage?

Leave a comment

March 24, 2014 by huionn

Sometimes a buggy java program may consume a lot of CPU. In order to troubleshoot it, you need to identify which threads in the java program are taking up so much cpu time.

There are two ways to find the problematic threads:

1) use jconsole plugin such as “top thread” plugin (http://lsd.luminis.nl/top-threads-plugin-for-jconsole/) or jtop plugin (demo plugin for jconsole). The plugin gathers the thread info through JMX.

2) use linux top command and stack trace

To check which in the Java process that has high CPU usage:

top –p <PID> –H

By default, the threads are sorted by cpu usage. pid can be obtained from either ps or jps.

Then use jstack to dump the stack traces of threads.

jstack –l [-m] <PID> > /tmp/stackdump

Depending on the format of jstack output, you may match the Thread ID from top directly to jstack output, or you may need to convert the Thread ID to hex to match nid (native thread id) in jstack output.

for example,  if the thread id (in PID column of ‘top’) is 4096, the nid is 0x1000.

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: