Overview of monitoring and management principle in Java

This article is the first article in the Java performance analysis, monitoring and optimization series. It was originally intended to introduce Java performance analysis methods and popular monitoring tools systematically, but after writing, I realized that it was too superficial to introduce only analysis methods and monitoring tools. If you can only use a tool without knowing the implementation principle behind it, you always feel a strange feeling. I think you are the same, so there is more this article.

Article catalog

Java SE monitoring management function

This article introduces the monitoring and management technology provided by Java Standard Edition (Java SE) platform - JMX (Java Management Extensions) technology.

The Java SE platform itself provides practical function modules for monitoring and managing services, which are mainly divided into the following four categories according to functions:

  • Java monitoring and Management API
  • Java virtual machine detection
  • Java Management Extension Technology (JMX)
  • Java monitoring and management tools

This article will introduce the relevant knowledge of these four parts in order to understand the relevant functions of Java SE monitoring and management and have an understanding of the relevant concepts.

Java monitoring and Management API

Java SE contains APIs for monitoring and management (java.lang.management). Through these APIs, application self-monitoring can be realized. This API mainly provides access to the following information:

  • Class loading related.
  • JVM related, such as runtime, system environment variables, user input parameters.
  • Thread related, such as thread status, thread statistics, thread stack, etc.
  • Memory usage.
  • GC status.
  • Deadlock detection.
  • Operating system information.

The following figure shows Java. Net in Java 17 Management module.

java.lang.management

JConsole draws the monitoring interface version by accessing the data provided by these management API s.

Java virtual machine monitoring

As mentioned above, out of the box monitoring and management functions have been built in Java SE. Through these functions, the self-monitoring of programs can be realized. Java has realized the monitoring of relevant information of Java virtual machine by default. Some contents that can be monitored by API are also listed in the part of Java monitoring and Management API. So how to use them?

The following is a simple example to demonstrate how to obtain system information, compiler information, memory information and garbage collector information through the Monitoring Management API.

package com.wdbyte;

import java.lang.management.CompilationMXBean;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryManagerMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.OperatingSystemMXBean;
import java.util.List;
import java.util.stream.Collectors;

public class JavaManagement {

    public static void main(String[] args) {
        OperatingSystemMXBean operatingSystemMXBean = ManagementFactory.getOperatingSystemMXBean();
        String osName = operatingSystemMXBean.getName();
        String osVersion = operatingSystemMXBean.getVersion();
        int processors = operatingSystemMXBean.getAvailableProcessors();
        System.out.println(String.format("Operating system:%s,edition:%s,Processor:%d individual", osName, osVersion, processors));

        CompilationMXBean compilationMXBean = ManagementFactory.getCompilationMXBean();
        String compilationMXBeanName = compilationMXBean.getName();
        System.out.println("Compiling system:" + compilationMXBeanName);

        MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
        MemoryUsage heapMemoryUsage = memoryMXBean.getHeapMemoryUsage();
        long max = heapMemoryUsage.getMax();
        long used = heapMemoryUsage.getUsed();
        System.out.println(String.format("Memory used:%dMB/%dMB", used / 1024 / 1024, max / 1024 / 1024));

        List<GarbageCollectorMXBean> gcMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
        String gcNames = gcMXBeans.stream()
            .map(MemoryManagerMXBean::getName)
            .collect(Collectors.joining(","));
        System.out.println("Garbage collector:" + gcNames);
    }
}

The runtime specifies a memory of 100MB (- Xms100M -Xmx100M), and the following results are obtained.

Operating system: Mac OS X,Version: 11.6,Processor: 12
 Compiling system: HotSpot 64-Bit Tiered Compilers
 Memory used: 2 MB/100MB
 Garbage collection: G1 Young Generation,G1 Old Generation

Note: tip

A closer look at the code shows that many of these classes end with mxbeans. What does this mean?

:::

Java Management Extension Technology (JMX)

In the code example of Java virtual machine monitoring, you can see many classes named at the end of MXBean. JMX (Java Management Extensions) technology has been involved here.

JMX technology provides a simple and standard way to manage resources, such as operating system, virtual machine information, memory state, thread information, etc. These are collectively referred to as managed resources. Moreover, JMX can be dynamic, so JMX technology can be used to monitor and manage various resources. You can use JMX technology to monitor the state of Java virtual machine, or you can use JMX technology to build your own resources to be managed.

Is JMX technology as simple as resource definition? no, it isn't. JMX specifies the way of resource definition, the way of resource management, the architecture of monitoring and management, the design pattern of specific implementation, the related API s of monitoring and management, and the remote monitoring service (RMI) for the network in Java. This series of functions are collectively referred to as JMX technology. Is a standard part of the Java SE platform.

Resource management has been mentioned many times above, so how to define a resource? JMX technology gives the architecture and design pattern of resource definition. In JMX, a Java object called MBean or MXBean is defined to represent the specified resources to be managed. The Java class name of resource definition must end with MBean or MXBean.

The following figure shows the resource definition classes ending with MXBean in Java 17. You can see what resources each class represents through naming.

Mxbeans in Java

This article mainly introduces the monitoring and management functions in Java SE, so that everyone can have a specific understanding of the principles and concepts behind monitoring and management in Java. Therefore, the specific design and implementation methods of MBean and MXBean are not the focus of this article. There is no more introduction here, which will be introduced in the next independent JMX technology article.

Java monitoring and management tools

JMX technology mentioned that JMX not only provides monitoring and management API s, but also provides services for network remote management. You can use JMX related monitoring and management tools to remotely connect to the running Java virtual machine through the network and monitor its running state. jconsole integrated in Java is such a tool.

Start a java program that can run continuously locally as the monitored object. If you have configured the Java environment variables, you can start the tool directly through jconsole.

 $ jconsole

After startup, jconsole has listed the local running Java programs and selected the ones you want to monitor.

Jconsole interface

After the connection is successful, you can see the resource usage of the current Java process.

JConsole monitoring

In the MBean page, you can see the details of various defined resources.

Jconsole MBean

Jconsole is a powerful graphical interface JMX management tool. It can not only connect local Java programs, but also monitor the running status of remote Java programs through the network. However, it is not the focus of this article and is not described in detail.

reference resources:

  • https://docs.oracle.com/en/java/javase/17/jmx/
  • https://docs.oracle.com/en/java/javase/17/management/

---- END ----

That's all for this article. I'm Alan.

Added by jmurch on Mon, 13 Dec 2021 05:04:35 +0200