Java which thread is using cpu
This is quite straightforward:. The first column is the PID, let's extract it. Unfortunately it turned out that top uses ANSI escape codes for colors - invisible characters that are breaking tools like grep and cut. Here comes the best part: if you look at jstack output available in JDK , each thread has some mysterious ID printed next to its name:. The last line simply dumps the JVM stack trace of the given PID and filters out using grep the thread which has matching nid. Guess what, it works:.
Running the script multiple times or with watch, see below will capture Busy thread in different places, but almost always inside regular expression parsing - which is our problematic piece! In case your application has multiple CPU-hungry threads, you can use watch -n1.
Testing with the following program:. Knowledge Base. Idea Exchange. Turn on suggestions. Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type. Showing results for. Show only Search instead for. Did you mean:. Sign in to Community. Not a customer? Start a free trial Click the Start a free trial link to start a day SaaS trial of our product and join our community as a trial user.
Does anyone know how this TID can be correlated to a thread dump? They don't match the "tid" or "nid" identifiers I'm seeing in my thread dumps after converting them to hex?
This was perfect for my usage — Sanjiv Jivan. SiHa 6, 12 12 gold badges 28 28 silver badges 40 40 bronze badges. Cowan Cowan 36k 11 11 gold badges 65 65 silver badges 64 64 bronze badges. I tried Bruce Chapman's command line Hot Thread detector and it worked for me it needed some class from tools. I prefer a command line tool in this case since system is very usually so slow that GUI tools are a real pain.
You can still read his post though, but not download any of his files. EduardoBezerra: downloading seems to work again — serv-inc. Lawrence Dol Lawrence Dol Even with JConsole bundled with Java 1. JVisualVM is bundled with J6 too. Any commercial Java profiler application will do the same, if for some reason you are stuck on an earlier version of the JDK. Bill: Good point. I have personally used JProfiler for years with code from Java 1 on and found it to work very well. Thank you.
JVisualVM indeed shows threads as runnable or not. But that does not mean they are actually running, some may be sitting on a socket - not sure why they are still marked as runnable - note that some threads are always green in JVisualVM threads view.
So it may give you an idea but not a definite answer to what thread is hogging cpu. Show 1 more comment. Mark Mark We just used this approach for locating a Thread loop and it worked fine. The Eclipse debug view is great for starting and stopping threads. You can pause all of them and then reactivate them one by one to see which one is spinning.
Alternately you can use this method to find the problem thread, record the thread ID, use java visual VM to do a thread dump, and find the name of the problem thread by comparing the thread IDs. Serge Serge 2, 15 15 silver badges 26 26 bronze badges.
Now let us see the threads that are taking most time 1 8? Use jstat not jstack! Alex Punnen Alex Punnen 3, 1 1 gold badge 41 41 silver badges 57 57 bronze badges. Paul Fisher Paul Fisher 9, 5 5 gold badges 34 34 silver badges 52 52 bronze badges. But this is quite tedious, especially if there is many threads and when on a one-CPU machine, which is the case of a small EC2 instance and when you run in VirtualBox. Neil Coffey Neil Coffey Peter Lawrey Peter Lawrey k 73 73 gold badges silver badges bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Visit chat.
Linked Related
0コメント