In addition to comprehensive monitoring and application management support offered by J2SE 5.0, JDK 5.0 and JDK 6.0 include the Java Monitoring & Management Console tool, also known as JConsole. The jconsole command launches a graphical console tool that enables you to monitor and manage Java applications on a local or remote machine.

JConsole can attach to any application that is started with the Java Management Extensions (JMX) agent. A system property defined on the command line enables the JMX agent. Once attached, you can use JConsole to display useful information such as thread usage, memory consumption, and details about class loading, runtime compilation, and the operating system.

Read about JConsole’s usage options, and find out how JConsole changed from JDK 5 to JDK 6. (Note: I am assuming that readers are familiar with Managed Beans, or MBeans, and JMX concepts.)

Why use JConsole?

In order to keep applications running smoothly, it’s important to be able to get information about performance and resource consumption, especially for long-running applications like Web sites. While you can get some information from the operating system (such as CPU and memory usage), you will often need much more detailed information. This is why application server vendors offer specialized tooling to monitor the application server and the applications running inside the application server.

But what if you’re not using an application server or if you’re using an application server that does not include a monitoring tool? Rather then having to rely on the operating system and log and trace files or build custom monitoring code, you can use the JMX that are part of the J2SE platform. MBeans significantly help here. While JConsole is not as sophisticated as some of the tooling offered by application server vendors, it does give some valuable information about resource consumption and access to the MBeans running in the JVM. By adding MBeans to your applications or using an application server like JBoss that supports JMX, you can easily extend the out-of-the-box functionality. JDK 6 even enables you to add functionality to the JConsole application via plug-ins.

In addition to monitoring, you can also use JConsole to dynamically change several parameters in the running system. For example, you can change the setting of the -verbose:gc option so that garbage collection trace output can be dynamically enabled or disabled for a running application.

How to use JConsole

JConsole requires Java 5 or later, so if you have an earlier version, it is time to upgrade. I recommend using Java 6, since its version of JConsole has more features. It is possible to use JConsole from JDK 6 to connect to a JDK 5 JVM; you’ll only be missing the features added to the JVM in release 6.

If you’re using Java 5 or 6, you need to start the application that you want to monitor while passing an extra flag to the JVM to enable monitoring. Let’s assume you want to start MyJavaApp.jar with JMX enabled. If you’re launching the application from the command line, simply add after the java command, like this:

java -jar MyJavaApp.jar

JConsole comes with the JDK (but not the JRE) and can be found in the %JDK_HOME%/bin directory. To launch JConsole, open a terminal or command window, change to the directory containing it, and execute jconsole. When JConsole starts, it shows a window listing the managed Java VMs on the machine. The process id (pid) and command line arguments for each Java VM are displayed. Select one of the Java VMs, and JConsole attaches to it. You’ll see a main window similar to Figure A.

Figure A

The JConsole interface in JDK 5 is composed of the following six tabs:

  • Summary displays summary information on the JVM and monitored values.
  • Memory displays information about memory use.
  • Threads displays information about thread use.
  • Classes displays information about class loading.
  • MBeans displays information about MBeans.
  • VM displays information about the JVM.

The Memory tab is used to monitor the memory consumption of the monitored application. To check for memory leaks in your application, you can start a load test to create a constant load on the application. If the functionality you’re testing during the load test does not contain a memory leak, the memory usage should stay between a specific upper and lower limit. See Figure B.

Figure B

Monitoring memory consumption is just one of the useful things you can do with JConsole. You can also monitor CPU usage, detect deadlocks on the Threads tab, and use the MBeans tab to monitor or manage specific parts of the JVM or the application running in it.

What is new in JDK 6

Here’s a quick rundown of the changes to JConsole from JDK 5 to JDK 6:

  • The Summary tab has been replaced with the Overview tab (see Figure C). This tab shows the graphs from the Memory, Threads and Classes tabs and a graph of the CPU usage of the JVM, which is not available in JDK 5. The information on the Summary tab has been merged with the VM tab, which is now called VM Summary.
  • JConsole has a Plugin API for adding custom functionality.
  • JConsole can use the API to dynamically attach to a JVM. This requires a Java 6 JVM. (To connect to a Java 5 JVM, the JVM must be started with
  • If you’re connecting to a Java 6 JVM, you’ll find a new HotSpot Diagnostics MBean on the MBeans tab. This new MBean provides an API to request heap dump at runtime and change the setting of certain JVM options.

Figure C

References for further reading

Peter V. Mikhalenko is a Sun certified professional who works as a business and technical consultant for several top-tier investment banks.


Get weekly development tips in your inbox
Keep your developer skills sharp by signing up for TechRepublic’s free Web Developer newsletter, delivered each Tuesday. Automatically subscribe today!

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays

Subscribe to the Developer Insider Newsletter

From the hottest programming languages to commentary on the Linux OS, get the developer and open source news and tips you need to know. Delivered Tuesdays and Thursdays