欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JVM调优前戏之JDK命令行工具---jstack

程序员文章站 2022-04-09 15:32:59
在JDK的命令行中,一般开发人员最耳熟能详的肯定就是java,javac,javap等常用命令,不过在jdk/bin下还有许多其他的命令行工具,它们被用来监测JVM运行时的状态,下面我们来详细解读一下几个常用的工具以及如何使用。 ......

在jdk的命令行中,一般开发人员最耳熟能详的肯定就是java,javac,javap等常用命令,不过在jdk/bin下还有许多其他的命令行工具,它们被用来监测jvm运行时的状态,下面我们来详细解读一下几个常用的工具以及如何使用。

jstack

jstack(jvm stack trace)用于生成虚拟机当前时刻的线程快照。线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的就是定位线程出现长时间停顿的原因,如线程死锁、死循环、请求外部资源导致的长时间等待等都是导致线程长时间停顿的常见原因。线程出现停顿的时候通过jstack来查看各个线程的调用堆栈,就可以知道没有响应的线程在后台到底做了些什么事情,或者等待着什么资源。

命令格式

jstack [option] lvmid

option参数

  • -f : 当正常输出请求不被响应时,强制输出线程堆栈
  • -l : 除堆栈外,显示关于锁的附加信息
  • -m : 如果调用到本地方法的话,可以显示c/c++的堆栈

示例

$ jstack -l 45995
2019-08-14 17:34:36
full thread dump java hotspot(tm) 64-bit server vm (25.45-b02 mixed mode):

"attach listener" #46 daemon prio=9 os_prio=31 tid=0x00007fc62527e800 nid=0x6b07 waiting on condition [0x0000000000000000]
   java.lang.thread.state: runnable

   locked ownable synchronizers:
    - none

"http-bio-8082-exec-10" #44 daemon prio=5 os_prio=31 tid=0x00007fc61f359000 nid=0x8a03 waiting on condition [0x0000700002fa3000]
   java.lang.thread.state: waiting (parking)
    at sun.misc.unsafe.park(native method)
    - parking to wait for  <0x00000006c04cdb28> (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:104)
    at org.apache.tomcat.util.threads.taskqueue.take(taskqueue.java:32)
    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:745)

   locked ownable synchronizers:
    - none

"http-bio-8082-exec-9" #43 daemon prio=5 os_prio=31 tid=0x00007fc620475000 nid=0x8803 waiting on condition [0x0000700002ea0000]
   java.lang.thread.state: waiting (parking)
    at sun.misc.unsafe.park(native method)
    - parking to wait for  <0x00000006c04cdb28> (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:104)
    at org.apache.tomcat.util.threads.taskqueue.take(taskqueue.java:32)
    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:745)

   locked ownable synchronizers:
    - none

"http-bio-8082-exec-8" #42 daemon prio=5 os_prio=31 tid=0x00007fc61f002000 nid=0x8603 waiting on condition [0x0000700002d9d000]
   java.lang.thread.state: waiting (parking)
    at sun.misc.unsafe.park(native method)
    - parking to wait for  <0x00000006c04cdb28> (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:104)
    at org.apache.tomcat.util.threads.taskqueue.take(taskqueue.java:32)
    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:745)

   locked ownable synchronizers:
    - none
    ......