Java线程堆栈信息查看

Java线程堆栈信息查看方法(Linux下):

第一步:在终端运行Java程序
第二步:通过命令 pidof java 找到已经启动的java进程的ID,选择需要查看的java程序的进程ID
第三步:使用命令 kill -3 <java进行的 pid> 打印出java程序的线程堆栈信息
第四步:通常情况下运行的项目可能会比较大,那么这个时候打印的堆栈信息可能会有几千到几万行,为了方便查看,我们往往需要将输出内容进行重定向
使用linux下的重定向命令方式即可:例如: demo.sh > run.log 2>&1 将输出信息重定向到 run.log中。
注:在操作系统中,0 1 2分别对应着不同的含义, 如下:
0 : 标准输入,即:C中的stdin , java中的System.in
1 : 标准输出, 即:C中的stdout ,java中的System.out
2 : 错误输出, 即:C中的stderr , java中的System.err


Demo:

----------------------------------------------------------------------------------------------
Sources Code :
public class PrintThreadTrace {


    Object obj1 = new Object();
    Object obj2 = new Object();


    public void func1(){
        synchronized (obj1){
            func2();
        }
    }


    public void func2(){
        synchronized (obj2){
            while(true){
                System.out.print("");
            }
        }
    }


    public static void main(String[] args){
        PrintThreadTrace ptt = new PrintThreadTrace();
        ptt.func1();
    }
}


----------------------------------------------------------------------------------------------------------------
按照步骤操作后的打印输出信息:


Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.79-b02 mixed mode):


"Service Thread" daemon prio=10 tid=0x00007fdc880a9000 nid=0x12a4 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"C2 CompilerThread1" daemon prio=10 tid=0x00007fdc880a7000 nid=0x12a3 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"C2 CompilerThread0" daemon prio=10 tid=0x00007fdc880a4000 nid=0x12a2 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"JDWP Command Reader" daemon prio=10 tid=0x00007fdc50001000 nid=0x1299 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"JDWP Event Helper Thread" daemon prio=10 tid=0x00007fdc880a1800 nid=0x1298 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"JDWP Transport Listener: dt_socket" daemon prio=10 tid=0x00007fdc8809e000 nid=0x1297 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"Signal Dispatcher" daemon prio=10 tid=0x00007fdc88091000 nid=0x1296 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"Finalizer" daemon prio=10 tid=0x00007fdc88071800 nid=0x1295 in Object.wait() [0x00007fdc77ffe000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ecb04858> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000000ecb04858> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)


"Reference Handler" daemon prio=10 tid=0x00007fdc8806f800 nid=0x1294 in Object.wait() [0x00007fdc7c10b000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ecb04470> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:503)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
- locked <0x00000000ecb04470> (a java.lang.ref.Reference$Lock)


"main" prio=10 tid=0x00007fdc8800b800 nid=0x128e runnable [0x00007fdc8fef7000]
   java.lang.Thread.State: RUNNABLE
at com.wenchain.study.PrintThreadTrace.func2(PrintThreadTrace.java:20)
- locked <0x00000000ecc04b20> (a java.lang.Object)
at com.wenchain.study.PrintThreadTrace.func1(PrintThreadTrace.java:13)
- locked <0x00000000ecc04b10> (a java.lang.Object)
at com.wenchain.study.PrintThreadTrace.main(PrintThreadTrace.java:27)


"VM Thread" prio=10 tid=0x00007fdc8806b000 nid=0x1293 runnable 


"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007fdc88021000 nid=0x128f runnable 


"GC task thread#1 (ParallelGC)" prio=10 tid=0x00007fdc88023000 nid=0x1290 runnable 


"GC task thread#2 (ParallelGC)" prio=10 tid=0x00007fdc88024800 nid=0x1291 runnable 


"GC task thread#3 (ParallelGC)" prio=10 tid=0x00007fdc88026800 nid=0x1292 runnable 


"VM Periodic Task Thread" prio=10 tid=0x00007fdc880b3800 nid=0x12a5 waiting on condition 


JNI global references: 1391


Heap
 PSYoungGen      total 17920K, used 1270K [0x00000000ecb00000, 0x00000000ede80000, 0x0000000100000000)
  eden space 15872K, 8% used [0x00000000ecb00000,0x00000000ecc3d898,0x00000000eda80000)
  from space 2048K, 0% used [0x00000000edc80000,0x00000000edc80000,0x00000000ede80000)
  to   space 2048K, 0% used [0x00000000eda80000,0x00000000eda80000,0x00000000edc80000)
 ParOldGen       total 39424K, used 0K [0x00000000c6200000, 0x00000000c8880000, 0x00000000ecb00000)
  object space 39424K, 0% used [0x00000000c6200000,0x00000000c6200000,0x00000000c8880000)
 PSPermGen       total 21504K, used 2619K [0x00000000c1000000, 0x00000000c2500000, 0x00000000c6200000)
  object space 21504K, 12% used [0x00000000c1000000,0x00000000c128edd8,0x00000000c2500000)


----------------------------------------------------------------------------------------------------------------------------
上面的信息中包含了当前JVM中所有运行的线程信息,其中在示例中我们启动的线程为main线程,其余的都是JVM自己创建的。
在打印的信息中,我们可以清楚的看见当前线程的调用上下文,可以很清楚的知道程序的运行情况。
并且我们在最后面还能看见当前虚拟机中的内存使用情况,青年世代,老年世代的信息等等...


PS: 在JDK1.5以上,我们可以通过在Java程序中调用Thread.getStackTrace()方法来进行堆栈的自动打印,使得线程堆栈的打印时机可编程控制。

你可能感兴趣的:(堆栈信息,java堆栈,线程堆栈,java线程堆栈)