JConsole allows you to monitor a Java Virtual Machine and the applications that run on it. A slightly improved tool similar to JConsole is JVisualVM and they're setup is the same. By enabling JMX (Java Management Extensions) settings on the JVM at startup, JConsole can then monitor JVM performance and how much resources its applications are consuming.
JConsole and JVisualVM ship with the JDK, and is thus an ideal starting point for monitoring server activity.
JConsole is useful for:
- Monitoring memory usage (heap, non-heap, and native)
This would tell you how close you are to using the maximum memory you defined for the heap. Looking at its values also gives you sense of how much garbage collection is going on.
If the application server is getting close to the maximum Java heap size, it might result in excess garbage collection and slowness.
- Monitoring the number of threads in use
You might take a look at these values when testing concurrency issues, or to check individual stack traces.
- Monitoring the CPU usage for the JVM
This might help you determine whether or not it's the JVM that's responsible for the slowness, or whether it's some other element in the software stack (slow database, slow network connections, slow web server, etc).
This article assumes you are using Tomcat; the equivalent settings for other application servers (such as WebSphere and WebLogic) are left for the reader to verify. I worked out this article's example using a Ubuntu virtual environment; there is nothing to stop you from adapting this example to a Windows environment.
Modifying the Tomcat startup script
The first step is to modify the Tomcat startup script to include JMX settings; this will allow the JConsole application to connect to the JMX port and thus monitor the server's activity.
The first step is to modify the setenv.sh script, which controls the Tomcat startup settings. Add the CATALINA_OPTS variable:
export CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=<IP Address>"
These options turn on JMX functionality, activate port 8999 for JMX monitoring, and set the hostname to the IP address of the machine that's hosting JRS.
After you have modified the script, you can go ahead and start up the Tomcat server.
Starting up JConsole
- From the command line, and as long as the JDK bin directory is in your path, go ahead and simply type "jconsole" into the command line. You should get something like this at start up:
- Select the "Remote Process" option and fill in the data with the server name (or IP)
- Once you connect, and you are on the "Overview" tab, you'll get a summary view of the JVM state:
- Observe the behavior before and after a test condition you're interested in.
If you know that performing a certain task results in slower performance, do a before-and-after comparison to quantify that observation. Using the tool, you'll be able to compare the values for memory usage, thread count, class loading, and CPU usage. For example, you might test:
Ad Hoc Reports - before running them, while you're running them, after they've run
If you see normal values and are still experiencing slow performance (for example, low CPU% but the end user experiences a slow system anyway), then at least you will have eliminated the JVM as the source of your performance issues.
- Analyze the chart data
Besides just presenting the data graphically, you can download the chart data in CSV format. By simply right-clicking on a chart image, JConsole will give you the opportunity to save the chart data to your disk. This allows to analyze the data with another tool, such as Excel. For example, the data in the Memory tab:
can be turned into this:
http://download.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html - This is the official JConsole user guide from Oracle