JVM调试常用命令——jstack命令与Java线程栈(1)

1 jstack 命令

jstack命令的主要作用是打印指定Java进程中每一个线程的工作状态,以及每个线程栈当前的方法执行顺序等详细情况。为什么jstack命令不和jmap、jinfo、jstat等命令一同讲解,而要单独成文呢?因为通过jstack命令给出的线程栈详细情况,可以有助我们与我们反向理解JVM 栈的内部结构。

1.1、jstack命令

1.1.1、jstack基本命令介绍

从简单来说,jstack就是打印当前指定java进程中每一个线程的工作情况,但实际上远没有那么简单。这里我们首先介绍jstack命令的基本使用,在本小节中有一些jstack打印出来的关键信息不会立即进行讲解(但后文都会进行详细说明),但没有关系,因为只要不影响当前本小节的阅读即可。通过以下命令,我们可以查看jstack的基本使用:

#jstack -help
Usage:
    jstack [-l] <pid>
        (to connect to running process)
    jstack -F [-m] [-l] <pid>
        (to connect to a hung process)
    jstack [-m] [-l] <executable> <core>
        (to connect to a core file)
    jstack [-m] [-l] [server_id@]<remote server IP or hostname>
        (to connect to a remote debug server)

Options:
    -F  to force a thread dump. Use when jstack <pid> does not respond (process is hung)
    -m  to print both java and native frames (mixed mode)
    -l  long listing. Prints additional information about locks
    -h or -help to print this help message

pid就是当前要进行线程栈情况分析的Java进程的进程号,另外的参数包括:

  • F 强制进行线程栈dump信息的显示,这种情况下一般都是JVM进程已经因为某种原因停止响应(急需进行进程详情分析)。这个参数很有用,因为一般做java进程的线程栈分析,都是因为java进程不工作了——你工作繁忙的时候不会对工作正常的线上节点进行持续jstack分析吧,即使进行分析,看到的各线程栈的工作情况可能也是正常的。

  • m 打印包括java线程(用户进程、GC进程、JVM内部工作线程)和 C/C++ JNI native线程(如果当前快照时间点存在JIN调用)在内的所有线程状态。

  • l 该参数为最常用的参数,指示jstack命令打印线程栈的长列表,包括每一个线程中持有的、等待的锁状态。

1.1.2、jstack命令示例

以下是一个简单的jstack命令效果:

# jstack -l 11890
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode):

"Attach Listener" #37 daemon prio=9 os_prio=0 tid=0x00007f7dbc001000 nid=0x2a44 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"DestroyJavaVM" #36 prio=5 os_prio=0 tid=0x00007f7dfc008800 nid=0x2e73 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"http-nio-5700-AsyncTimeout" #34 daemon prio=5 os_prio=0 tid=0x00007f7d98af8000 nid=0x2eab waiting on condition [0x00007f7ddaff2000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at org.apache.coyote.AbstractProtocol$AsyncTimeout.run(AbstractProtocol.java:1200)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"http-nio-5700-Acceptor-0" #33 daemon prio=5 os_prio=0 tid=0x00007f7d98af6800 nid=0x2eaa runnable [0x00007f7ddb0f3000]
   java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
        at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
        at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
        - locked <0x00000007853375c0> (a java.lang.Object)
        at org.apache.tomcat.util.net.NioEndpoint$Acceptor.run(NioEndpoint.java:453)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"http-nio-5700-ClientPoller-0" #31 daemon prio=5 os_prio=0 tid=0x00007f7d98acc000 nid=0x2ea8 runnable [0x00007f7ddb2f5000]
   java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
        at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
        at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
        at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
        - locked <0x0000000785603e70> (a sun.nio.ch.Util$3)
        - locked <0x0000000785603e60> (a java.util.Collections$UnmodifiableSet)
        - locked <0x0000000785603d48> (a sun.nio.ch.EPollSelectorImpl)
        at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
        at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:785)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"http-nio-5700-exec-10" #30 daemon prio=5 os_prio=0 tid=0x00007f7d98a99000 nid=0x2ea7 waiting on condition [0x00007f7ddb3f6000]
   java.lang.Thread.State: WAITING (parking)
        at sun.misc.Unsafe.park(Native Method)
        - parking to wait for  <0x00000007853d5748> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
        at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
        at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
        at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
        at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:103)
        at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:31)
        at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1067)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

………… 为节约篇幅省略了一些分析信息

"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007f7dfc1a8800 nid=0x2e7f runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007f7dfc183800 nid=0x2e7e waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007f7dfc181800 nid=0x2e7d waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007f7dfc17e800 nid=0x2e7c waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007f7dfc17d000 nid=0x2e7b runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

   Locked ownable synchronizers:
        - None

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f7dfc14a000 nid=0x2e7a in Object.wait() [0x00007f7de9add000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000006c7c1da10> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
        - locked <0x00000006c7c1da10> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

   Locked ownable synchronizers:
        - None

"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007f7dfc145800 nid=0x2e79 in Object.wait() [0x00007f7de9bde000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000006c7c1dbc8> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:502)
        at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
        - locked <0x00000006c7c1dbc8> (a java.lang.ref.Reference$Lock)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

   Locked ownable synchronizers:
        - None

"VM Thread" os_prio=0 tid=0x00007f7dfc13d800 nid=0x2e78 runnable

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f7dfc01d800 nid=0x2e74 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f7dfc01f800 nid=0x2e75 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f7dfc021800 nid=0x2e76 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f7dfc023000 nid=0x2e77 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f7dfc1ab800 nid=0x2e80 waiting on condition

JNI global references: 314

以上的示例信息中,包括了典型的用户线程、JVM内部工作线程和GC线程。

  • 典型的用户线程
"NioBlockingSelector.BlockPoller-1" #20 daemon prio=5 os_prio=0 tid=0x00007f7d98a05000 nid=0x2e9d runnable [0x00007f7de04a6000]
   java.lang.Thread.State: RUNNABLE
        at sun.nio.ch.EPollArrayWrapper.epollWait(Native Method)
        at sun.nio.ch.EPollArrayWrapper.poll(EPollArrayWrapper.java:269)
        at sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:93)
        at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:86)
        - locked <0x0000000785338d58> (a sun.nio.ch.Util$3)
        - locked <0x0000000785338cd0> (a java.util.Collections$UnmodifiableSet)
        - locked <0x0000000785338908> (a sun.nio.ch.EPollSelectorImpl)
        at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:97)
        at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:339)

   Locked ownable synchronizers:
        - None

  • 典型的HotSpot VM 线程
"VM Thread" os_prio=0 tid=0x00007f7dfc13d800 nid=0x2e78 runnable

"VM Periodic Task Thread" os_prio=0 tid=0x00007f7dfc1ab800 nid=0x2e80 waiting on condition
  • 典型的HotSpot GC 线程
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f7dfc01d800 nid=0x2e74 runnable

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f7dfc01f800 nid=0x2e75 runnable

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f7dfc021800 nid=0x2e76 runnable

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f7dfc023000 nid=0x2e77 runnable

(至少能看出JVM 堆内存区域使用的GC回收器是ParallelGC)

==============================
后文我们将结合java代码中调用的wait、sleep、notify、lock等代码,结合jstack命令讲解其在线程工作效果。

posted @ 2018-11-25 18:52  点点爱梦  阅读(359)  评论(0编辑  收藏  举报