Java 线程栈数据收集

10 篇文章 0 订阅

最近做技术支持碰到一个问题,tc server 启动快速死掉了,不知道什么出错,日志都很干净,看不出来什么异常,于是自己想写一个程序来分析当时的线程栈,写了个shell来抓:重点讲一下我的思路:
1> 用jps抓运行的Java进程。
jps是jdk提供的一个查看当前Java进程的小工具, 可以看做是JavaVirtual Machine Process Status Tool的缩写。使用官方说明:
jps General Commands Manual
Name
jps - Java Virtual Machine Process Status Tool
SYNOPSIS
jps [ options ] [ hostid ]
PARAMETERS
options
Command-line options.
hostid
The host identifier of the host for which the process report should be generated. The hostid may include optional components that indicate the communications protocol, port number, and other implementation specific
data.

重点看看它所支持的参数:
-q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
-m Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l Output the full package name for the application’s main class or the full path name to the application’s JAR file.
-v Output the arguments passed to the JVM.
-V Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags= argument).
-Joption
Pass option to the java launcher called by jps. For example, -J-Xms48m sets the startup memory to 48 megabytes. It is a common convention for -J to pass options to the underlying VM executing applications written in
Java.

HOST IDENTIFIER
The host identifier, or hostid is a string that indicates the target system. The syntax of the hostid string largely corresponds to the syntax of a URI:
[protocol:][[//]hostname][:port][/servername]
protocol
The communications protocol. the default protocol is rmi.
hostname
A hostname or IP address indicating the target host. If hostname is omitted, then the target host is the local host.
参看实例:
1)jps
bash-4.2$ jps
3064 ProviderImpl
11666 activemq.jar
10768 Locator
23951 Bootstrap
23891 WrapperStartStopApp
2)jps –l:输出主类或者jar的完全路径名
3)jps –v :输出jvm参数
4)jps –q :仅仅显示java进程号
5) jps -m:输出启动参数。

在实际使用中,如果有多个类似的实例启动,为了更好的定位,往往需要多个参数输出,然后根据输出进行筛选才找到你需要定位的Java 进程。

2> 用jstack抓运行的Java thread。
jstack General Commands Manual
Name
jstack - Stack Trace
SYNOPSIS
jstack [ option ] pid
jstack [ option ] executable core
jstack [ option ] [server-id@]remote-hostname-or-IP
PARAMETERS
Options are mutually exclusive. Option, if used, should follow immediately after the command name. See OPTIONS.
DESCRIPTION
jstack prints Java stack traces of Java threads for a given Java process or core file or a remote debug server. For each Java frame, the full class name, method name, ‘bci’ (byte code index) and line number, if available,
are printed. With the -m option, jstack prints both Java and native frames of all threads along with the ‘pc’ (program counter). For each native frame, the closest native symbol to ‘pc’, if available, is printed. C++ mangled
names are not demangled. To demangle C++ names, the output of this command may be piped to c++filt. If the given process is running on a 64-bit VM, you may need to specify the -J-d64 option, e.g.:
jstack -J-d64 -m pid
OPTIONS
-F Force a stack dump when ‘jstack [-l] pid’ does not respond.
-l Long listing. Prints additional information about locks such as list of owned java.util.concurrent ownable synchronizers @
-m prints mixed mode (both Java and native C/C++ frames) stack trace.
-h prints a help message.

jstack使用很简单,但是线程是在不断地在变化的,为了捕抓住更多的信息,一般都是要多次定时去检查才能准确的拿到更多有用的信息。
我写了一个脚本就是反复的检查进程,然后输出10次线程栈,需要的可以点击 下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值