JVisualVM and JConsole

来源:互联网 发布:淘宝商城男装夹克 编辑:程序博客网 时间:2024/05/17 00:53

From : http://tianya23.blog.51cto.com/1081650/311984

 

最近VisualVM很火,热度甚至超过了Jconsole。( 关于Jconsole的使用步骤如下:步骤1、在命令行输入:jconsole,或在java的bin下面双击‘jconsole.exe’启动
步骤2、输入需要连接的远端的ip:端口,如10.20.153.188:9999, 能连接的前提是在服务器上启动了相关进程
自己的使用情况是如下:
1、VisualVM启动
  (1)服务器端启动
   步骤1: 在Linux服务器上建立文件:jstatd.all.policy, 文件内容为:
 
grant codebase "file:${java.home}/../lib/tools.jar" {    
        permission java.security.AllPermission;    
    };    
 步骤2:在linux服务器上启动该文件
jstatd -J-Djava.security.policy=jstatd.all.policy
jstatd -J-Djava.security.policy=/home/admin/jstatd.java.policy -J-
Djava.rmi.server.logCalls=true
 
最好后台启动该服务,使用如下方式:
nohup jstatd -J-Djava.security.policy=jstatd.all.policy &
  (2)客户端启动
在安装了Java的客户端,在bin下面存在着jvisualvm.exe的程序。
 步骤1:下载和安装VisualVM软件,下载地址:https://visualvm.dev.java.net/download.html,或下载地址:https://visualvm.dev.java.net/files/documents/7163/144406/visualvm_12.zip 解压即可使用,不过windows和linux需要下载不同的版本。在我本地使用的是:visualvm_122-ml
 步骤2:在visualVM/visualvm_122-ml/visualvm_122/bin下面直接点击visualvm.exe即可启动客户端。添加远程服务器,在该服务器上点击‘添加jstatd连接’,使用默认的端口1099,点击确认之后即可和服务器建立连接。
 
 
2、原理分析
VisualVM监控远程JVM是通过RMI服务器来实现的,因此需要在被监控的远程服务器上启动RMI服务。启动的RMI服务的端口是1099,如果需要以其它端口启动RMI服务,请查看官方文档:http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jstatd.html
 
3、使用中遇到的问题
问题1:
    在服务器中启动一段时间(比如好几天)后,存在客户端访问不到服务器上相关的Java进程的问题。
    解答: 这是由于Jstatd采样数据是检测在它自身启动时间之前的进程的相关信息,之后启动的Java进程的相关信息将不再纳入到其收集的范围内,此时需要关掉服务器上的visualvm相关的进程,再启动,则可正常的在客户端使用了。
问题2:启动的时候出现:Could not bind /JStatRemoteHost to RMI Registry
问题:默认的1099端口冲突了
参考:http://www.sleberknight.com/blog/sleberkn/entry/20070425
So I changed to port 1982 because I read somewhere that JBoss binds the naming to 1099,code like this:启动方式:jstatd -J-Djava.security.policy=jstatd.all.policy -p 1982
连接的时候使用如下方式进行:ip:1982
 
 
 4、Visual VM的插件
https://visualvm.dev.java.net/plugins.html
 插件安装步骤:
To install a plugin from VisualVM Plugins Center follow these steps:
  1. In VisualVM main menu choose Tools | Plugins, Plugins dialog is opened
  2. Switch to Available Plugins tab and select the plugin you want to install
  3. Click the Install button, review and accept plugin license and wait for the plugin to be downloaded and installed

jvisualvm中的jconsole使用方法:

http://java.dzone.com/news/visual-vm-free-and-open-source

http://lixjluck.javaeye.com/blog/713782

 5、调节GC大小
Java5.0 GC的官方文档:http://www.oracle.com/technetwork/java/gc-tuning-5-138395.html
Java6 性能白皮书的官方文档: http://java.sun.com/performance/reference/whitepapers/6_performance.html
 
 
----------------------------------------------------------------------------------------------------------------------------------
二、JConsole

1、远程连接端口

远程连接相关的java进程:使用jps产看进程,使用jps pid查看port; 也可以通过netstat -anp | grep pid查看使用的端口。 
  1. netstat -anp | grep 9836 
  2. tcp        0      0 172.29.63.17:1399           172.29.63.17:37769          ESTABLISHED 9836/java  

 2、使用jconsole监控Jboss

jconsole远程监视的配置步骤如下:

服务器端:

在JBoss的 /bin/run.sh中

JAVA_OPTS="$JAVA_OPTS -Dprogram.name= 加上

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

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

客户端连接服务器端的port为:1099,启动netstat -nap|grep 1099 查看1099端口是否已在监听 


客户端配置参见: 
http://java.sun.com/j2se/1.5.0/docs/guide/management/jconsole.html

jconsole文档地址:http://java.sun.com/j2se/1.5.0/docs/tooldocs/share/jconsole.html


就在启动脚本的java参数中加入:
-Dcom.sun.management.jmxremote.port=1099Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false
 

 3、监控Activemq的远程端口

在Activemq中,提供了端口给外面的jconsole连接的端口,可以在activemq.xml配置文件中进行
  1. <managementContext> 
  2.             <managementContext createConnector="true" connectorPort="1399"/> 
  3.         </managementContext> 
 
 三、Jprofiler
10天试用注册:http://www.ej-technologies.com/download/jprofiler/trial
 下载:http://www.ej-technologies.com/download/jprofiler/files.html
 问题1:查看内存中的所有对象
"Go To" -> "All Objects", 打开之后可以按照各种情况进行排序
问题2:展开调用树
“Go To”  -> "Allocation Call Tree", 再右键点击“expand  mutiple level”展开整个调用树
 问题3:服务器端开启远程访问端口
JAVA_OPTS="$JAVA_OPTS -agentpath:/home/forum/jprofiler/bin/linux-x64/libjprofilerti.so=port=10004 "
jprofiler的默认端口为8849,可以调整。 eg:-agentpath:/home/forum/jprofiler/bin/linux-x64/libjprofilerti.so=port=10004

原创粉丝点击