10 August 2011 ~ 2 Comments

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.


Tags: , ,

2 Responses to “Hunting down CPU hogging Java Thread”

  1. Shalom 19 March 2012 at 6:09 pm Permalink

    Is it not enough to just close the file hogging CPU through Alt Cnt Del?

    • ashish 28 March 2012 at 6:13 pm Permalink

      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.

      cheers


Leave a Reply