Today we will show a pretty useful program which is a part of Java JDK but whose existence is not known to many Java programmers. It’s named jconsole and it is a GUI client for accessing Java Management Extensions (JMX). In short, it allows you to view a lot of data about memory usage, running threads, garbage collector performance and much more other useful runtime informations about any Java process running on your machine.

The program itself is part of Java JDK 1.6. It is extremely easy to run and use. If you are running a Linux system with JDK 1.6 installed, all you have to open the terminal and type:

jconsole

On a Windows system it should also work by running it from the Command Line, assuming you have your PATH value properly set to your JDK’s bin directory. Otherwise you should find the jconsole.exe file in ‘C:\Program Files\Java\jdk1.6.0_16\bin’ directory (change the jdk1.6.0_16 to directory name your JDK is installed in).

Either way, after running it you will see the cool ‘New Connection’ dialog window:

jconsole start

This is where you can specify which Java process you want to connect to. Because the JConsole is itself a Java process, you see it too on the ‘Local Process’ list. If any other Java programs were running on your system, they would be automatically discovered and you would see them on the list too, under some other PID. There is also a ‘Remote Process’ option that allows you to connect to a Java process running on another machine (this requires more configuration though).

For the simplicity let’s connect now to JConsole process itself. Click ‘Connect’ and wait for like 3 seconds while the JConsole is trying to connect. After it succeeds, you will be moved to the ‘Overview’ screen which shows a few graphs about memory usage, CPU, Loaded classes and so on:

jconsole overview

To put it clear: in this situation the JConsole program is connected to itself and the displayed data are of the JConsole Java process. Normally you would not use it like that, instead you would connect it to some real, separate program, for example a Java EE server or any other running Java application.

All the graphs you can see in the JConsole can be clicked on with the right mouse button in order to save their data as a CSV file. Besides ‘Overview’ there are also other tabs, like ‘Memory’ and ‘MBeans’. In the ‘Memory’ tab you can see a lot of information about how Garbage Collector works and distribution of data in memory. You can choose any of the memory regions by clicking one of the green rectangles on the bottom right and a detailed graph will be displayed:

jconsole memory

Since JConsole is a client for the JMX services, you could use it to access your own MBeans. They are special kind of classes (kind of Java Beans with getters and setters) useful for gathering informations about resources you care about. All the graphs you can see in the JConsole are provided by some standard set of MBeans registered by default when you run any Java application. You can write and register also your own MBeans. There is a nice tutorial about it available on Sun’s website.