jvisualvm是Jdk自带的,具体点,据说是jdk1.6-07后加到jdk中的。本地监控很简单,在JAVA_HOME/bin中打开jvisualvm即可,所有本机的java程序会自动列出来。我们来说下远程监控的问题。
当然网上有很多相关资料,我就把我实践的结果列出来吧。
环境, 本地: WIN-XP sp3, jdk1.6.0_17 远端:CentOS5.1, jdk1.6.0_3
具体步骤如下,
1. 启动远端jstatd.
1.1 配置[b]java[/b]安全访问,将如下的代码存为文件 jstatd.all.policy (名字随便起),但要放到JAVA_HOME/bin中,其内容如下,
grant codebase"file:${java.home}/../lib/tools.jar"{ permission java.security.AllPermission; };
1.2 进入到JAVA_HOME/bin中,执行./jstatd -J-Djava.security.policy=jstatd.all.policy
注:这时候能启动正常,但是后面本地用 jvisualvm的时候抛异常,
java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:180)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(RMIDirectSocketFactory.java:22)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(RMIMasterSocketFactory.java:128)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:595)
Caused: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:601)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:198)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:110)
at java.rmi.server.RemoteObjectInvocationHandler.invokeRemoteMethod(RemoteObjectInvocationHandler.java:178)
at java.rmi.server.RemoteObjectInvocationHandler.invoke(RemoteObjectInvocationHandler.java:132)
at $Proxy7.activeVms(Unknown Source)
at sun.jvmstat.perfdata.monitor.protocol.rmi.RemoteVmManager.activeVms(RemoteVmManager.java:82)
Caused: sun.jvmstat.monitor.MonitorException: Error communicating with remote host: Connection refused to host: 127.0.0.1; nested exception is:
java.net.ConnectException: Connection refused: connect
at sun.jvmstat.perfdata.monitor.protocol.rmi.RemoteVmManager.activeVms(RemoteVmManager.java:85)
at sun.jvmstat.perfdata.monitor.protocol.rmi.MonitoredHostProvider.activeVms(MonitoredHostProvider.java:217)
at com.sun.tools.visualvm.jvmstat.application.JvmstatApplicationProvider.processNewHost(JvmstatApplicationProvider.java:126)
at com.sun.tools.visualvm.jvmstat.application.JvmstatApplicationProvider.access$000(JvmstatApplicationProvider.java:69)
at com.sun.tools.visualvm.jvmstat.application.JvmstatApplicationProvider$5$1.run(JvmstatApplicationProvider.java:276)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
[catch] at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
==>
据查,有两种方法解决,一种是把 /etc/hosts中的ip改成实际的IP,然后重启网络,但我用此法没有解决问题。另一种是,在jstatd后加hostname的参数,如下,
./jstatd -J-Djava.security.policy=jstatd.all.policy -J-Djava.rmi.server.hostname=192.168.0.23
我用第二种方法解决了此问题。
2.打开本地的jvisualvm, 右键Remort,选择 "Add Remort Host...",在弹出框中输入你的远端IP,比如192.168.0.23. 连接成功后如图,
其中CPU一项显示"NOT supported for this JVM",不知何故,难道是远端的jdk版本需要升级到jdk1.6.0_17? 还没有试。 另外“Perform GC”跟"Heap Dump"都亮,也不知何故。
顺便解释下Heap跟PermGen的区别, 一般而言,PerGen放的是类的定义;而Heap里面放的是类的实例;所以一般程序稳定运行后,PermGen基本保持不变,要变也只会增加,因为GC只会扫描heap中的实例;而Heap变化较大,OutOfMemory一般也是Heap不够了。