How to find a Java thread running on Linux with ps -axl?

61,810

Solution 1

Use

jps -v

for finding your java process. Sample Output:

3825 RemoteMavenServer -Djava.awt.headless=true -Xmx512m -Dfile.encoding=MacRoman
6172 AppMain -Didea.launcher.port=7533 -Didea.launcher.bin.path=/Applications/IntelliJ IDEA 10.app/bin -Dfile.encoding=UTF-8
6175 Jps -Dapplication.home=/Library/Java/JavaVirtualMachines/1.6.0_31-b04-411.jdk/Contents/Home -Xms8m

Then use

jstack 6172

(6172 is id of your process) to get stack of threads inside jvm. Thread priority could be found from it. Sample output:

.....
"main" **prio=5** tid=7ff255800800 nid=0x104bec000 waiting on condition [104beb000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at au.com.byr.Sample.main(Sample.java:11)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

..... 

Enjoy!

EDIT: If application running under different user than yourself (typical case on production and other non-local environments) then jps/jstack should be run via sudo. Examples:

sudo jps -v

sudo jstack 6172

Solution 2

On Linux, the Sun/Oracle JVM implements Java threads using native Linux threads, so yes, you can see them in "ps" output. Any thread belonging to a javaprocess is a Java thread. But you won't see the threads' names, since those are specific to Java and the OS doesn't know about them.

Linux threads do have IDs, but they're just numbers, and "ps axl" doesn't show them. "ps -eLf" does, in the "LWP" column. ("LWP" is short for "lightweight process", which is another name for a thread.) Within Java, the Thread.getId() method might return the LWP number that you see in "ps -eLf" output, but I'm not sure.

Solution 3

All of the methods mentioned here work just fine. I was also searching for something similar and came across this blog by Timur Akhmadeev. I hope it helps.

Edit:

As I was pointed by fellow programmers, the following is a summary of the post by Timur:

On a *nux based system first do a

top -H

to show you CPU usage on a per-thread basis. Look for user as oracle and in the command as Java. Note the PID for this process and then run

top -H -p PID

This brings up a list that displays all the tasks that the process(java program) is currently performing. After this we need to know what task each thread may be performing, for which we use a utility that the jdk provides, namely jstack. In linux, Java (JVM HotSpot) threads are mapped to threads that of the kerner.

jstack -pid_of_the_thread

To map OS level thread to a Java thread in a thread dump, we need to convert native thread ID from Linux to base 16, and search for “nid=$ID” in the stack trace. For example, thread ID is 7601 is 0x1db1 may be one you were monitoring. If you would like to further keep an eye out for what the thread may do in the future, i.e, track its behavior, simply write a shell script to watch for changes. Hope this makes sense.

Solution 4

The ps(1) thread-selector switch H will ask ps(1) to show threads as processes. (Which is roughly how they're implemented anyway.)

See:

$ ps axl | wc -l
163
$ ps axlH | wc -l
325

Apparently I've got a lot of threaded processes running right now.

Solution 5

  1. find threads ID by top with option H "on", and write down threads PIDs
  2. make thread dump and find the stack of your thread. There are PIDs in hex format.
Share:
61,810
JohnPristine
Author by

JohnPristine

I hope I could help.

Updated on July 06, 2021

Comments

  • JohnPristine
    JohnPristine almost 3 years

    I have a running JVM with two threads. Is it possible to see these running threads on my Linux OS with ps -axl ? I am trying to find out what priority the OS is giving to my threads. More info about this other issue here.