tags: java, troubleshooting, monitor


In a word: Java application monitoring, why? Monitoring what? How is it monitored? This article answers for you.

Introduction: Why monitor Java applications

Java developers know that the Java (class file) or java-jar (jar or WAR package) commands are used to launch Java applications. And the Java command is actually start a Java virtual machine (JVM), program is running on the JVM, the JVM is in charge of class loading, run-time stack allocation area, etc, and stack for application objects and threads in use, respectively affect the system’s CPU and memory, if the program involves the file or data reading and writing, can also affect the system IO. So if a Java application starts up and doesn’t monitor the resources it consumes, it’s no doubt that an airplane takes off without a dashboard that no one would dare to fly on. Therefore, as a developer, it is necessary to know the running condition of the application after it is started, and be able to monitor the running condition of the application. In this way, the problems that may occur can be predicted and corrected in time, or problems can be found faster and better, and then solved. The good news is that programs themselves, Java tools, and third-party tools all provide us with many ways to monitor Java applications, so as Java developers, it is necessary to have a systematic understanding of them so that we can handle problems more easily and efficiently in practical applications, especially in production environments.

2 What to monitor

When a Java application is running and there is a problem, our first reaction is to check the log to see what the error is. If there is no error, but it is running slowly, or it has not been reported yet, what do we need to monitor? In this section, we’ll take a look at what we need to monitor for Java applications compared to what we normally do with the operating system.

2.1 System monitoring content

For the operating system we usually use, such as Windows and Linux, when applications open slowly and the system responds slowly, it is generally necessary to check the CPU running and memory usage of the system, find out the process that occupies high resources, and locate the cause. In Windows, directly check the task Manager -> Performance, as shown in the following figure.

On Linux, you can run the top,free,df, and iostat commands to view information, as shown in the following figure.

During daily operation and maintenance, the system monitors disk usage, I/O, CPU, memory, and network to identify processes that occupy high resources and troubleshoot problems.

2.2 Java Application monitoring Content

