jstack命令应用

来源:互联网 发布:安卓上运行windows游戏 编辑:程序博客网 时间:2024/05/17 05:04

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 200032017-11-08 20:25:46Full 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 虚拟机(第二版),周志明著

原创粉丝点击