There are many reasons one might desire JVM runtime information. These include the ability to figure out why an application runs differently in two different environments, the ability to use other tools and utilities based on runtime information (such as Java process ID), and the ability to identify Java processes that are not closing even when politely asked.
The jps command (Java Virtual Machine Process Status Tool) is one of the tools I use most often in my Java development. I have covered it more thoroughly before, but I quickly describe it here for convenience. The
jpscommand options I generally use are "l" and "m." The overall command,
jps -lmprovides the process IDs of all running Java processes on that machine and the options provide enough detail to know which IDs go with which process descriptions. There are some permissions issues that can potentially reduce the usefulness of
jpsand the jps man page warns that "this utility is unsupported and may not be available in future versions of the JDK," but I have found this simple command-line tool to be enormously helpful.
One of the ways to run the jinfo (Java Configuration Info) tool is run it against a Java process. The
jpstool just mentioned can be used to retrieve the appropriate Java process ID.
-helpflag can be used to see how to use
jinfo. There are fewer options available on Windows than on Linux. The options available to
jinfoon Windows are shown in the next screen snapshot.
Even with the Windows
jinfo, I can view individual Virtual Machine (VM) options. This is demonstrated in the next screen snapshot.
How do you know which VM options are available? In Windows,
jinfodoes NOT support the
-flagsoption which is supposed to provide a list of VM options. However, the HotSpot VM options are available at http://java.sun.com/javase/technologies/hotspot/vmoptions.jsp and Troubleshooting Guide for Java SE 6 with HotSpot VM also contains several of them. This limited version of
jinfofor Windows has only been available since J2SE 6.
On Linux, the
-flagsoption is supported and can be used to see all flags associated with the given Java process. The Linux version of
jinfoalso supports the
-syspropsto view the system properties associated with the particular application and supports no option to see properties and command-line flags. The Linux version of
jinfois obviously more powerful than the Windows version.
Command-line tools like
jinfohave several advantages such as reduced overhead, potentially faster performance, and availability to be used in non-interactive scripts. However, there are also situations when a graphical interface is preferred for ease of use or for improved presentation. JConsole supports visual representation of virtual machine information and has been available as a standard part of the Sun-provided JDK since J2SE 5 (and was improved for Java SE 6).
JConsole is particularly easy to use with Java SE 6 because the Attach API enables JConsole to automatically look up Java processes running on the same host and under the same user without any special properties being specified for the Java application being monitored (can be done with J2SE 5 if appropriate system properties are first specified). This automatic detection of local processes in Java SE 6 is shown in the next screen snapshot.
As the screen snapshot above indicates, it is easy to identify Java process IDs in JConsole as they are explicitly shown ("PID" column) in the first screen. When one clicks on one of these processes, the "Connect" button is enabled. When that button is clicked on, JConsole connects to the selected Java process.
Once JConsole connects to a selected Java process, six tabs are present by default (and JConsole allows creation of additional custom tabs). The "VM Summary" tab is useful for getting highly descriptive summary of characteristics and settings of the JVM. An example of this is shown in the next screen snapshot.
There is significantly more that can be gleaned from JConsole, but for now I'll focus on how to obtain the type of information provided by
jinfo. The VM options can be obtained via the MXBean provided with the Sun implementation of Java called
getVMoperation on this MXBean allows the name of the desired VM option to be specified in a text field and that option's value is returned when the "getVMOption" button next to the text field is clicked. The next screen snapshot demonstrates this (note that this all takes place in the "MBeans" tab).
JConsole allows system properties to be accessed via its "MBeans" tab as well. They are accessed via the MXBean
java.lang.Runtime. The next screen snapshot demonstrates how this appears when this MXBean is selected.
The only problem at this point is that the value
javax.management.openmbean.TabularDataSupportis not exactly the type of thing we're looking for when we want to see system properties. Fortunately, one can simply click on this bold string and the entire area expands as shown in the next screen snapshot.
As the last screen snapshot demonstrates, one system property is shown at a time. The "Tabular Navigation" buttons with
>icons can be used to move through the various properties. In this case, the displayed
java.endorsed.dirsproperty is the 9th of 53 properties.
Besides being more aesthetically pleasing, JConsole offers the advantage of working well and fully on Windows as well as on Linux. The fact that JConsole is constructed to generically handle exposed JMX MBeans makes its use virtually limitless. Not only can it be used to monitor and management standard properties provided with the JVM, but it can even be used to manage and monitor custom JMX-enabled applications.
VisualVM has been available as a distinct open source product for some time, but has been included with Sun's JDK since Java SE 6 Update 7. It provides advantages of JConsole such as visualization of memory and performance data and generic access to JMX MBeans.
jconsoleused to run JConsole, the
jvisualvmcommand for running VisualVM is located in the same directory as the other Sun-provided Java tools such as
jinfo. Although I have discussed jinfo and VisualVM together before, I look at them together again here for convenience.
The next screen snapshot shows the VisualVM startup screen. Like JConsole, local Java processes running under the same user are automatically displayed with the Process ID in parentheses.
Clicking on the desired process leads to several tabs being shown. The "Overview" tab is shown in the next screen snapshot.
In the bottom right corner, there is a pair of tabs nested within this "Overview" tab. In the screen snapshot above, the "System Properties" tab is selected. As shown, all the properties are shown at once and a scroll bar on the right can be used to scroll down the list. The other nested tab can be used to see any VM arguments.
To see VM options, one can use the JMX-exposed MXBean used with JConsole. To access MBeans from VisualVM, the "MBeans" plugin should be installed as I have blogged about previously. Once the plug-in is installed, the "MBeans" tab can be accessed and used in a manner very similar to how the "MBeans" tab is used in JConsole. The next screen snapshot shows viewing of one of eight VM options this way.
Like JConsole, there is much more to VisualVM than is shown here. However, as these simple examples have shown, a wide variety of information about the JVM is available via VisualVM and JMX-exposed applications can be accessed generically from this tool.
Because the Sun JVM is widely instrumented to support monitoring and management via JMX, any JMX client (generic or custom) can be used to monitor and manage the JVM. However, I see very little reason to use other tools for JMX-based JVM monitoring and management with the ready availability of JConsole and VisualVM. For even more sophisticated non-Java client access of JVM information, compatible web services clients (implement WS-Management, such as winrm) can manage and monitor JVMs via the JMX Web Services connector.
There are many alternatives available for acquiring information related to the Sun JVM. These options get better and more prevalent with each new major Java release. This blog posting has attempted to demonstrate how the command-line tools
jpsand the graphical tools
jvisualvmare readily available and easily provide information on the JVM.