
Set up VisualVM to monitor resources of remote Java Spring Service
This article is to introduce the way to use VisualVM to monitor the resources on remote Java Spring service.
Environment:
– Server: AWS EC2 instance, Tomcat server, Spring MVC Application
– Tool: VisualVM 2.0.2 (https://visualvm.github.io/download.html)
The items that we will work through are as following:
1. Configure the JVM arguments or add JMX remote params on Remote server;
2. Configure the Jstat to retrieve the application information on Remote Server;
3. Remote access with ssh, enable sock tunnel and start Jstatd process
4. Configure the VisualVM and retrieve the application info.
Configure the JVM arguments or add JMX remote params on Remote server
Setup the JAVA_OPTS or CATALINA_OPTS as JVM arguments to allow JMX remote.
Create file /tomcat/bin/setenv.sh
CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
Configure the Jstat to retrieve the application information on Remote Server
About Jstat:
The jstatd tool is an RMI server application that monitors for the creation and termination of instrumented HotSpot Java virtual machines (JVMs) and provides an interface to allow remote monitoring tools to attach to JVMs running on the localhost.
docs.oracle.com
Create a permission declaration file (jstats_permission.txt) anywhere, for example, at the ec2-user folder of EC2 instance, with content as followings:
grant {
permission java.security.AllPermission;
};
My permission file is created at /home/ec2-user/jstats_permisison.txt
Remote access with ssh, enable sock tunnel and start Jstatd process
Access remotely to server with sock tunnel enabled as following:
ssh -D 10000 -i <file_path_to_pem_file>
After a successful connection to remote service, it’s required to start the Statd process to retrieve the service information. To start the StatD process with declared permissions, follow the instruction:
jstatd -J-Djava.security.policy=/home/ec2-user/jstatd_permission.txt
Now, let the terminal runs itself. We will come back to our PC to setup the VisualVM to see what happens with our service.
Configure the VisualVM and retrieve the application info
Add Remote Host
Open VisualVM and add remote host in file -> add remote host.

Host name is the private IP of EC2 instance. The Display Name can be whatever you want.
Add JMX connection
To add JMX connection, right click at the remote service that has been created in previous step and choose “Add JMX Connection…”

The connection is <your EC2’s private IP>:10000.
10000 is the sock tunnel port that you are using while connecting to the remote server by ssh.
After that, you can see the JStatD and Tomcat processes are ready for you to monitor.
![]()