Java虚拟机工具之堆栈跟踪工具jstack定位死循环

jstack命令用于打印Java进程,core文件或者远程调试服务器的堆栈跟踪记录,它会打印类名,方法名,字节码索引(BCI),如果允许的话,还会打印出行号。

jstack主要用于生成线程快照,这样可以定位长时间停顿的原因,比如死锁或者死循环。

jstack的命令格式

jstack [ options ] pid

我在代码里特意写了个死循环,代码如下:

/**
 * Created by kevin on 11/4/16.
 */
public class DeadLoop {
    public static void main(String[] args){
        while (true);
    }
}

跑起来,用jps查看一下它的pid:

$ jps
8544 AppMain
8561 Launcher
8587 Jps
2815 Main

好,看到刚刚运行的代码的pid是8544,那就用jstack看一下8544的情况:

l$ jstack -l 8544
2016-11-04 16:16:51
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode):

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

   Locked ownable synchronizers:
    - None

"Monitor Ctrl-Break" #9 daemon prio=5 os_prio=0 tid=0x00007fddbc13a800 nid=0x2170 runnable [0x00007fdda80ff000]
   java.lang.Thread.State: RUNNABLE
    at java.net.PlainSocketImpl.socketAccept(Native Method)
    at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
    at java.net.ServerSocket.implAccept(ServerSocket.java:545)
    at java.net.ServerSocket.accept(ServerSocket.java:513)
    at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
    at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
    - None

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

   Locked ownable synchronizers:
    - None

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

   Locked ownable synchronizers:
    - None

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

   Locked ownable synchronizers:
    - None

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

   Locked ownable synchronizers:
    - None

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

   Locked ownable synchronizers:
    - None

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fddbc084000 nid=0x2169 in Object.wait() [0x00007fdda8a4d000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000ec188ee0> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
    - locked <0x00000000ec188ee0> (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=0x00007fddbc07f800 nid=0x2168 in Object.wait() [0x00007fdda8b4e000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000ec186b50> (a java.lang.ref.Reference$Lock)
    at java.lang.Object.wait(Object.java:502)
    at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
    - locked <0x00000000ec186b50> (a java.lang.ref.Reference$Lock)
    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

   Locked ownable synchronizers:
    - None

"main" #1 prio=5 os_prio=0 tid=0x00007fddbc00a800 nid=0x2162 runnable [0x00007fddc30a1000]
   java.lang.Thread.State: RUNNABLE
    at DeadLoop.main(DeadLoop.java:6)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

   Locked ownable synchronizers:
    - None

"VM Thread" os_prio=0 tid=0x00007fddbc078000 nid=0x2167 runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fddbc01f800 nid=0x2163 runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fddbc021000 nid=0x2164 runnable 

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007fddbc023000 nid=0x2165 runnable 

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007fddbc024800 nid=0x2166 runnable 

"VM Periodic Task Thread" os_prio=0 tid=0x00007fddbc0c3000 nid=0x216f waiting on condition 

JNI global references: 15

结果出来了,注意78,79行的输出:

java.lang.Thread.State: RUNNABLE
    at DeadLoop.main(DeadLoop.java:6)

可以看到第6行这里有死循环,然后我们对照一下自己的源码,确实在第6行。

你可能感兴趣的:(JVM)