Wednesday, July 13, 2011

Remote JVM profiling on Amazon EC2

Using SSH Port Forwarding (SSH tunneling) establish a secure connection with an Amazon EC2 instance, run Java VisualVM or JConsole locally, and monitor a remote Tomcat process over this connection.

This approach requires Tomcat 6.0.24 or latter as this was the version that the JmxRemoteLifecycleListener was introduced. I'll look at how to monitor older versions of Tomcat using X11 Forwarding in an upcomming post.

Setup security

First we need to open a port on the EC2 firewall, for JMX/RMI communication, and limit access to the public IP address of our client computer.

To find the client's IP address enter


Using a security group of trader, a port of 10001 for JMX/RMI, and a client IP address of, we'll open the port using the EC2 command line tool

ec2-authorize trader -p 10001 -s <your public IP address>

Configure Tomcat

Determine the host you want to monitor. 16 means filter only running instances.

ec2-describe-instances --filter instance-state-code=16

Now SSH to the instance

ssh -i $EC2_AWS/ec2.pem

To check what version of Tomcat is running


To set the JMX/RMI port number we need to enable and configure the JMX Remote Lifecycle Listener for Tomcat. First we need to download the required jar.

sudo wget -P $CATALINA_HOME/lib

Install into Tomcat

cp catalina-jmx-remote.jar $CATALINA_HOME/lib

Next edit /etc/default/tomcat6

sudo vim /etc/tomcat6/server.xml

And add the following listener

<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" rmiRegistryPortPlatform="10001" rmiServerPortPlatform="10001"/>

Now edit the /etc/default/tomcat6 file

sudo vim /etc/default/tomcat6

And add the following system properties. Note that the java.rmi.server.hostname is the public hostname of the EC2 instance.


Restart Tomcat for the changes to take affect

sudo /etc/init.d/tomcat6 restart

Monitor Tomcat

Now, on the client computer create the SSH tunnel that will forward port 10001 on our local client computer to port 10001 on the remote EC2 instance.

ssh -N -v -L 10001:localhost:10001

Open a new terminal and enter either



$JAVA_HOME/bin/jconsole localhost:10001

Sometimes takes a few seconds for the localhost:10001 connection to show in the UI.


The above was tested using:

- Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
- Tomcat 6.0.28
- Ubuntu 10.04 Lucid Lynx

- Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
- Ubuntu 11.04 Natty Narwhal

Why are we using



1 comment: