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
- Using jstack or visualvm, take a Thread dump.
- Convert LWP ID to Hex and search for the ID in Thread dump.
We get an output as shown in the figure
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).
Then you can narrow down the thread which is consuming max CPU and investigate further.