30

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.

Community
  • 1
  • 1
JohnPristine
  • 3,485
  • 5
  • 30
  • 49

8 Answers8

65

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
Petro Semeniuk
  • 6,970
  • 10
  • 42
  • 65
9

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.

Wyzard
  • 33,849
  • 3
  • 67
  • 87
7

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.

rkrakz
  • 71
  • 1
  • 3
5

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 1
    Works with top/htop too. Start it and press H for thread mode – moodywoody Mar 30 '12 at 04:57
  • To get a list of running java processes and the number of respective threads: ps -Haxo pid,stat,cmd | grep -v grep | grep java | sort | uniq -c | sort -nr – FuePi Feb 28 '20 at 15:08
3
  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.
vasste
  • 56
  • 2
1

Since java 1.8 you can use jcmd docs.oracle.com

list of runing jvm

jcmd -l

stack of threads inside jvm

jcmd 123 Thread.print
europrimus
  • 53
  • 5
1

You can make a JNI jump into native code to obtain the native TID associated with the particular Java thread. Then use OS commands or procfs as others have suggested or even better send the particulars of the thread back up to Java.

Example: native code

JNIEXPORT jlong JNICALL Java_nativeGetThreadId(JNIEnv * env, jobject obj) {
    jlong threadId;
    threadId = (jlong)gettid();
    return threadId;
}
Java42
  • 7,628
  • 1
  • 32
  • 50
1

If you have JDK installed, you can use a tool called jvisualvm to see the threads (and do many others operations relavant to a java process - see memory, quick check on objects etc)

Jayan
  • 18,003
  • 15
  • 89
  • 143