Java VisualVM to profile a remote server

Since Version 1.5, Sun's JDK includes a nice profiling tool called VisualVM, intended to be used by developers, sysadmins and any person that needs to troubleshoot and profile memory consumption in Java applications and servers.  To run it, just execute the file jvisualvm located in $JAVA_HOME/bin.

 

 

To profile local applications is pretty easy, since you only need to start it and it'll detect all Java-based applications and you'll be able to see the Heap/PermGen usage, the number of threads used, the classes loaded by the class loader and other stuff.

When it comes really handy is to profile and troubleshoot remote applications (e.g. profile a Tomcat instance in a remote server).  In order to do these, you should only start the application jstatd in the remote server, open the required ports and you're ready.  Here are the steps to do it:

 

The jstatd tool is an RMI server application that provides a interface to allow remote monitoring tools to attach to JVMs running on the local host.


It is located in the JDK's bin directory, located at $JAVA_HOME/bin, and in order to run it, you need to create policy file to enable jstatd to run without security exceptions.  For this, create a file called jstatd.all.policy in $JAVA_HOME/bin and paste the following contents into it:

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

Then, just run the jstatd application from the command line, like this:

./jstatd -J-Djava.security.policy=jstatd.all.policy


After this, check the ports that need to be opened to allow the remote instance of VisualVM to connect to it. This application opens 2 ports: the default RMI port, which is 1099, and then a random port, which changes every time you start the application.  To check which ports are used by jstatd, run the following command:

netstat -nlp | grep jstatd


And you should get an output like this:

tcp        0      0 :::39779                    :::*                        LISTEN      28661/jstatd        
tcp        0      0 :::1099                     :::*                        LISTEN      28661/jstatd


The 4th column indicates the ports that are open.  In this case, the ports that need to be opened in the firewall are 1099 and 39779.  To open this, you should check IPTables documentation for how to accomplish this.

After all ports are opened and jstatd is running, just open VisualVM, right click on Remote and select Add Remote Host, and finally type the IP address of the remote host.  After this, you'll be able to see all Java-based applications running in the remote server, including Tomcat, Ant, jstatd and any other.

The following screenshot shows a local VisualVM profiling an instance of Tomcat running in a remote host (click to enlarge):

 

 

Comments (0)