Article Categories

Using Java VisualVM Remotely

Java visualVM is a troubleshooting tool for all java programs.

What you can do with it?
1. Get an overview of Java related env variables
2. Monitor CPU usage, Memory usage (Heap space/PermGen) , number of threads running etc.
3. Details of all the threads and their status
4. At any given time sample CPU usage and percentage of share of every thread over CPU
5. Visual garbage collection
6. Profiling (available only for local machines)

When you can use it?

1. analyze memory usage (when you face problems like OutOfMemoryException)
2. analyze threads (excessive CPU usage)

(note: A.B.C.D is the IP address of the remote m/c)

HOW?

There are two ways to use jvisualvm remotely. (when debugging a remote machine via local m/c where the jvisualvm is running)


1. jstatd: provides a interface to allow remote monitoring tools to attach to JVMs running on the local host.(http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstatd.html)

a. Run the command:
./jstatd -J-Djava.security.policy=<path to policy file> -J-Djava.rmi.server.hostname=A.B.C.D

This will expose all the jvm’s running in the remote.
jstatd requires a policy file.

b. create the policy file:
vim jstatd.all.policy

c. write the following in the file and save:
grant codebase “file:/usr/java/jdk1.7.0_79/lib/tools.jar” { permission java.security.AllPermission; };

(check the location of tools.jar file in your remote)

2. jmx : java management extension. (http://docs.oracle.com/javase/tutorial/jmx/overview/index.html). jmx is more generic in nature and comes with overwhelming amount of features. Use of jmx is recommend as jmx offers thread monitoring and thread-sampling which jstatd doesn’t support.

a. Run the service with the following command:

./java -jar -XX:+HeapDumpOnOutOfMemoryError -Djava.rmi.server.hostname=A.B.C.D -Dcom.sun.management.jmxremote.port=3333 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false /opt/yaxa/ml/yaxaml-1.0.0.1.jar

Explanation:

1. -XX:+HeapDumpOnOutOfMemoryError :: to enable taking heap dump when OOME happens.
2. -Djava.rmi.server.hostname=A.B.C.D :: address of the remote machine which is the m/c where you are running the command.
3. -Dcom.sun.management.jmxremote.port=3333 :: jmx port.
4. -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false :: security null.

Now, after starting JMX or jstatd open jvisualvm ( can be found in jdk/bin). then add remote host and add jstatd or jmx connection.

Do whatever you want, you live in a free world!

Useful terms:

Heap: The Heap graph displays the total heap size and how much of the heap is currently used. These are the figures that a Java technology-based application (Java application) can obtain using the java.lang.Runtime.totalMemory() and java.lang.Runtime.freeMemory() calls.

PermGen: The PermGen graph displays changes in the permanent generation area over time. The permanent generation is the area of the heap where class and method objects are stored. If an application loads a very large number of classes, then the size of the permanent generation might need to be increased using the -XX:MaxPermSize option.

 

About the Author

Avik Dutta

Avik Dutta

Avik Dutta works at Polaris Networks as a Software Engineer.

(Show Bio)