How do you find out what is happening to a running Java application?
We may have questions like:
How much memory has it allocated on the heap?
What are the threads that are active?
What is the CLASSPATH or command line parameters that are set?
Questions like these cannot be answered easily without a management framework.
On the Java platform, the Java Management
Extension or JMX is the monitoring and management framework. Sun has bundled
in its distribution since JDK 5. There is an accompanying tool called jconsole which allows you to monitor and manage Java applications with JMX enabled.
In the following series of articles, we will look at:
How to monitor existing Java applications with jconsole
How to expose specific parts of your application to JMX. These includes exporting your
application's attributes, operations and events to jconsole or any JMX compliant tools
How to develop your own management client that integrates with JConsole
In JDK 5, Sun's Java Virtual Machine (JVM) has built-in
instrumentation that allows you to find out what is going on internally in a
currently executing application. However, the instrumentation is not enabled by
default. To enable the JVM for instrumentation, set the com.sun.management.jmxremote property to true. You can
do this from the command line using the '-D' option
You can find Notepad.jar in $JAVA_HOME/demo/jfc/Notepad. Once the Notepad application has started, run
JConsole. A window, as shown in Figure 1, shows which
JVM has JMX enabled.
Select application to monitor
Select the application to connect to, if there is more than one, then click on Connect.
Once you have connected to the application, jconsole will display a summary of the resource
utilization and environment settings of that application. An example of this is
shown in Figure 2.
Figure 2 Summary of resources used in an application
The summary page shows
the number of threads that are currently spawned in the JVM.
the current and maximum heap size. It also displays garbage
collector (GC) statistics and what is the collector that is currently used. In
the example shown in Figure 2, we see that there are actually two collectors
that are active. The one that is used in the young generation is the default
collector called Copy while the collector that is used in the old generation
is the Mark-Sweep collector. You will also be able to view some GC statistics
like the number of collections that has occurred (Copy = 39 times) since the
start of the application and the total time spend in the collection (Copy =
the number of classes that are loaded and unloaded in the permanent space of the
the amount of memory in the operating system.
If you want to find out details about, for example, memory,
click on the Memory tab. It will show you detailed information about memory
utilization at specific intervals during the lifetime of the application; see an
example of this in Figure 3.
Figure 3 Detailed memory
You can also get statistics on utilization on specific parts
of the VM's memory. I will leave Threads, Classes and
VM for the user to explore.
One tab that is not represented in the
summary page is the MBeans tab. MBeans are managed resources. Applications expose statistics
and configurable items through MBeans. For example,
the JVM exposes GC statistics through an MBean.
Figure 4 Memory MBean
In Figure 4, we see the memory MBean. An MBean consists of the following
a password file called mypasswords, for example. Add
username and password into mypasswords in the
following format: username<space>password. Spaces and tabs cannot be
part of your username. Here is an example of two users with their corresponding
Since the password file is in plain text, you will have to
change the permission so that only the relevant users may be able to read
you to give access rights to Fred and Barney. The default location of the
access rights files is located in $JAVA_HOME/jre/lib/management/jmxremote.access. fred and barney can either have
"readonly" or "readwrite".
If you have "readwrite" access, this means that you
can change the MBean like changing the 'Verbose'
attribute (see above). Thus Fred and Barney will only have readonly access.
Now start the Notepad application again, with the following options
Lee Chuk-Munn has been programming in the Java language since 1996, when he first joined Sun Microsystems in Hong Kong. He currently works as a senior developer consultant and technology evangelist for Technology Outreach at Sun in Singapore. Chuk's focus is in Java APIs, Java EE, Java SE, and Java ME. Chuk graduated in 1987 from the Royal Melbourne Institute of Technology in Melbourne, Australia, where his favorite subject was compiler theory.