jstack
jstack用于生成java虚拟机当前时刻的线程快照。线程快照是当前java虚拟机内每一条线程正在执行的方法堆栈的集合,生成线程快照的主要目的是定位线程出现长时间停顿的原因,如线程间死锁、死循环、请求外部资源导致的长时间等待等。 线程出现停顿的时候通过jstack来查看各个线程的调用堆栈,就可以知道没有响应的线程到底在后台做什么事情,或者等待什么资源。 如果java程序崩溃生成core文件,jstack工具可以用来获得core文件的java stack和native stack的信息,从而可以轻松地知道java程序是如何崩溃和在程序何处发生问题。另外,jstack工具还可以附属到正在运行的java程序中,看到当时运行的java程序的java stack和native stack的信息, 如果现在运行的java程序呈现hung的状态,jstack是非常有用的。【线程分析】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [root@localhost bin] # jstack -help Usage: jstack [-l] <pid> (to connect to running process) jstack -F [-m] [-l] <pid> (to connect to a hung process) jstack [-m] [-l] <executable> <core> (to connect to a core file ) jstack [-m] [-l] [server_id@]<remote server IP or hostname > (to connect to a remote debug server) Options: -F to force a thread dump. Use when jstack <pid> does not respond (process is hung) -m to print both java and native frames (mixed mode) -l long listing. Prints additional information about locks -h or -help to print this help message |
参数
-F:当正常输出请求不被响应时,强制输出线程栈堆。
-l:除线程栈堆外,显示关于锁的附加信息。
-m:如果调用本地方法的话,可以显示c/c++的栈堆
示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | [root@localhost bin] # jstack -m 24971 | more Attaching to process ID 24971, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.79-b02 Deadlock Detection: No deadlocks found. ----------------- 24972 ----------------- 0x00007fe07d2cd69d __GI___poll + 0x2d 0x00007fe070dc80a7 Java_java_net_PlainSocketImpl_socketAccept + 0x1e7 0x00007fe0745a3d98 * java.net.PlainSocketImpl.socketAccept(java.net.SocketImpl) bci:0 (Interpreted frame) 0x00007fe074597058 * java.net.AbstractPlainSocketImpl.accept(java.net.SocketImpl) bci:7 line:398 (Interpreted frame) 0x00007fe074597058 * java.net.ServerSocket.implAccept(java.net.Socket) bci:60 line:530 (Interpreted frame) 0x00007fe074597058 * java.net.ServerSocket.accept() bci:48 line:498 (Interpreted frame) 0x00007fe074597233 * org.apache.catalina.core.StandardServer.await() bci:180 line:470 (Interpreted frame) 0x00007fe074597706 * org.apache.catalina.startup.Catalina.await() bci:4 line:782 (Interpreted frame) 0x00007fe074597058 * org.apache.catalina.startup.Catalina.start() bci:209 line:728 (Interpreted frame) 0x00007fe0745914e7 <StubRoutines> 0x00007fe07c967e95 _ZN9JavaCalls11call_helperEP9JavaValueP12methodHandleP17JavaCallArgumentsP6Thread + 0x365 0x00007fe07c9668f8 _ZN9JavaCalls4callEP9JavaValue12methodHandleP17JavaCallArgumentsP6Thread + 0x28 0x00007fe07cbfdeef _ZN10Reflection6invokeE19instanceKlassHandle12methodHandle6Handleb14objArrayHandle9BasicTypeS3_bP6Thread + 0x 47f 0x00007fe07cbfeca0 _ZN10Reflection13invoke_methodEP7oopDesc6Handle14objArrayHandleP6Thread + 0x160 |