- Monitoring and Management of the Java Virtual Machine
- Platform MXBeans and the Platform MBean Server
- JConsole
- Out-of-the-Box Management in Action
- Monitoring an Application by Using JConsole
- Monitoring java virtual machine
- Key Monitoring and Management Features
- Java Virtual Machine Instrumentation
- Monitoring and Management API
- Monitoring and Management Tools
- Java Management Extensions Technology
- Platform MXBeans
Monitoring and Management of the Java Virtual Machine
The JMX technology can also be used to monitor and manage the Java virtual machine (Java VM).
The Java VM has built-in instrumentation that enables you to monitor and manage it by using the JMX technology. These built-in management utilities are often referred to as out-of-the-box management tools for the Java VM. To monitor and manage different aspects of the Java VM, the Java VM includes a platform MBean server and special MXBeans for use by management applications that conform to the JMX specification.
Platform MXBeans and the Platform MBean Server
The platform MXBeans are a set of MXBeans that is provided with the Java SE platform for monitoring and managing the Java VM and other components of the Java Runtime Environment (JRE). Each platform MXBean encapsulates a part of Java VM functionality, such as the class-loading system, just-in-time (JIT) compilation system, garbage collector, and so on. These MXBeans can be displayed and interacted with by using a monitoring and management tool that complies with the JMX specification, to enable you to monitor and manage these different VM functionalities. One such monitoring and management tool is the Java SE platform’s JConsole graphical user interface (GUI).
The Java SE platform provides a standard platform MBean server in which these platform MXBeans are registered. The platform MBean server can also register any other MBeans you wish to create.
JConsole
The Java SE platform includes the JConsole monitoring and management tool, which complies with the JMX specification. JConsole uses the extensive instrumentation of the Java VM (the platform MXBeans) to provide information about the performance and resource consumption of applications that are running on the Java platform.
Out-of-the-Box Management in Action
Because standard monitoring and management utilities that implement the JMX technology are built into the Java SE platform, you can see the out-of-the-box JMX technology in action without having to write a single line of JMX API code. You can do so by launching a Java application and then monitoring it by using JConsole.
Monitoring an Application by Using JConsole
This procedure shows how to monitor the Notepad Java application. Under releases of the Java SE platform prior to version 6, applications that you want to monitor with JConsole need to be started with the following option.
-Dcom.sun.management.jmxremote
However, the version of JConsole provided with the Java SE 6 platform can attach to any local application that supports the Attach API. In other words, any application that is started in the Java SE 6 HotSpot VM is detected automatically by JConsole, and does not need to be started using the above command-line option.
- Start the Notepad Java application, by using the following command in a terminal window:
java -jar jdk_home/demo/jfc/Notepad/Notepad.jar
Where jdk_home is the directory in which the Java Development Kit (JDK) is installed. If you are not running version 6 of the Java SE platform, you will need to use the following command:
java -Dcom.sun.management.jmxremote -jar jdk_home/demo/jfc/Notepad/Notepad.jar
Monitoring java virtual machine
This topic introduces the features and utilities that provide monitoring and management services to the Java Platform, Standard Edition (Java SE platform).
Key Monitoring and Management Features
The Java SE platform includes significant monitoring and management features. These features fall into four broad categories:
Java Virtual Machine Instrumentation
The Java Virtual Machine (Java VM) is instrumented for monitoring and management, enabling built-in (or ready-to-use ) management capabilities that can be accessed both remotely and locally.
The Java VM includes a platform MBean server and platform MBeans for use by management applications that conform to the Java Management Extensions (JMX) specification. These platforms are implementations of the monitoring and management API. The platform MXBeans and MBean servers are introduced in the Platform MXBeans and Platform MBean Server topics.
Monitoring and Management API
Java SE includes the following APIs for monitoring and management:
- java.lang.management : Enables monitoring and managing the Java virtual machine and the underlying operating system. The API enables applications to monitor themselves, and enables JMX-compliant tools to monitor and manage a virtual machine locally and remotely. This API provides access to the following types of information:
- Number of classes loaded and threads running
- Java VM uptime, system properties, and VM input arguments
- Thread state, thread contention statistics, and stack trace of live threads
- Memory consumption
- Garbage collection statistics
- Low memory detection
- On-demand deadlock detection
- Operating system information
Monitoring and Management Tools
The Java SE platform provides a graphical monitoring tool called JConsole . JConsole implements the JMX API, and enables you to monitor the performance of a Java VM and any instrumented applications. It provides information to help you optimize the performance.
Some of the enhancements in JConsole are as follows:
- JConsole plug-in support, which allows you to build your own plug-ins to run with JConsole. For example, you can add a custom tab for accessing the MBeans of the application.
- Dynamic attach capability allowing you to connect JConsole to any application that supports the Attach API.
- Enhanced user interface, which makes data more easily accessible.
- New Overview and VM Summary tabs for a better presentation of general information about your Java VM.
- HotSpot Diagnostic MBean, which provides an API to request heap dump at runtime and also change the setting of certain VM options.
- Improved presentation of MBeans to make it easier to access the MBeans operations and attributes.
JConsole is presented in detail in the Using JConsole topic.
Other command-line tools are also supplied with the Java SE platform.
Java Management Extensions Technology
The Java SE platform, release 17 includes the Java Management Extensions (JMX) specification, version 1.4. The JMX API allows you to instrument applications for monitoring and management. A remote method invocation (RMI) connector allows this instrumentation to be remotely accessible, for example, using JConsole.
See JMX technology documentation in the Java Platform, Standard Edition Java Management Extensions Guide .
The following sections provide a brief introduction to the main components of the JMX API.
What Are MBeans?
JMX technology MBeans are managed beans , namely Java objects that represent resources to be managed. An MBean has a management interface consisting of the following:
- Named and typed attributes that can be read and written.
- Named and typed operations that can be invoked.
- Typed notifications that can be emitted by the MBean.
For example, an MBean representing an application’s configuration can have attributes representing different configuration parameters, such as a CacheSize . Reading the CacheSize attribute will return the current size of the cache. Writing CacheSize updates the size of the cache, potentially changing the behavior of the running application. An operation such as save stores the current configuration persistently. The MBean can send a notification such as ConfigurationChangedNotification when the configuration changes.
MBeans can be standard or dynamic. Standard MBeans are Java objects that conform to design patterns derived from the JavaBeans component model. Dynamic MBeans define their management interface at runtime. An additional type of MBean, called MXBean, is added to the Java platform.
- A standard MBean exposes the resource to be managed directly through its attributes and operations. Attributes are exposed through getter and setter methods. Operations are the other methods of the class that are available to managers. All these methods are defined statically in the MBean interface and are visible to a JMX agent through introspection. This method is the most straightforward way of making a new resource manageable.
- A dynamic MBean is an MBean that defines its management interface at runtime. For example, a configuration MBean determines the names and types of the attributes that it exposes, by parsing an XML file.
- An MXBean is a type of MBean that provides a simple way to code an MBean that references only a predefined set of types. In this way, you can ensure that the MBean is usable by any client. It includes remote clients without any requirement that the client has access to model-specific classes, which represents the types of your MBeans. The platform MBeans are all MXBeans.
MBean Server
To be useful, an MBean must be registered in an MBean server. An MBean server is a repository of MBeans. Each MBean is registered with a unique name within the MBean server. Usually the only access to the MBeans is through the MBean server. In other words, code does not access an MBean directly, but rather accesses the MBean by the name through the MBean server.
The Java SE platform includes a built-in platform MBean server. See Using the Platform MBean Server and Platform MXBeans.
Creating and Registering MBeans
There are two ways to create an MBean. One is to construct a Java object that will be the MBean, then use the registerMBean method to register it in the MBean server. The other method is to create and register the MBean in a single operation using one of the createMBean methods.
The registerMBean method is simpler for local use, but cannot be used remotely. The createMBean method can be used remotely, but sometimes requires attention to the class loading issues. An MBean can perform actions when it is registered in or unregistered from an MBean server if it implements the MBeanRegistration interface.
Instrumenting Applications
General instructions on how to instrument your applications for management by the JMX API is beyond the scope of this document.
Platform MXBeans
A platform MXBean is an MBean for monitoring and managing the Java VM, and other components of the Java run-time environment. Each MXBean encapsulates a part of VM functionality such as the class loading system, just-in-time (JIT) compilation system, garbage collector, and so on.
Table 1-1 lists all the platform MXBeans and the aspect of the VM that they manage. Each platform MXBean has a unique javax.management.ObjectName for registration in the platform MBean server. A Java VM may have zero, one, or more than one instance of each MXBean, depending on its function, as shown in the table.
Table 1-1 Platform MXBeans