Sunday, 6 October 2013

How to identify a High CPU thread in a Java application

Objective

To understand the basics of how a high cpu thread can be identified in a Java application.

Notes

  1. There are many good articles in net which tell step by step instructions to identify a high cpu thread in a Java application running on different OS. In this article I just try to summarize the key concepts instead of walk you through the steps/examples.
  2. The Linux OS have one to one mapping between Java (version 1.2 onwards) threads and OS threads, and the easiest way to identify a high cpu thread is:
    1. Run command: top -H
  3. The output of above command will be PID (thread id) sorted in descending order of CPU usage.
  4. Once you get the PID of high cpu thread (probably you need to monitor the high CPU thread for some time to see if it is keeping the CPU usage high for some longer time), take a thread dump of your Java application (via jstack or any other profiling tool lie VisualVM).
  5. The PID you got has to be converted into Hexa number before you search that into thread dump to identify which Java thread in your application it is and with the stack trace in dump you will be able to identify what kind of task the thread is doing.
Thanks You!