JDK命令行工具
JDK的bin目录下的命令行工具(*.exe,如java.exe、javac.exe)的功能代码主要是在lib/tools.jar类库中实现的,其实是对tools.jar的一层薄封装。
1、jps:虚拟机进程状况工具
JVM Process Status Tool可以列出正在运行的虚拟机进程,显示进程的LVMID(Local Virtual Machine Identifier,本地虚拟机ID)和主类(main()方法所在的类)的名称。
命令格式:jsp [options] [hostid]
[root@LIN-5FF84753572 10256170@zte.intra]# jps -help
usage: jps [-help]
jps [-q] [-mlvV] [<hostid>]
Definitions:
<hostid>: <hostname>[:<port>]
hostid:jps可以通过RMI协议查询开启了RMI服务的远程虚拟机进程状态,hostid为RMI注册表中注册的主机名;
options:
2、jstat:虚拟机统计信息监视工具
JVM Statistics Monitoring Tool用于监视虚拟机的各种运行状态信息,可以显示本地或远程虚拟机进程中的类装载、内存、垃圾收集、JIT编译等运行数据。
命令格式:jstat [option vmid [interval [s|ms] [count]]]
[root@LIN-5FF84753572 10256170@zte.intra]# jstat -help
Usage: jstat -help|-options
jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]]
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.
vmid:如果是本地虚拟机进程,与LVMID一致;如果是远程虚拟机进程,格式为[protocol:][//]lvmid[@hostname[:port]/servername];
interval:查询间隔,省略该参数代表查询一次;
s|ms:interval的时间单位,s为秒,ms为毫秒,默认为毫秒;
count:查询次数,如jstat -gc 2764 250 20代表每250毫秒查询一次进程2764的垃圾收集情况,一共查询20次;
option:
3、jinfo:Java配置信息工具
Configuration Info For Java可以实时查看和调整虚拟机各项参数。
命令格式:jinfo [option] pid
[root@LIN-5FF84753572 10256170@zte.intra]# 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
4、jmap:Java内存映像工具
Memory Map For Java主要用于生成堆转储快照(heapdump或dump文件),还可以查询finalize执行队列、java堆和永久代的详细信息(空间使用率、使用的收集器等)。
命令格式:jmap [option] vmid
[root@LIN-5FF84753572 10256170@zte.intra]# 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
option:
5、jhat:虚拟机堆转储快照分析工具
JVM Heap Analysis Tool与jmap工具搭配使用,分析jmap生成的堆转储快照。内置了HTTP/HTML服务器,生成的分析结果可以在浏览器中查看。一般不会使用jhat直接分析,而使用更强大更专业的Visual VM。
命令格式:
[root@LIN-5FF84753572 10256170@zte.intra]# jhat -help
Usage: jhat [-stack <bool>] [-refs <bool>] [-port <port>] [-baseline <file>] [-debug <int>] [-version] [-h|-help] <file>
-J<flag> Pass <flag> directly to the runtime system. For
example, -J-mx512m to use a maximum heap size of 512MB
-stack false: Turn off tracking object allocation call stack.
-refs false: Turn off tracking of references to objects
-port <port>: Set the port for the HTTP server. Defaults to 7000
-exclude <file>: Specify a file that lists data members that should
be excluded from the reachableFrom query.
-baseline <file>: Specify a baseline object dump. Objects in
both heap dumps with the same ID and same class will
be marked as not being "new".
-debug <int>: Set debug level.
0: No debug output
1: Debug hprof file parsing
2: Debug hprof file parsing, no server
-version Report version number
-h|-help Print this help and exit
<file> The file to read
For a dump file that contains multiple heap dumps,
you may specify which dump in the file
by appending "#<number>" to the file name, i.e. "foo.hprof#3".
All boolean options default to "true"
6、jstack:Java堆栈跟踪工具
Stack Trace For Java用于生成虚拟机当前时刻的线程快照(threaddump或javacore文件,即当前虚拟机内每条线程正在执行的方法堆栈的集合),目的是定位线程出现长时间停顿的原因(如死锁、死循环、请求外部资源导致的线程长时间等待)。
命令格式:jstack [option] vmid
[root@LIN-5FF84753572 10256170@zte.intra]# 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
option:
java.lang.Thread类的getAllStackTraces()方法可以获取虚拟机所有线程的StackTraceElement对象,可以完成jstack的大部分功能。 通过调用这个方法制作一个可以随时查看线程状况的JSP页面,如下:
<%@page import="java.util.Map"%>
<html>
<head>
<title>服务器线程信息</title>
</head>
<body>
<pre>
<%
for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()) {
Thread thread = (Thread) stackTrace.getKey();
StackTraceElement[] stack = (StackTraceElement[]) stackTrace.getValue();
if (thread.equals(Thread.currentThread())) {
continue;
}
out.print("\n线程:" + thread.getName() + "\n");
for (StackTraceElement element : stack) {
out.print("\t" + element + "\n");
}
}
%>
</pre>
</body>
</html>
7、HSDIS:JIT生成代码反汇编
在字节码层面,分析程序的执行语义(虚拟机做了什么)完全可行,但分析程序的执行行为问题(虚拟机怎样做的、性能如何)没有意义,需要通过其他方式解决。
通过软件调试工具打断点调试是分析程序如何运行最常见的手段,但在虚拟机中大量执行代码是通过JIT编译器动态生成到CodeBuffer中的,无法简单地处理这种混合模式的调试。HSDIS是虚拟机JIT编译代码的反汇编插件,通过-XX:+PrintAssembly指令调用它把动态生成的本地代码还原为汇编代码输出,根据输出的代码分析问题。
JDK可视化工具
1、JConsole:Java监视与管理控制台
Java Monitoring And Management Console是基于JMX的可视化监管工具,针对JMX MBean进行管理。
通过JDK的bin目录下的jconsole.exe启动后,将自动搜索出本机运行的所有虚拟机进程。
选择进程并点击连接后进入主界面。
概览页是整个虚拟机主要运行数据的概览,包括堆内存使用量、线程、类、CPU占用率4种信息曲线图,分别是对内存、线程、类页内容的信息汇总;内存页功能相当于可视化的jstat命令,用于监视受收集器管理的虚拟机内存的变化趋势;线程页功能相当于可视化的jstack命令,用于分析线程长时间停顿的原因;
2、VisualVM:多合一故障处理工具
All-In-One Java TroubleShooting Tool除了运行监视、故障处理外,还提供了性能分析等其他功能。由于不需要被监视的程序基于特殊Agent运行,对程序性能的影响很小,可以直接运行在生产环境中。
VisualVM基于NetBeans平台开发,具备插件扩展功能,通过插件,可以实现:
显示虚拟机进程以及进程的配置、环境信息(jps、jinfo);
监视应用程序的CPU、GC、堆、方法区、以及线程信息(jstat、jstack);
dump及分析堆转储快照(jmap、jhat);
方法级的程序运行性能分析,找出被调用最多、运行时间最长的方法;
离线程序快照,收集程序的运行时配置、线程dump、内存dump等信息建立一个快照;
其他plugins的无限可能性。
运行JDK的bin目录下的jvisualvm.exe启动VisualVM,
选择一个需要监视的程序进入程序的主界面,根据安装插件数量不同显示的页签可能不同。
生成dump文件有两种方式,一是在程序节点右键菜单中选择,而是在程序的监视页中单击"堆Dump"。
Profiler页提供了程序运行期间方法级的CPU执行时间分析及内存分析,进行性能分析对程序性能有较大的影响,一般不在生产环境中使用。单击CPU或内存按钮进行分析,VisualVM会记录应用程序从开始到这段时间中执行过的方法,CPU分析将统计每个方法的执行次数、执行耗时,内存分析将统计每个方法关联的对象以及对象所占的空间。单击停止按钮结束监控过程。
BTrace插件可以在不停止目标程序运行的前提下,通过HotSpot虚拟机的HotSwap(热代码替换技术,允许在不停止运行的情况下更新已加载的类的代码)技术动态加入原本并不存在的调试代码,如动态打印调用堆栈、参数、返回值等。