Using JConsole to Monitor Applications
The Java 2 Platform, Standard Edition (J2SE) 5.0 release provides comprehensive monitoring and management support. It not only defines the management interfaces for the Java virtual machine, but also provides out-of-the-box remote monitoring and management on the Java platform and of applications that run on it. In addition, JDK 5.0 includes the Java Monitoring and Management Console (JConsole) tool. It uses the extensive instrumentation of the Java virtual machine to provide information on performance and resource consumption of applications running on the Java platform using Java Management Extension (JMX) technology. The article describes how JConsole can be used to observe information about an application running on the Java platform. The article first gives an overview of the J2SE 5.0 monitoring and management architecture and how JConsole plugs into the architecture. It then describes how to use JConsole to access several core monitoring and management functionalities provided by the Java platform including:
Architectural Overview
Figure 1 shows the architecture of J2SE 5.0 monitoring and management support. In the 5.0 release, the Java virtual machine (JVM) is highly instrumented for monitoring and management. The platform instrumentation provides information on performance, resource consumption, and the JVM and logging settings of applications running on the Java platform. JMX provides a standard way to instrument the Java runtime environment and applications, and the JMX Remote API allows that instrumentation to be accessed remotely. The instrumentation is accessible through the JMX managed bean (MBean) interfaces, which are registered in the platform MBean server. Applications can also create their own MBeans and register them in the platform MBean server, which can serve as a single point for remote access. A JMX-compliant client, such as JConsole, can connect to the platform MBean server and manage the application (as well as the Java platform) using JMX technology.
Platform Beans
The Java platform provides a set of platform MBeans (Managed Beans) for monitoring and management of the Java virtual machine and the logging facility: <!-- BEGIN VCD8 DATA TABLE COMPONENT -->
An MBean is a managed object that follows the design patterns conforming to the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed. The management interface of an MBean comprises a set of readable and/or writable attributes and a set of invokable operations. MBeans can also emit notifications when predefined events occur. Each platform MBean has a rich set of attributes and operations such as memory usage, thread CPU usage, garbage collection statistics, and so on. Some might also emit notifications. We will explore a few platform MBeans in the following sections.
The JConsole Tool
JConsole is a JMX-compliant GUI tool that connects to a running JVM, which started with the management agent. To start an application with the management agent for local monitoring, set the JDK_HOME/bin/java -Dcom.sun.management.jmxremote -jar JDK_HOME/demo/jfc/Java2D/Java2Demo.jar where To start JConsole, run JDK_HOME/bin/jconsoleA connection dialog box opens, whose Local tab lists any JVMs running on the local system started with the same user ID as JConsole, along with their process ID and class/argument information.
Jconsole can connect to a running JVM in 3 different ways:
When JConsole successfully establishes the connection, it obtains information from the JVM MBeans in the connected JMX agent, and displays the information in the following tabs:
The MBeans tab displays information on all MBeans registered in the target JVM in a generic way. The MBeans tab allows you to access the full set of the platform instrumentation, including those that are not visible in the other tabs. Note that the other tabs present some of the information on the platform MBeans. In addition, you can monitor and manage your application's MBeans using this MBeans tab.
Using MBeans Tab to Monitor and Manage MBeans
All platform and application MBeans that are registered in the connected JMX agent can be accessed through the MBeans tab. For example, the Memory MBean is defined as follows:
The Memory MBean contains four attributes:
The Memory MBean supports one operation,
The tree on the left shows the list of all MBeans, organized by their object names. An MBean object name consists of a domain name and a list of key properties. For example, the platform MBeans for the JVM are all grouped under the " When you select an MBean in the tree, its attributes, operations, notifications, and other information are displayed on the right. You can set the value of attributes, if they are writeable (the value is displayed in blue). You can invoke operations displayed in the Operations tab. You can also watch for the notifications emitted by an MBean: by default, JConsole does not listen to any notification emitted by an MBean until you subscribe to it. You can click the "
The Memory tab provides information about memory consumption, memory pools, and garbage collection statistics by accessing the memory system, memory pools, garbage collector MBeans.
The chart shows the memory usage over time, for heap and non-heap memory, and for specific memory pools. The memory pools available depend on the JVM being used. The following list shows the pools for the HotSpot virtual machine.
The Details area shows several current memory metrics:
The bar chart on the lower right shows memory consumed by the memory pools in the JVM. The bar turns red when the memory used exceeds the usage threshold. The usage threshold is one of the attributes defined in the Memory Pool MBean for low memory detection support. A set of methods defined in the MemoryPoolMXBean interface for low memory detection support follows.
Each memory pool may have two kinds of memory thresholds for low memory detection support: a usage threshold and a collection usage threshold. Either one of these thresholds might not be supported by a particular memory pool. The usage threshold is a manageable attribute of a memory pools. It enables the monitoring of memory use with low overhead. Setting the threshold to a positive value enables usage threshold checking for a memory pool. Setting the usage threshold to zero disables usage threshold checking. The default value is supplied by the JVM. A JVM performs usage threshold checking on a memory pool at the most appropriate time, typically during GC and sometimes at allocation time. If the JVM detects that the current memory usage exceeds the usage threshold, it will set the Some memory pools may not support the usage threshold. You can use the Collection usage threshold is a manageable attribute of some garbage-collected memory pools. After a JVM has performed garbage collection on a memory pool, some memory in the pool will still be occupied by reachable objects. The collection usage threshold allows you to set a value to check against the memory usage only after garbage collection. If the JVM detects that the memory usage exceeds the collection usage threshold, it will set the You can use the The usage threshold and collection usage threshold is set in the MBeans tab. For example, select the
When the memory usage of the
As described earlier, the memory system MBean defines a boolean attribute called Verbose that allows you to turn the GC verbose tracing on or off dynamically. The GC verbose traces will be displayed at the location specified at JVM startup. The default location for GC verbose output of the Hotspot virtual machine is To enable or disable the GC verbose tracing, select the Memory MBean and set the Verbose attribute to true or false. Similarly, the class loading MBean also has the Verbose attribute, which can be set to enable or disable class loading verbose tracing.
The Threads tab provides information about threads running in an application.
The Threads list in the lower left lists all the active threads. If you enter a string in the Filter field, the Threads list will show only those threads whose name contains the string you enter. You can get the thread dump of a thread by clicking on the name of a thread in the Threads list. The Threading MBean provides several other useful operations that are not covered by the Threads tab:
To access these additional features, go to the MBeans tab and select the Threading MBean in the MBeans tree. It lists all the attributes and operations for accessing information in the JVM being monitored.
To check if your application has run into a deadlock (for example, your application seems to be hanging), you can invoke the
Once you click on the
For a deadlock situation, you would typically be interested in the stack trace. You can enter the thread ID of a deadlocked thread in the first parameter of the getThreadInfo operation (say,
Double-clicking on the value field of the stackTrace attribute will show a Composite Navigation view that allows you to traverse the stack trace. Figures 13 and 14 show the Composite Navigation view that displays the top and second top frame of the stack trace of
The Threads tab provides a more user-friendly way to look at the stack trace of a thread. You can find the name of the deadlocked threads using the
The Logging MBean defines a
The Logging MBean also supports three operations:
All three operations take a logger name as the first parameter. To change the level of a logger, enter the logger name in the first parameter and the name of the level it should be set to in the second parameter of the
The JDK 5.0 extends the Operating System MBean to include certain OS resource information such as:
When the Operating System MBean in the MBeans tab is selected, you see all the attributes and operations including the platform extension. You can monitor the changes of a numerical attribute over time—for example, the process CPU time—by double-clicking the value field of the attribute.
In addition, the VM tab and the Summary tab provide information on the operating system resources. The SampleTest application being monitored has its own Hello MBean with the object name: com.sun.example:type=HelloIf the CacheSize attribute is changed, the Hello MBean will send a notification. You can use the MBeans tab to manage your application's MBeans, as well as the platform MBeans. For example, you might want to monitor when the CacheSize attribute is changed. You first subscribe to the notifications in the Notification tab. If you change the CacheSize to 300 , you will see one notification sent out.
Summary
The JConsole tool demonstrates the comprehensiveness and ease-of use of the JDK 5.0's monitoring and management features. You can use JConsole to connect to a running Java virtual machine, and then monitor the memory usage and thread activity. You can obtain class-loading information, plus information on the JVM and the operating system. JConsole also gives you a generic way to access MBeans, such that you can access the full set of instrumentation of the Java platform and also manage your application. We described how to access several core monitoring and management functionalities provided by the Java platform through JConsole, including low memory detection, virtual machine verbose tracing, deadlock detection, and logger level control.
Related Information
About the Author
Mandy Chung is a Senior Staff Engineer in the Java Serviceability Group at Sun Microsystems. She designed and implemented the JVM monitoring and management interface in JDK 5.0 and led the JConsole development. She holds a masters degree in Computer Science from the University of California, Davis, and a B.S. in Computer Studies from the University of Hong Kong. |
本文原始地址:
http://java.sun.com/developer/technicalArticles/J2SE/jconsole.html