jstack (Stack Trace for Java ) 用户生成当前时刻的虚拟机线程快照。往往在排查 Java 进程 CPU 飙高,线程卡死,线程数升高,线程运行卡顿,线程死锁等问题,非常有效。(曾经解决过一个线程数量飙升的问题,就是利用这个工具,查处了有许多相同的线程)。
1.2.1. 命令格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
[root@test ~]# 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
[root@test ~]# jstack -l 30482
1.2.2. 常用参数
选项
作用
-F
当正常输出的请求不被响应时,强制输出线程堆栈
-l
除堆栈外,显示关于锁的附加信息
-m
如果调用到本地方法的法(JNI), 可以显示 C/C++ 的堆栈
1.2.3. 获取虚拟机所有线程信息
JDK 1.5 中,java.lang.Thread.getAllStackTraces() 方法可以获取虚拟机栈中所有线程的 StackTraceElement 对象。使用这个方法可以通过几行简单的代码完成 jstack 大部分的功能。所以,可以做一个 html 页面,随时获取虚拟机堆栈的线程信息。
[root@test ~]# jinfo -help Usage: jinfo [option] <pid> (to connect to running process) jinfo [option] <executable <core> (to connect to a core file) jinfo [option] [server_id@]<remote server IP or hostname> (to connect to remote debug server)
where <option> is one of: -flag <name> to print the value of the named VM flag -flag [+|-]<name> to enable or disable the named VM flag -flag <name>=<value> to set the named VM flag to the given value -flags to print VM flags -sysprops to print Java system properties <no option> to print both of the above -h | -help to print this help message
[root@test ~]# jinfo -flags 30482 Attaching to process ID 30482, please wait... Debugger attached successfully. Server compiler detected. JVM version is 25.222-b10 Non-default VM flags: -XX:CICompilerCount=12 -XX:InitialHeapSize=264241152 -XX:MaxHeapSize=4204789760 -XX:MaxNewSize=1401421824 -XX:MinHeapDeltaBytes=524288 -XX:NewSize=88080384 -XX:OldSize=176160768 -XX:+UseCompressedClassPointers -XX:+UseCompressedOops -XX:+UseParallelGC Command line:
Definitions: <option> An option reported by the -options option <vmid> Virtual Machine Identifier. A vmid takes the following form: <lvmid>[@<hostname>[:<port>]] Where <lvmid> is the local vm identifier for the target Java virtual machine, typically a process id; <hostname> is the name of the host running the target Java virtual machine; and <port> is the port number for the rmiregistry on the target host. See the jvmstat documentation for a more complete description of the Virtual Machine Identifier. <lines> Number of samples between header lines. <interval> Sampling interval. The following forms are allowed: <n>["ms"|"s"] Where <n> is an integer and the suffix specifies the units as milliseconds("ms") or seconds("s"). The default units are "ms". <count> Number of samples to take before terminating. -J<flag> Pass <flag> directly to the runtime system. # 查看 options 可选参数列表 » jstat -options -class -compiler -gc -gccapacity -gccause -gcmetacapacity -gcnew -gcnewcapacity -gcold -gcoldcapacity -gcutil -printcompilation
Linux 系统下可以通过 kill -3 命令发送给 Java 进程退出信号“吓唬”一下虚拟机,也能拿到 dump 文件。
生成的 dump 文件目录在:
When ‘kill -3’ option is used thread dump is sent to the standard error stream. If you are running your application in tomcat, thread dump will be sent into /logs/catalina.out file.
» jmap -help Usage: jmap [option] <pid> (to connect to running process) jmap [option] <executable <core> (to connect to a core file) jmap [option] [server_id@]<remote server IP or hostname> (to connect to remote debug server)
where <option> is one of: <none> to print same info as Solaris pmap -heap to print java heap summary -histo[:live] to print histogram of java object heap; if the "live" suboption is specified, only count live objects -clstats to print class loader statistics -finalizerinfo to print information on objects awaiting finalization -dump:<dump-options> to dump java heap in hprof binary format dump-options: live dump only live objects; if not specified, all objects in the heap are dumped. format=b binary format file=<file> dump heap to <file> Example: jmap -dump:live,format=b,file=heap.bin <pid> -F force. Use with -dump:<dump-options> <pid> or -histo to force a heap dump or histogram when <pid> does not respond. The "live" suboption is not supported in this mode. -h | -help to print this help message -J<flag> to pass <flag> directly to the runtime system # 例如 » jmap -dump:format=b,file='/Users/MoMo/Downloads/test.dump' 90965 Dumping heap to /Users/MoMo/Downloads/test.dump ... Heap dump file created
1.6.2. 常用参数
选项
作用
-dump
生成 Java 堆转储快。格式为:-dump:[live,]format=b,file=,其中 live 子参数说明是否只 dump 存活的对象