Jvisualvm使用介绍

Jvisualvm使用JMX和jstatd远程调试


转自:
http://blog.csdn.net/t227hou/article/details/39996237
http://blog.csdn.net/YQWCS2012/article/details/20137845

一.Java VisualVM 概述

 对于使用命令行远程监控jvm 太麻烦  jdk1.6 中 Oracle 提供了一个新的可视化的。 JVM 监控工具 Java VisualVM jvisualvm.exe JDK 的 bin 目录下。

 

   双击启动 Java VisualVM 后可以看到窗口左侧 “应用程序 ”栏中有“ 本地 ”、“远程  、“快照 ”三个项目。

 

  “本地 ”下显示的是在 localhost 运行的 Java 程序的资源占用情况,如果本地有 Java 程序在运行的话启动 Java VisualVM 即可看到相应的程序名,点击程序名打开相应的资源监控菜单,以图形的形式列出程序所占用的 CPU 、 Heap 、 PermGen 、类、线程的 统计信息。


  “远程” 项下列出的远程主机上的 Java 程序的资源占用情况,但需要在远程主机上运行 jstatd 守护程序

 


VisualVM分为 3 类, 本地 它会自动侦测到,并显示出来

双击Local 下的任一节点,看到右边的变化 ,你可以监控 CPU ,内存,类,线程等运行状况,实时监控服务器性能。

 


 右键 VisualVM我们可以看到 Thread Dump, Heap Dump

 做 Thread Dump 很快,马上就可以看到结果

 

 

Heap Dump要稍花费一些时间(可以看到当前 heap 里对象的数量及占用的比例,做 OOM 很好用)

 

 

 

对其功能不再做描述,可以查阅网上相关质量,我们主要讲的是如何使用 VisualVM 远程监控。


二.使用visualvm 远程监控 JVM --使用JMX的配置方式:
tomcat 的 catalina.sh/bat 中添加如下参数---普通JAVA工程启动批处理配置也类似
Java代码 JAVA_OPTS='-Dcom.sun.management.jmxremote.port=8099 -

Dcom.sun.management.jmxremote.ssl=false -

Dcom.sun.management.jmxremote.authenticate=false -

Djava.rmi.server.hostname=192.168.6.104'
注:
1. -Dcom.sun.management.jmxremote.port这个是配置远程 connection 的端口号的,要确定

这个端口没有被占用

2. -Dcom.sun.management.jmxremote.ssl=false 指定了 JMX 是否启用 ssl

3. -Dcom.sun.management.jmxremote.authenticate=false   指定了JMX 是否启用鉴权(需要

用户名,密码鉴权)

4. -Djava.rmi.server.hostname 这个是配置 server 的 IP

2,3两个是固定配置,是 JMX 的远程服务权限的

配置好后,重启tomcat即可,然后接下来的操作则是jvisualvm的操作了,如下图所示:

 




右键选择“添加JMX连接”菜单,即可弹出如下窗口,在连接这个框里填上catalina.sh文件中所填的ip地址以及端口号,请注意一定要保持一致。


填好后,即会如下图所示,双击192.168.6.104:8099即可看到监测的统计图:




 

三.使用visualvm 远程监控 JVM --通过jstatd远程连接

找到需要远程连接的Linux服务器的jdk的bin目录,在其下面建立一个指定安全策略的文件

jstatd.policy(ps:文件名可随意取,只是需要与下面的执行脚本相对应),
文件内容如下:
grant codebase "file:${java.home}/../lib/tools.jar" { 
   permission java.security.AllPermission; 
}; 

注:
-J-Djava.security.policy=jstatd.policy        
该处jstatd.policy对应上面说的建立安全策略文件名

-J-Djava.rmi.server.hostname=192.168.6.104   
该处ip地址对应Linux服务器的IP地址
然后在jdk的bin目录下执行下面的脚本:
jstatd -J-Djava.security.policy=jstatd.policy -J-Djava.rmi.server.hostname=192.168.6.104 

jstatd -J-Djava.security.policy=jstatd.policy -J-Djava.rmi.server.hostname=192.168.6.104启

动完成后,即可以在客户端的Jvisualvm中建立连接







点击确定后即可。

当使用远程连接时,是看不到Linux服务器上tomcat的内存对象开销的,所以如果想监控Java程序中对象的内存开销,则还是需要在Linux的图形界面上运行JVisualvm来监控。
 

VisualVM多合一故障处理工具使用过程中遇到的问题


下载地址:http://visualvm.java.net/download.html
在Profiler页签中使用CPU和内存时,---JDK1.5之后在Client模式下的虚拟机加入了

一.自动启动了类共享---可能与Profiler功能冲突,故被监视的程序运行时使用-Xshare:off参数关闭此共享优化.

二.在windows下由于以下原因会导致 Local Applications Cannot Be Detected (Error Dialog On Startup):

Local Applications Cannot Be Detected (Error Dialog On Startup)
Description: An error dialog saying that local applications cannot be detected is shown immediately after VisualVM startup.

Resolution:  This can happen on Windows systems, it's caused by misconfigured jvmstat technology used to detect the running applications. There are two possible causes:

Insufficient permissions for %TMP%\hsperfdata_username folder - make sure you're able to create a file in the directory and eventually update the permissions for full folder access. Alternatively you can just re-create the folder which should automatically set the correct access rights. See see this forums.sun.com thread for more details.
Having %TMP%\hsperfdata_username folder on a FAT disk - by default jvmstat doesn't work on FAT disks due to security restrictions. You can bypass the security check by setting the -XX:+PerfBypassFileSystemCheck flag for both VisualVM and the monitored application. See the JDK bug #5042659 for more details.


1. Java程序缺少读写Windows的临时目录的权限
2. Windows的临时目录是放在非NTFS分区(确切地说,是不支持ACL的分区)
所以我们必须保证以下两点
1. 重新授权,保证对Windows的临时目录有足够的权限
2. 运行需要监控的Java程序时加上参数-XX:+PerfBypassFileSystemCheck

 

三 visualvm.exe启动出现如下错误(http://visualvm.java.net/troubleshooting.html#jpswin2)
Local Applications Cannot Be Monitored (Error Dialog On Startup)

Local Applications Cannot Be Monitored (Error Dialog On Startup)
Description: An error dialog saying that local applications cannot be monitored is shown immediately after VisualVM startup. Locally running Java applications are displayed as <Unknown Application> (pid ###).

Resolution:  This can happen on Windows systems if the username contains capitalized letters. In this case, username isUserName but the jvmstat directory created by JDK is%TMP%\hsperfdata_username. To workaround the problem, exit all Java applications, delete the%TMP%\hsperfdata_username directory and create new%TMP%\hsperfdata_UserName directory.

--表示jvmstat创建一个临时目录,但在操作系统中出了一个与它一样的目录(其中有大写字母),故需要删除有大写的目录.
C:\Documents and Settings\登陆用户名\Local Settings\Temp\目录下找到hsperfdata_登陆用户名的目录(此目录中有大写字母)删除.

四 eclipse远程调试出现Failed to connect to remote VM. Connection timed out.

 

 


这个是由于 调试的超时时间设置的比较短引起的,把下面的超时时间设置长一点就可以了



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值