Hunting down CPU hogging Java Thread

Most of us have encountered a situation to find cause of high CPU usage in Java application. Profiling is the best way, but at times running Profiler in production is not an option. Fortunately, there is a simple way, if you are running your app on *nix.

Lets explore how to find this.

  • Find the pid of the application, using top or jps command
  • Once you get the pid, run following command
    $ ps -L pid
  • We get an output as shown in the figure

    ps output

    The output displays all the Threads in the application along with the time spent. Find the Thread that has spent highest time in execution (Entry circled on right). Once we identify this, get the LWP ID of the Thread (Entry circled on left).

  • Using jstack or visualvm, take a Thread dump.
  • Convert LWP ID to Hex and search for the ID in Thread dump.

Then you can narrow down the thread which is consuming max CPU and investigate further.

2 thoughts on “Hunting down CPU hogging Java Thread

    • Not its not, as it might be result of a bug in the code. This shall depend on your use case, the post just describes how you can locate the source. In my case it was always the case, and located the cause and fixed the same. Hope it answers your question.


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.