[Java] Check the status of the Java application without using a monitoring tool

1 minute read

Overview

jstack is a command that comes with the JDK. If the machine is running java, you can easily get the process status without installing a special monitoring tool.

This tool displays the thread dump in the command line console. You can use it if you know the Java PID. When performance does not appear or when a phenomenon such as deadlock occurs, you can use this tool to acquire the thread state and use it for investigation.

Usage

$ jstack -l <pid>

You can check the stack trace for each thread as the output result.

"Thread-0" #12 prio=5 os_prio=0 cpu=0.00ms elapsed=496.25s tid=0x00007f6a741f1800 nid=0x1a5 waiting on condition [0x00007f6a17a9f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep([email protected]/Native Method)
        at Server.sleepForever(Server.java:20)
        at Server.lambda$0(Server.java:9)
        at Server$$Lambda$1/0x0000000840060840.run(Unknown Source)
        at java.lang.Thread.run([email protected]/Thread.java:834)

"Thread-1" #13 prio=5 os_prio=0 cpu=0.00ms elapsed=496.25s tid=0x00007f6a741f3800 nid=0x1a6 waiting on condition [0x00007f6a1798f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep([email protected]/Native Method)
        at Server.sleepForever(Server.java:20)
        at Server.lambda$0(Server.java:9)
        at Server$$Lambda$1/0x0000000840060840.run(Unknown Source)
        at java.lang.Thread.run([email protected]/Thread.java:834)

"Thread-2" #14 prio=5 os_prio=0 cpu=0.00ms elapsed=496.25s tid=0x00007f6a741f5000 nid=0x1a7 waiting on condition [0x00007f6a1787f000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep([email protected]/Native Method)
        at Server.sleepForever(Server.java:20)
        at Server.lambda$0(Server.java:9)
        at Server$$Lambda$1/0x0000000840060840.run(Unknown Source)
        at java.lang.Thread.run([email protected]/Thread.java:834)

For example, in the case where java is running with multiple threads, when a phenomenon such as some thread particularly consuming a lot of CPU usage or memory, it can be specified as follows.

① Identify the process that is eating resources from the process.

$ top -H -b -n 1 -p ${java pid}

Get the PID eating the resource from the above command

② Convert PID to hexadecimal and grep from jstack result ③ The trace whose hexadecimal number matches with nid is a resource-consuming process, so follow it from there.

It is possible to investigate in such a flow.

Tags:

Updated: