jstack命令应用

0 概述

jstack 命令用于生成虚拟机当前时刻线程快照(一般称为threaddump 或者javacore 文件)。线程快照就是当前虚拟机内每一条线程正在执行的方法堆栈集合,生成线程快照的目的主要用于定位如线程间的死锁、死循环、请求外部服务时间过长等导致机器load、cpu等过高。本文主要结合top、jstack命令来实际分析cpu过高线程。

1 jstack命令

jstack [option] pid

option作用
-F当正常输出请求不被响应时候,强制输出线程堆栈
-l显示锁相关信息
-m混合模式,如果调用到本地方法栈,会显示本地方法栈

2 实例分析

public class ThreadTest {

    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //死循环
                while (true) {

                }
            }
        });
        thread.start();

        //主线程不退出
        Thread.sleep(10000000);

    }
}

启动程序后,
1.通过jps -l 找到相应java进程,
2.然后使用top -Hp pid (可以打印出改进程中线程运行情况)从图中可以看出20018 (16进制:4e32)比较消耗cpu
这里写图片描述
3.执行jstack pid (实际工作中可能吧比较多,可以将其输出到文件中)

jstack 20003
2017-11-08 20:25:46
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.72-b15 mixed mode):

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

"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f38b00e4800 nid=0x4e32 runnable [0x00007f3892eab000]
   java.lang.Thread.State: RUNNABLE
    at ThreadTest$1.run(ThreadTest.java:7)
    at java.lang.Thread.run(Thread.java:745)

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

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

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

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

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

"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007f38b0083000 nid=0x4e2b in Object.wait() [0x00007f38935b2000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait(Native Method)
    - waiting on <0x00000000d6f88ee0> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
    - locked <0x00000000d6f88ee0> (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)

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

"main" #1 prio=5 os_prio=0 tid=0x00007f38b0009000 nid=0x4e24 waiting on condition [0x00007f38b5e12000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
    at java.lang.Thread.sleep(Native Method)
    at ThreadTest.main(ThreadTest.java:14)

"VM Thread" os_prio=0 tid=0x00007f38b0076800 nid=0x4e29 runnable 

"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007f38b001e000 nid=0x4e25 runnable 

"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007f38b001f800 nid=0x4e26 runnable 

"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007f38b0021800 nid=0x4e27 runnable 

"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007f38b0023000 nid=0x4e28 runnable 

"VM Periodic Task Thread" os_prio=0 tid=0x00007f38b00c9800 nid=0x4e31 waiting on condition 

JNI global references: 6

搜索对应线程Id(4e32),就可以找到对应代码区域

"Thread-0" #9 prio=5 os_prio=0 tid=0x00007f38b00e4800 nid=0x4e32 runnable [0x00007f3892eab000]
   java.lang.Thread.State: RUNNABLE
    at ThreadTest$1.run(ThreadTest.java:7)
    at java.lang.Thread.run(Thread.java:745)

参考文献:
[1] 深入理解java 虚拟机(第二版),周志明著

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值