Sun JDK随版本同时发布了一些性能剖析的工具,这些工具基本上都是基于JVM MangeAPI和Sun JVM Attach API实现,所以其能提供什么样的功能可以参见JVM Manage API 的说明。我们来了解一下:

    一、功能性工具
    1.jps:列出所有的JVM进程的进程ID和进程名字,实现原理可见Sun JVM Attach API
    2.jinfo:打印JVM启动内部信息,譬如启动参数、JVM版本、操作系统信息等,具体可参见JVM Manage API的OperatingSystem和Runtime这两个Bean,本功能基于JVM Manage API实现。最常用的使用格式:

jinfo pid

     jinfo支持连接到远程的JVM上(远程服务需要启动jsadebugd),实际应用意义不大
    3.jstat:一个内存和垃圾回收情况的统计工具,最常用的使用格式是:

jstat -outputOptions vmid [interval[s|ms] [count]]

    jstat支持连接到远程的JVM上(远程服务需要启动jstatd),实际应用意义不大    
    可使用的outputOptions选项包括:

 
class Statistics on the behavior of the class loader.
compiler Statistics of the behavior of the HotSpot Just-in-Time compiler.
gc Statistics of the behavior of the garbage collected heap.
gccapacity Statistics of the capacities of the generations and their corresponding spaces.
gccause Summary of garbage collection statistics (same as -gcutil), with the cause of the last and current (if applicable) garbage collection events.
gcnew Statistics of the behavior of the new generation.
gcnewcapacity Statistics of the sizes of the new generations and its corresponding spaces.
gcold Statistics of the behavior of the old and permanent generations.
gcoldcapacity Statistics of the sizes of the old generation.
gcpermcapacity Statistics of the sizes of the permanent generation.
gcutil Summary of garbage collection statistics.
printcompilation HotSpot compilation method statistics.

     更详细的说明见:http://java.sun.com/javase/6/docs/technotes/tools/share/jstat.html
     范例:

jstat -gc 23450 250 4 //23450是进程ID,250表示每250豪秒采样一次,4是总执行4次,输出结果如下:
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127
1600.0 1600.0 786.5 0.0 13184.0 11964.2 114688.0 3684.3 131072.0 13484.2 8 0.127 0 0.000 0.127

S0C|S1C/ S0U|S1U:两个Suvivor区容量/空闲的容量
EC/EU:Eden 区容量/空闲的容量
OC/OU:Tenured 区容量/空闲的容量
PC/PU:Perm区容量/空闲的容量
YGC/ YGCT:Young Gen回收次数/总回收时间
FGC/FGCT:Full GC次数/总回收时间
GCT:总的垃圾回收时间(YGCT+FGCT)

     4.jstack:打印JVM当前所有的线程、线程栈和锁信息信息,最常用使用格式如下:

jstack –l pid

    范例:

jstack -l 23450
Full thread dump Java HotSpot(TM) Server VM (10.0-b22 mixed mode):

"Attach Listener" daemon prio=10 tid=0x08058400 nid=0x3005 waiting on condition [0x00000000..0x00000000]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None

"DestroyJavaVM" prio=10 tid=0x822af400 nid=0x5b9b waiting on condition [0x00000000..0xb7dc2120]
java.lang.Thread.State: RUNNABLE

Locked ownable synchronizers:
- None
……

     jstack支持连接到远程的JVM上(远程服务需要启动jsadebugd),实际应用意义不大
     5.jmap:当前的堆内存信息dump,JDK6中支持dump到一个文件,由其他分析程序对堆内存进行分析(譬如jhat/Eclipse Memory Anaylzer)。最常用的使用格式如下:

jmap -dump:file={filename} pid

     范例:如下将堆内存dump到result.bin中

jmap -dump:file=result.bin 23450
显示如下信息:
Dumping heap to …/bin/result.bin ...
Heap dump file created

     二、辅助性工具
    辅助性工具本身没有提供什么功能,只是为功能性工具提供支持
     1. jstatd/jsadebugd:提供远程代理服务,支持jstat/jinfo/jstack通过远程获取信息,这个工具实际意义不大
     2.jhat:分析jmap dump出来的堆,以网页的形式提供访问。jhat功能基本上比较弱,非常占用内存,在堆文件比较大(譬如超过1G)的情况下基本无法使用,Eclipse Memory Anaylzer是一个优秀的替代品。使用格式如下:

jhat {dumpfile}
在堆文件比较大的情况下,需要使用其他的启动参数,譬如如下
jhat –J-mx512M {dumpfile}

    访问http://localhost:7000可以看到分析后的结果