Determining the source of high java CPU

This post is related to a situtation in which you find yourself with high CPU utilization, but can’t determine the source. This will gather thread dumps and the CPU used by each thread. For the threads that use a large amount of CPU, you can convert the PID to a hexadecimal nid, and get the thread stack trace in the dump.

This script is useful for the java HotSpot compiler.

For example, if in the output generated below you determine that between two runs of the script PID 25221 is using a lot of CPU, you can get the nid (thread id, essentially) by doing the following…

-bash-4.1$ printf "%x\n" 25221
6285
-bash-4.1$

You can then search the thread dump for that thread by searching for nid=0x6285, and examine its stack trace.

The complete gathering script is shown below.

#!/bin/sh
<
${DATE}_dump.txt ps -eLfp $PID | awk '{print $4,$9}' >> ${DATE}_dump.txt

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.