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

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

my.policy文件需要自己建立,内如如下:

grant codebase "file:${java.home}/../lib/tools.jar" {
permission java.security.AllPermission;
};

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

[b][size=x-large]二、jps[/size][/b]
列出所有的jvm实例
实例:
jps
列出本机所有的jvm实例

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

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

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

[b][size=x-large]三、jconsole[/size][/b]

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

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

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

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

[b]在cmd中输入命令:jconsole 3980[java进程号] 弹出下图[/b]
[table]
|[img]http://dl.iteye.com/upload/attachment/0063/6682/f1b36d51-c8c0-3597-bb19-4f943e5d6bf7-thumb.jpg[/img]|[img]http://dl.iteye.com/upload/attachment/0063/6691/ecf64541-dba0-31d7-bddf-3e716efbad16-thumb.jpg[/img]|[img]http://dl.iteye.com/upload/attachment/0063/6693/a334392f-0260-31b7-aed5-586fa412a3c3-thumb.jpg[/img]
|[img]http://dl.iteye.com/upload/attachment/0063/6697/ebf90f41-74bb-3d3b-9734-8924c0f59edc-thumb.jpg[/img]|[img]http://dl.iteye.com/upload/attachment/0063/6699/de283394-44c2-32d2-8059-d2e894a5fed4-thumb.jpg[/img]
[/table]

[b][size=x-large]四、jinfo[/size][/b]
[color=red][b]用法:[/b]
jinfo [ option ] pid
jinfo [ option ] executable core
jinfo [ option ] [server-id@]remote-hostname-or-IP

[b]参数:[/b]

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

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

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

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

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

[b]具体用法[/b]
jstack -l 进程ID


[b][size=x-large]六、jmap(linux下特有,也是很常用的一个命令)[/size][/b]
观察运行中的jvm物理内存的占用情况。
[color=red]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[/color]
[color=red][b]参数如下:[/b]
-heap:打印jvm heap的情况
-histo:打印jvm heap的直方图。其输出信息包括类名,对象数量,对象占用大小。
-histo:live :同上,但是只答应存活对象的情况
-permstat:打印permanent generation heap情况[/color]

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

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

[b]jmap -histo java进程id[/b]
可以查看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插件([url]http://www.eclipse.org/mat/[/url]),然后在eclipse中,file---->open,打开这个文件heap.bin,利用现成的OOM工具进行分析。具体操作方法:首先输入网址http://www.eclipse.org/mat/previousReleases.php,然后查看你自己的Eclipse版本,我的是Indigo的,所以点击链接“Previous Releases”,选择Indigo版本的URL[url]http://download.eclipse.org/mat/1.1.0/update-site/[/url]

[b][size=x-large]七、jstat[/size][/b]
最后要重点介绍下这个命令,这是jdk命令中比较重要,也是相当实用的一个命令,可以观察到classloader,compiler,gc相关信息。
[color=red][b]具体参数如下:[/b]
-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:不知道干什么的,一直没用过。[/color]

语法结构:

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 — 从应用程序启动到采样时用于垃圾回收的总时间(单位秒)

[b]一般比较常用的几个参数是:[/b]

jstat -class 27629 3000 10 //每隔1秒监控一次,一共做10次

[b]输出内容含义如下:[/b]
[table]
|Loaded|Bytes|Unloaded|Bytes|Time
|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
[/table]

[b]jstat -gc 2083 2000 20[/b](每隔2秒监控一次,共做10)
[b]输出内容含义如下:[/b]
[table]
|S0C|Current survivor(存活的) space 0 capacity (KB).
|EC|Current eden space capacity (KB).
|EU|Eden space utilization (KB).
|OC|Current old space capacity (KB).
|OU|Old space utilization (KB).
|PC|Current permanent space capacity (KB).
|PU|Permanent space utilization (KB).
|YGC|Number of young generation GC Events.
|YGCT|Young generation garbage collection time.
|FGC|Number of full GC events.
|FGCT|Full garbage collection time.
|GCT|Total garbage collection time.
[/table]
监控内存使用情况 参数 (查看内存溢出相对有用)

[b]jstat -gccause 2083 5000 [/b](每隔5秒监控一次)
[b]输出内容含义如下:[/b]
[table]
|S0|Survivor space 0 utilization as a percentage of the space's current capacity.
|S1|Survivor space 1 utilization as a percentage of the space's current capacity.
|E|Eden space utilization as a percentage of the space's current capacity.
|O|Old space utilization as a percentage of the space's current capacity.
|P|Permanent space utilization as a percentage of the space's current capacity.
|YGC|Number of young generation GC events.
|YGCT|Young generation garbage collection time.
|FGC|Number of full GC events.
|FGCT|Full garbage collection time.
|GCT|Total garbage collection time.
|LGCC|Cause of last Garbage Collection.
|GCC|Cause of current Garbage Collection.
[/table]

[b]jstat -gcutil 25444 [/b]

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


[b]jstat -gcutil 25444 1000 5 [/b] (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

[b][size=x-large]八、jvisualvm[/size][/b]
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就可以启动该工具了。 或者我们直接双击点击该软件
[b]在cmd中输入命令:jvisualvm 弹出下图[/b]
[img]http://dl.iteye.com/upload/attachment/0065/7766/95d07567-2a39-354e-b754-d49810506a8a.jpg[/img]

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

[b]下面我们来讲解如何利用visualvm对远程的主机进行监控[/b]
首先,我们可以在用户目录下的任何地方(比如说:用户根目录,或者是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

得到如下结果:
[list]
[*]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
[/list]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值