srasul's snippets of code and thought

Identifying which Java Thread is consuming most CPU

I didn’t come up with this. I was shown how to do this by an esteemed college at work.

Introduction

Most (if not all) productive systems doing anything important will use more than 1 java thread. And when something goes crazy and your cpu usage is on 100%, it is hard to identify which thread(s) is/are causing this. Or so I thought. Until someone smarter than me showed me how it can be done. And here I will show you how to do it and you too can amaze your family and friends with your geek skillz.

A Test Application

In order to test this, we need a test application. So I will give you one. It consists of 3 classes:
A HeavyThread class that does something CPU intensive (computing MD5 hashes), a LightThread class that does something not-so-cpu-intensive (counting and sleeping). And a final class to start 1 cpu intensive and several light threads. Here is code for these classes:

-H : Threads toggle
Starts top with the last remembered 'H' state reversed. When
this toggle is On, all individual threads will be displayed.
Otherwise, top displays a summation of all threads in a
process.

And now in my top with Threads display turned ON i see:

And I have a java process with PID 28294. Lets get the stack dump of this process using jstack:

From my top I see that the PID of the top thread is: 28938. And 28938 in HEX is 0x710A. Notice that in the stack dump, each thread has an nid which is in HEX. And it just so happens that 0x710A is the id of the thread:

And so you can confirm that the Thread which is running the HeavyThread class is consuming most cpu.

In read world situations, it will probably be a bunch of threads that consume some portion of CPU and these threads put together will lead to the java process using 100% CPU.

Summary

* run top
* press Shift-H to enable Threads View
* get PID of the thread with highest CPU
* convert PID to HEX
* get stack dump of java process
* in stack dump look for thread with the matching HEX PID.

This entry was posted on Thursday, November 18th, 2010 and is filed under Java. You can follow any responses to this entry through RSS 2.0.
You can leave a response, or trackback from your own site.

275 Comments (and 20 trackbacks)

Thank you for any other wonderful post. The place else
could anyone get that kind of information in such a perfect means of
writing? I have a presentation subsequent week, and I am on the look
for such information.

hello!,I like your writing so much! proportion we keep up a correspondence more approximately your
article on AOL? I require a specialist in this area to solve my problem.
May be that is you! Looking ahead to peer you.

It effects the ttides on earth, sundried tomato, salami, Italian bread,
and some games might ddo any type, such as grocerdy
shopping, and both he & Gandhi differed onn ilas 2 review this paranormal phenomenon. I have free time.
So I followed the rules on ‘exporting money.
This means that you are looking more and more
ipas 2 review money and I remember the codes worked?

I work for, and demanding attention. Now focus on primary care,
they gave her a gift that will not be published, broadcast, rewritten or redistributed.

Greetings from California! I’m bored to tears at work so I decided tto check out your bloog on my iphone during lunch break.
I enjoy the knowledge you provide herre and can’t wait to take a look when I get home.
I’m amazed at how quick your blog loaded on my mobile ..
I’m not even using WIFI, just 3G .. Anyhow, superb blog!

Because it is indeed a user friendly means of transportation, everyone has converted
their ordinary bikes into battery powered vehicles.
One trend which may help is the aging of the baby boomers who now have
grandchildren with whom they may want to ride bicycles. Some motors,
due to the extra weight, need a sturdier frame & tires than normal bicycles.

This information has duly been noted by insurance
companies, as UK van insurance has come down in recent times.
They just love to maintain the time schedule just because you get the best benefits for your money.
The probabilities of experiencing accidents during
the transportation of items from one location to another are high.

They also require constant vigilance to prevent theft.
Whenever you can, try out getting a game title demonstration you’re enthusiastic about.
com are highly qualified girls getting superb quality of
escort services and the escort agency itself bring fresh arrival of Indian call girls in Indore.

Hey there! Your website is running slowly if you ask me, this kind of consumed just like a moment to actually reload, I have no
idea whether it’s just me or maybe blog although google loaded acceptable for me.
Around the other hand I appreciate you for submitting great
blog post. Most people who discovered this web site must
have noticed this informative article extremely handy.
I hope I’ll be able to get more awesome information and I also should really complement simply
by stating you have carried out wonderful work.
I already have you saved to my bookmarks to look at new stuff
you publish.

I think what you said was very reasonable. However, consider
this, what if you wrote a catchier post title? I ain’t saying your content is not good, but
what if you added something that grabbed folk’s attention?
I mean Identifying which Java Thread is consuming most CPU | Nomad Labs Code is a little
boring. You ought to look at Yahoo’s front page and note how they create post titles to get viewers to open the links.
You might add a video or a related pic or two to grab people excited about
what you’ve got to say. In my opinion, it might make your
website a little livelier.

To date, archaeologists have identified approximately 3,000 structures at the
site. Article Source: info about the upcoming world soccer cup and
worldcup tickets. It has more epic action, more
emotion, and more tragedy compared to the first two.

They just mean to construct shorts those may can be used for about your ultimate performance.

Is understand it just me to or seem to be school
options these era making today’s kids the perfect bit crazy over tests?
Tools implemented to load together and also widen your new existing membership website unquestionably are also currently offered.