`

Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm

 
阅读更多
在开始介绍之前,先介绍几篇写的比较详细的博客,咱们不求最精,一定最全,最省事。
http://blog.csdn.net/fenglibing/article/details/6411924
一、jstatd
启动jvm监控服务。它是一个基于rmi的应用,向远程机器提供本机jvm应用程序的信息。默认端口1099。
实例:jstatd -J-Djava.security.policy=my.policy

my.policy文件需要自己建立,内如如下:
grant codebase "file:${java.home}/../lib/tools.jar" {
 permission java.security.AllPermission;
};

这是安全策略文件,因为jdk对jvm做了jaas的安全检测,所以我们必须设置一些策略,使得jstatd被允许作网络操作

二、jps
列出所有的jvm实例
实例:
jps
列出本机所有的jvm实例

jps 192.168.0.77
列出远程服务器192.168.0.77机器所有的jvm实例,采用rmi协议,默认连接端口为1099
(前提是远程服务器提供jstatd服务)

输出内容如下:
jones@jones:~/data/ebook/java/j2se/jdk_gc$ jps
6286 Jps
6174  Jstat

详细请看连接:http://www.blogjava.net/aoxj/archive/2007/12/29/171447.html

三、jconsole

用法: jconsole [ -interval=n ] [ -notile ] [ -pluginpath  ] [ -version ] [ connection ...]

  -interval   将更新间隔时间设置为 n 秒(默认值为 4 秒)
  -notile     最初不平铺显示窗口(对于两个或更多连接)
  -pluginpath 指定 jconsole 用于查找插件的路径
  -version    输出程序版本

  connection = pid || host:port || JMX URL (service:jmx:://...)

  pid       目标进程的进程 ID
  host      远程主机名或 IP 地址
  port      用于远程连接的端口号

  -J          对正在运行 jconsole 的 Java 虚拟机指定输入参数


在cmd中输入命令:jconsole 3980[java进程号] 弹出下图


四、jinfo
用法:
jinfo [ option ] pid
jinfo [ option ] executable core
jinfo [ option ] [server-id@]remote-hostname-or-IP

参数:

pid   进程号
executable   产生core dump的java executable
core   core file
remote-hostname-or-IP  主机名或ip
server-id    远程主机上的debug server的唯一id

选项:
no option  打印命令行参数和系统属性
-flags  打印命令行参数
-sysprops  打印系统属性
-h  帮助


观察运行中的java程序的运行环境参数:参数包括Java System属性和JVM命令行参数
实例:
jinfo 2083
其中2083就是java进程id号,可以用jps得到这个id号。我在windows上尝试输入这个命令,但是不管用,于是我输入了下面这个命令:
jinfo -flag MaxPermSize 3980
显示如下:
-XX:MaxPermSize=67108864

五、jstack
该命令应该如何使用呢?首先需要用jstack命令产生java进程的dump文件,然后分析dump文件中的数据,下面的连接是一篇讲述如何分析jstack产生的dump文件数据的文章,写的非常不错:
http://www.blogjava.net/jzone/articles/303979.html

命令格式:
jstack [ option ] pid
基本参数:
-F当’jstack [-l] pid’没有相应的时候强制打印栈信息
-l长列表. 打印关于锁的附加信息,例如属于java.util.concurrent的ownable synchronizers列表.
-m打印java和native c/c++框架的所有栈信息.
-h | -help打印帮助信息
pid 需要被打印配置信息的java进程id,可以用jps查询.

具体用法
jstack -l 进程ID


六、jmap(linux下特有,也是很常用的一个命令)
观察运行中的jvm物理内存的占用情况。
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
    -permstat            to print permanent generation 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

参数如下:
-heap:打印jvm heap的情况
-histo:打印jvm heap的直方图。其输出信息包括类名,对象数量,对象占用大小。
-histo:live :同上,但是只答应存活对象的情况
-permstat:打印permanent generation heap情况


命令使用:
jmap -heap 2083
可以观察到New Generation(Eden Space,From Space,To Space),tenured generation,Perm Generation的内存使用情况

jmap -histo 2083 | jmap -histo:live 2083
可以观察heap中所有对象的情况(heap中所有生存的对象的情况)。包括对象数量和所占空间大小。

jmap -histo java进程id
可以查看java进程中的所有实例、实例的个数,可用于查询单例对象是否真的只生成了一个实例。

在控制台,输入命令“jmap -histo 7329 > histo_dump”,得到如下结果:
 num     #instances         #bytes  class name
----------------------------------------------
   1:        605348       68849960  [C
   2:       1609734       51511488  java.util.concurrent.ConcurrentHashMap$Segment
   3:       1610022       38640528  java.util.concurrent.locks.ReentrantLock$NonfairSync
   4:         70784       31478168  [I
   5:        218224       27628072  <constMethodKlass>
   6:       1609734       26423552  [Ljava.util.concurrent.ConcurrentHashMap$HashEntry;
   7:         40379       24430792  [B
   8:        218224       19211544  <methodKlass>
   9:        602848       14468352  java.lang.String
  10:         19374       11640184  <constantPoolKlass>
  11:        236950       11451216  <symbolKlass>
  12:        283746       11349840  java.util.concurrent.ConcurrentHashMap$ValueIterator
  13:         19374        8826272  <instanceKlassKlass>
  14:        100613        8048728  [Ljava.util.concurrent.ConcurrentHashMap$Segment;
  15:         85036        7332664  [Ljava.lang.Object;
  16:         15559        6614824  <constantPoolCacheKlass>
  17:         78919        6313520  java.lang.reflect.Method
  18:        103377        4962096  com.sun.tools.javac.zip.ZipFileIndexEntry
  19:         51998        4324096  [Ljava.util.HashMap$Entry;
  20:        100613        4024520  java.util.concurrent.ConcurrentHashMap
  21:        157136        3771264  java.util.concurrent.ConcurrentHashMap$HashEntry
  22:         35932        3736928  com.asiainfo.dbm.core.bean.monitor.FlowService
  23:         35932        3736928  com.asiainfo.dbm.core.bean.monitor.FlowService


该命令通常用来分析内存泄漏OOM,通常做法是,首先配置JVM启动参数,让JVM在遇到OutOfMemoryError时自动生成Dump文件:
-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/path

然后使用命令:
jmap  -dump:format=b,file=/path/heap.bin 进程ID   

如果只dump heap中的存活对象,则加上选项-live,如下:
jmap  -dump:live,format=b,file=/path/heap.bin 进程ID

最后在eclipse中安装MAT插件(http://www.eclipse.org/mat/),然后在eclipse中,file---->open,打开这个文件heap.bin,利用现成的OOM工具进行分析。具体操作方法:首先输入网址http://www.eclipse.org/mat/previousReleases.php,然后查看你自己的Eclipse版本,我的是Indigo的,所以点击链接“Previous Releases”,选择Indigo版本的URLhttp://download.eclipse.org/mat/1.1.0/update-site/

七、jstat
最后要重点介绍下这个命令,这是jdk命令中比较重要,也是相当实用的一个命令,可以观察到classloader,compiler,gc相关信息。
具体参数如下:
-class:统计class loader行为信息
-compile:统计编译行为信息
-gc:统计jdk gc时heap信息
-gccapacity:统计不同的generations(不知道怎么翻译好,包括新生区,老年区,permanent区)相应的heap容量情况
-gccause:统计gc的情况,(同-gcutil)和引起gc的事件
-gcnew:统计gc时,新生代的情况
-gcnewcapacity:统计gc时,新生代heap容量
-gcold:统计gc时,老年区的情况
-gcoldcapacity:统计gc时,老年区heap容量
-gcpermcapacity:统计gc时,permanent区heap容量
-gcutil:统计gc时,heap情况
-printcompilation:不知道干什么的,一直没用过。


语法结构: 
 
Usage: jstat -help|-options 
 
       jstat -<option> [-t] [-h<lines>] <vmid> [<interval> [<count>]] 
 
参数解释: 
 
Options — 选项,我们一般使用 -gcutil 查看gc情况 
 
vmid    — VM的进程号,即当前运行的java进程号 
 
interval– 间隔时间,单位为秒或者毫秒 
 
count   — 打印次数,如果缺省则打印无数次 
 
S0  — Heap上的 Survivor space 0 区已使用空间的百分比 
S1  — Heap上的 Survivor space 1 区已使用空间的百分比 
E   — Heap上的 Eden space 区已使用空间的百分比 
O   — Heap上的 Old space 区已使用空间的百分比 
P   — Perm space 区已使用空间的百分比 
YGC — 从应用程序启动到采样时发生 Young GC 的次数 
YGCT– 从应用程序启动到采样时 Young GC 所用的时间(单位秒) 
FGC — 从应用程序启动到采样时发生 Full GC 的次数 
FGCT– 从应用程序启动到采样时 Full GC 所用的时间(单位秒) 
GCT — 从应用程序启动到采样时用于垃圾回收的总时间(单位秒)

一般比较常用的几个参数是:
jstat -class 27629 3000 10 //每隔1秒监控一次,一共做10次

输出内容含义如下:
LoadedBytesUnloadedBytesTime
49955 49701.5    35528 32456.1      34.71
49955 49701.5    35528 32456.1      34.71
49955 49701.5    35528 32456.1      34.71
49955 49701.5    35528 32456.1      34.71
49963 49708.8    35528 32456.1      34.71
49963 49708.8    35528 32456.1      34.71
49963 49708.8    35528 32456.1      34.71
49971 49716.1    35528 32456.1      34.71
49971 49716.1    35528 32456.1      34.71
49971 49716.1    35528 32456.1      34.71


jstat -gc 2083 2000 20(每隔2秒监控一次,共做10)
输出内容含义如下:
S0CCurrent survivor(存活的) space 0 capacity (KB).
ECCurrent eden space capacity (KB).
EUEden space utilization (KB).
OCCurrent old space capacity (KB).
OUOld space utilization (KB).
PCCurrent permanent space capacity (KB).
PUPermanent space utilization (KB).
YGCNumber of young generation GC Events.
YGCTYoung generation garbage collection time.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.

监控内存使用情况 参数 (查看内存溢出相对有用)

jstat -gccause 2083 5000 (每隔5秒监控一次)
输出内容含义如下:
S0Survivor space 0 utilization as a percentage of the space's current capacity.
S1Survivor space 1 utilization as a percentage of the space's current capacity.
EEden space utilization as a percentage of the space's current capacity.
OOld space utilization as a percentage of the space's current capacity.
PPermanent space utilization as a percentage of the space's current capacity.
YGCNumber of young generation GC events.
YGCTYoung generation garbage collection time.
FGCNumber of full GC events.
FGCTFull garbage collection time.
GCTTotal garbage collection time.
LGCCCause of last Garbage Collection.
GCCCause of current Garbage Collection.

 
jstat -gcutil 25444 
 
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT 
 
11.63   0.00   56.46  66.92  98.49 162    0.248    6      0.331    0.579 

 
jstat -gcutil 25444 1000 5 (25444是java的进程号,ps -ef | grep java) 
 
  S0     S1     E      O      P     YGC     YGCT    FGC    FGCT     GCT 
 
73.54   0.00  99.04  67.52  98.49    166    0.252     6    0.331    0.583 
 
73.54   0.00  99.04  67.52  98.49    166    0.252     6    0.331    0.583 
 
73.54   0.00  99.04  67.52  98.49    166    0.252     6    0.331    0.583 
 
73.54   0.00  99.04  67.52  98.49    166    0.252     6    0.331    0.583 
 
73.54   0.00  99.04  67.52  98.49    166    0.252     6    0.331    0.583

八、jvisualvm
java visualvm 是visualvm的一个针对java vm的发布版本。 关于visualvm的更多信息可以访问 visualvm.java.net.jvisualvm 工具从jdk 6 update7 (apple的java 是从 mac os x 10.5 update 4)之后,默认就已经存在jdk工具里。
java visualvm是一个稳定的工具,用每一个jdk发布版本测试过。 最新的jdk请到oracle(sun)公司的网上进行下载。

jvisualvm 的功能及ui比jconsole还要强大。我们先来看下jvisualvm的用法。 他是一个gui(图形界面)的工具,所以上手应该会很快。

官网上关于jvisualvm的用法介绍 http://docs.oracle.com/javase/6/docs/technotes/tools/share/jvisualvm.html

简单来说,我们不需要传递任何参数就可以启动jvisualvm。

我们可以把jvisualvm也放到jdk的path里, 然后加到环境的path里。 这时候我们在windows的运行或者cmd里运行
jvisualvm就可以启动该工具了。 或者我们直接双击点击该软件
在cmd中输入命令:jvisualvm 弹出下图


具体用法可以参考下面这个链接:http://www.iteye.com/topic/516447

下面我们来讲解如何利用visualvm对远程的主机进行监控
首先,我们可以在用户目录下的任何地方(比如说:用户根目录,或者是jdk根目录)新建一个名称为jstatd.all.policy的文件,文件内容如下:
grant codebase "file:${java.home}/../lib/tools.jar" {   
  permission java.security.AllPermission;   
};


新建完文件后,我们给这个文件赋予执行权限,命令如下:
chmod 755 jstatd.all.policy


然后,我们在我们运行如下命令,启动jstatd服务(jstatd服务的默认端口为1099):
jstatd -J-Djava.security.policy=/sw/bes/jstatd.all.policy

记住jstatd.all.policy文件必须为绝对路径,防止出现java.security.AccessControlException: access denied (java.util.PropertyPermission java.rmi.server.ignoreSubClasses write)错误。

最后,我们可以用jps命令查看是否启动成功:
jps -l

得到如下结果:
  • 9481 com.bes.enterprise.ee.nodeagent.NodeAgentMain
  • 7329 com.bes.enterprise.server.Entry
  • 18968 com.bes.enterprise.server.Entry
  • 15802 sun.tools.jstatd.Jstatd
  • 16075 sun.tools.jps.Jps
  • 9328 com.bes.enterprise.server.Entry
  • 大小: 115.3 KB
  • 大小: 114 KB
  • 大小: 98.9 KB
  • 大小: 98.4 KB
  • 大小: 190.5 KB
  • 大小: 55.4 KB
  • 大小: 103.5 KB
分享到:
评论
1 楼 Stark_Summer 2015-08-12  
不错,基于冯立彬的文章,然后再自己亲自尝试下,赞+,加油~

相关推荐

Global site tag (gtag.js) - Google Analytics