Skip to content

Profiling application

You can profile local and remote Java applications using Java VisualVM.

Edit the $TOMCAT_HOME/bin/setenv.* files (setenv.sh for Linux and setenv.bat for Windows).

CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote=true"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.port=9090"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=false"
CATALINA_OPTS="$CATALINA_OPTS -Djava.rmi.server.hostname=127.0.0.1"

To enhance security, enable authentication. Edit the $TOMCAT_HOME/bin/setenv.* files (setenv.sh for Linux and setenv.bat for Windows).

CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=true"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.password.file=$CATALINA_HOME/conf/jmxremote.password"
CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.access.file=$CATALINA_HOME/conf/jmxremote.access"

Edit the access control file $CATALINA_HOME/conf/jmxremote.access:

monitorRole readonly
controlRole readwrite

Edit the password file $CATALINA_BASE/conf/jmxremote.password:

monitorRole tomcat
controlRole tomcat

Restrict access to these files:

Terminal window
$ chmod 600 $CATALINA_HOME/conf/jmxremote.access
$ chmod 600 $CATALINA_HOME/conf/jmxremote.password

The easiest way to connect to the remote JMX is to use a SOCKS proxy.

Terminal window
$ ssh user@external.server.ip -D 8888

Configure VisualVM to use this proxy. Go to Tools > Options > Network and edit Manual Proxy Settings. Configure the SOCKS proxy at localhost and port 8888.


Now you can connect VisualVM to the remote target. Go to File > Add JMX Connection… and type the IP or hostname of the remote machine and the configured JMX port (in our sample configuration, 9090).

Java includes HPROF, a profiler which collects application runtime information. HPROF is capable of presenting CPU usage, heap allocation statistics, and monitor contention profiles.

For example, it can collect CPU usage information by sampling threads.

Edit the $TOMCAT_HOME/bin/setenv.sh file (or setenv.bat if using Windows):

CATALINA_OPTS="$CATALINA_OPTS -agentlib:hprof=cpu=samples"