Friday, April 22, 2011

Various Java Monitoring tools and how to use them


  • Note: to be able to run any “X” Applications remotely, you may have to set you X server locally to  accept connections from the machine, or just allow all with a command like xhost +
  • If you are running this locally, make sure the server is in init level 5, and not 3 as by many servers are in level 3, you can do this by issuing “telinit 5” or just type “startx”
  • If you are running X through SSH, make sure at /etc/ssh/sshd_config you have X11forwarding set to yes

JvisualVM


JVisualVM is a profiling tool for profiling a running JVM. It comes bundled with the Oracle JRE 1.6+. It can be found in the %JAVA_HOME%\bin directory.

This is very useful for monitoring the memory, CPU and thread usage of a running JVM.
The tool provides for taking snapshots that can be analyzed later offline. It would be very useful to keep these snapshots.

The best way to run it is locally, the executable name is just jvisualvm. This way you get CPU and Heap profiling.  This requires tunneling an X session via SSH or exporting the display of the remote machine.
Connecting remotely provides less information than connecting locally. Notably, there is no CPU profiling.
There are two ways to run it remotely:

• JMX RMI connection
• jstatd RMI connection

• JMX RMI
Add the following JVM arguments when launching the java process:
-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

Running jvisualvm locally or via an JMX RMI connection turns on a visual view of threads running inside the process. This is not available via a jstatd remogte connection

jstatd

jstatd is another tool that ships with the standard Oracle JRE in the bin directory. jstatd is a small server process that runs alongside the JVM providing instrumentation information to remote clients.
jstatd should be considered if JVisualVM cannot be run locally or if a JMX RMI connection cannot be established.
jstatd requires a security policy file.

Create a new text file called jstatd.all.policy in %JAVA_HOME%\bin with the following contents:

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

Run jstatd on the host machine with this command:
./jstatd -J-Djava.security.policy=jstatd.all.policy &

& on the end is for Linux only--gets the process to run in the background.

Then Run jvisualvm


jconsole

Allows remote management of a running JVM. Provides more information about a running java virtual machine and can changing some settings via JMX (logging, etc).
JMX is a connection level protocol which means there is more than one way to connect to the same JMX agent running inside the JVM.
• JMX API for connecting locally
• RMI – requires JVM arguments
• JMXMP – requires special configuration on the client side but can be more secure in a production environment
Running JConsole locally - JMX API
To connect just type jconsole on the command line and select the desired process from the list when JConsole loads.



No comments:

Post a Comment