Overview of monitoring and management principle in Java

Like and see again, unlimited power. Hello world:) wechat search Program ape Alan 」.

this paper Github.com/niumoo/JavaNotes and Program ape Alan blog Has been included, there are many knowledge points and series of articles.

The current article belongs to Java performance analysis and optimization Series of articles, click to view all articles.

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. However, after writing, I realized that it was too superficial to introduce only analysis methods and monitoring tools. If I can only use a tool without knowing the implementation principle behind it, I always feel strange. I miss you It's the same with you, so there's more of this article.

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 the java.management module in Java 17.

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. JMX technology can be used to monitor the state of Java virtual machine or JMX technology Build your own resources to manage.

Is JMX technology as simple as resource definition? No. JMX specifies the way of resource definition, resource management, monitoring and management architecture, specific implementation design patterns, monitoring and management related API s and remote monitoring service (RMI) for network in Java. These functions are collectively referred to as JMX technology. It is a standard part of Java SE platform.

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 resource 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.

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.

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

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

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:

The current article belongs to Java performance analysis and optimization Series of articles, click to view all articles.

The article is constantly updated. You can search it through wechat Program ape Alan "Or visit" Program ape Alan blog "Read this article for the first time Github.com/niumoo/JavaNotes It has been included. There are many knowledge points and series articles. Welcome Star.

Tags: Java

Posted on Mon, 29 Nov 2021 21:31:34 -0500 by rhiza