Java applications need to monitor similar things to the operating system, such as CPU usage, memory usage, which includes JVM parameters of Java applications, heap usage, thread health, class loading, garbage collection (GC) status. As to why you need to monitor these things, you need to have some understanding of the Java Virtual Machine (JVM). Since the JVM covers a lot of content in this article, I will have the opportunity to explain the JVM in detail later. Here is a brief introduction to the Java JVM architecture to help readers understand the following chapters. See below:

  • When Java runs an application, an instance of the JVM is generated, and the Java application runs in that JVM instance. When the application exits, the JVM instance is shut down. Starting multiple Java applications also starts multiple JVM instances, and they don’t affect each other (of course, they all take up resources on the system).

  • The VIRTUAL machine has three main modules, a Class Loader Subsystem (to load the classes), an Execution Engine (to execute the method instructions and garbage collection of the Class), A Runtime Data area (which is responsible for storing Data when a program is running).

  • The runtime data is divided into method area (storage such as class information, the method information, references, constant pool, etc.), heap (storage class instance objects and arrays), Java stack (stored in the stack way to frame for the unit to save the thread running state of the frame), local method stack (data area associated with local method), the program counter (each thread has its own program counter, Represents the “address” of the next instruction to be executed.

  • Java application startup process is to load the relevant classes through the class loading subsystem, and then the relevant data such as class information, methods, etc., in the stack of the method area, instantiate the relevant classes, while the instance object is stored in the heap, the program running position is specified by each thread using counters. The method area and heap are shared by threads, and the program counter and Java stack are thread private.

  • The runtime data area is the monitoring area of the Java application runtime, in which the memory of each area, especially the memory usage of the heap, is the key area. The heap is also divided into young generation, old generation, and Metaspace, and the garbage collector does generational collection. Through its recycling monitoring, can detect whether there is a memory leak, and the Java stack is related to the thread, the running state of the thread is related to the CPU, so the monitoring of the Java stack can know the problem of excessive CPU usage, while the method area and the memory occupied by the Java stack is also a monitoring indicator.

3 Monitoring

From the above description, we have a pretty good idea of why and what we need to monitor a Java application once it starts. Then put into practice, how to carry out monitoring? Following is an overview of Java application monitoring tools and methods, which will be described in detail in a series of articles.

According to the monitoring methods of monitoring tools, it is mainly divided into the following four categories:

  • Built-in – log and monitor page
  • Java comes with command-line monitoring tools
  • Java comes with visual monitoring tools
  • Third-party diagnostic tools

3.1 Program built-in monitoring

Program built-in monitoring is relatively simple, in the beginning of Learning Java, the most commonly used is to use system.out.println () to output the content you want to output, in the development stage, some people like this, on the one hand, can output business content, monitoring the normal function, On the other hand, you can output System properties system.getProperties () and System.getProperty(). Of course, more and more logging frameworks are now used for output, and logs are exported to files by level, such as log4j and Logback. For Spring Boot apps, it can also use the Actuator to monitor the application. The Tomcat container itself also comes with a monitor page. This type of monitoring is mainly characterized by built-in program and monitoring through log output. Developers are relatively familiar with it, and it is commonly used in the development and testing stage. However, in production environment, logs are usually at error level or some monitoring pages may be closed due to security concerns. Therefore, such monitoring will not be described in detail.

3.2 Java has its own command line monitoring tool

The bin directory in the JDK installation directory provides various command-line monitoring tools for developers and O&M personnel to monitor Java applications and diagnose problems. Therefore, such tools are an important means of Java application monitoring. In general, the commonly used command line tools including the JPS, jinfo, jmap, jstack, jstat, among them

  • jpsView the ID of the Java process
  • jinfoView and adjust VM parameters
  • jmapView heap usage and generate heap snapshots
  • jstackView thread running status and generate thread snapshot
  • jstatShows the running data in the process, such as class loading, memory, garbage collection, etc.

Through these tools, you can basically understand the Java application memory change status, thread running status and other information, and then provide a basis for application monitoring and problem diagnosis. These tools will be described in detail in future articles.

3.3 Java provides visual monitoring tools

In addition to command-line tools, the JDK also provides visual monitoring tools for monitoring Java application health in a more intuitive and convenient way on Windows platforms. The two tools, jConsole and JVisualVM, can be found in the bin directory of the JDK. They can monitor local and remote Java applications, including heap usage, thread usage, CPU usage, class loading, GC, jVisualVM can also generate the corresponding heap and thread snapshot, and can also use the corresponding plug-in for further analysis. The use of these two tools will be described in more detail in a later article.

3.4 Third-party diagnostic tools

In addition to Java’s native tools, third-party tools for monitoring and analyzing diagnostics and performance tuning are available, including MAT, BTrace, and Arthas. Among them

  • MATiseclipseMemory analysis plug-in, throughMATThe dump heap snapshot can be analyzed to assist in the analysis of the cause of memory leakage, quickly calculate the size of the object in memory, garbage collector’s recycling work, and can intuitively view the objects that may cause such a result through the report.
  • BTraceSun is a Java dynamic, secure tracking (monitoring) tool that can monitor system performance without downtime, with minimal intrusion and use of system resources. Especially suitable for Java application monitoring and troubleshooting in production environment.
  • ArthasIs ali open source online Java diagnostic tool, can also monitor the system without shutdown, including memory, thread, GC, runtime data, can also monitor method parameters, return value, abnormal return and other data, can be called a magic tool, in the production environment is very convenient to use.

The use of these three third-party tools will be described in more detail in the following articles.

4 summarizes

I hope that through this series of articles, you can have an understanding of Java application monitoring technology, familiar with a variety of monitoring tools, in order to meet online problems (especially performance problems, OOM problems, etc.), can calmly face and deal with.

The resources

  • The JVM specification:https://docs.oracle.com/javase/specs/jvms/se8/html/index.html
  • JDK tools reference documentation:https://docs.oracle.com/javase/8/docs/technotes/tools/unix/
  • JAVA Virtual Machine Architecture:https://www.cnblogs.com/java-my-life/archive/2012/08/01/2615221.html
  • Book: In-depth Understanding of the Java Virtual Machine – Advanced FEATURES and Best Practices of the JVM – Zhipeng Zhou