tags: java, troubleshooting, monitor
Summarized in one sentence: java application monitoring, and why? What is monitoring? How to monitor? This article answers for you.
1 Introduction: Why do we need to monitor java application
java developers know, start the java application using the java
( class
file) or java -jar
( jar
or war
package) command. The java
command is actually start a java virtual machine ( JVM
), the program is running JVM
on, JVM
the CPU is responsible for loading classes, regions, etc. runtime stack allocation, and stack objects and threads are used in the program were affected system and memory, if the procedure involves files or data read and write, but also affect IO system. Therefore, after a java application starts, it does not take up resources if the monitoring, no doubt in the plane took off, but there is no dashboard, this aircraft is estimated that no one dared to sit. Therefore, as a developer, you have to understand the operation of the application after the start, and the ability to monitor the health of applications, so in order to timely predict issues that may occur in a timely manner or faster correction after the occurrence of the problem, better find out why where, then solve. The good news is that the program itself, java tools and third-party tools, provides a number of methods to monitor java application for us, therefore, as a java developer, it is necessary to make them understand a system to facilitate the practical application ( especially in a production environment) is more calm, handle problems more efficiently.
2 What monitoring
A java application up and running, if problems arise, our first reaction is certainly view the log, look specifically report what is wrong, if there is no error, just run very slowly, or not being given just yet, then we need to monitor what it ? In this section, we usually use the comparison of the operating system to look for java applications need to monitor what.
2.1 System Monitoring content
We usually use the operating system, such as windows
, linux
systems, applications appear slow to open, slow system response, is the first general view cpu and memory usage to run the system, identify high resource-intensive process, positioning reasons, windows
directly in 任务管理器-->性能
the View, as shown below.
In linux
, can be used top
, free
, df
, iostat
commands to view, as in FIG.
In the daily operation and maintenance process, the situation is also using the system disk, IO, CPU, memory, network monitoring, etc., for timely detection of high-footprint process, locate and then deal with the problem.
2.2 java application monitoring content
With respect to the contents of the operating system, java applications need to be monitored also have a similar place, cpu situation as occupied memory conditions, which will include JVM parameters java application heap occupancy, thread running status, class loading, the garbage collection ( GC
)Happening. As to why the need to monitor the content, you need java virtual machine (JVM) have a certain understanding, due to the JVM more involved in the content of this article, the back of the JVM have the opportunity to explain in detail, where java JVM architecture can make a brief introduced to facilitate the reader's understanding of the later chapters. See below:
-
When running a java application, it will generate a JVM instance, while the java application is running on this JVM instance, when an application exits, JVM instance will be shut down. Launch multiple java applications, will launch multiple JVM instances, they do not affect each other (of course, they will take up system resources).
-
There are three major virtual machine module, a class loader subsystem (
Class Loader Subsystem
responsible for loading classes), an execution engine (Execution Engine
responsible for carrying out the method of instruction and class garbage collection), a run-time data area (Runtime Data Areas
data storage program is responsible for run-time) . -
Where runtime data divided into the method area (memory such as class information, method information, references, constant pool, etc.), the stack (storage class instance object and arrays), Java stack (in the stack mode is stored in units of frames stored threads running state frame), native method stacks (with associated local method of data area), the program counter (each thread has its own program counter indicates a command to be executed under the "address").
-
java application startup process is loaded by the loading associated Classes subsystem, and the data information such as classes, methods and the like stored in the stack area method, examples of related classes, and the instance of the object stored on the heap, running location is specified using a counter for each thread. Heap and method area is shared by the threads, the program counter and Java thread stack is private.
-
Run-time data area is the region java application run-time monitoring, in which case each region of memory, especially memory usage of the heap, are key areas. Heap will divide the young generation, old generation and
Metaspace
the garbage collector will be generational recovery. Through its recovery monitoring, it can detect memory leaks, and java stack is related to threads, running state of the thread and associated with the CPU, so java stack monitoring may know the CPU utilization is too big a problem, but the method area and java stack size is also an indicator of memory for monitoring.
3 how to monitor
After the above description, we probably already know a java application after the start, why do we need to monitor java application and what things need to be monitored. That into practical applications, how to monitor it? By following a overview of the java application monitoring tools and methods, follow-up will be in the form of a series of articles on various monitoring tools and methods in detail.
Press monitoring tool monitoring methods, divided into the following four categories:
- App - log and monitor page
- java comes with command-line monitoring tools
- java comes with visual monitoring tool
- Third-party diagnostic tools
3.1 Built-monitoring program
Built-in monitoring procedure is relatively simple, beginner in java, the most common is to use the System.out.println()
content you want the output to output, in the development stage it was also like this, one can export business content, the normal function of monitoring whether or not the other aspects may output system properties System.getProperties()
and System.getProperty()
. Of course, more usage log output frame, and outputs it to the log file by level, such as log4j
and logback
. For Spring Boot
applications, it can also be used actuator
to monitor the operation of the program. tomcat
Container itself with monitoring page. The main features of such monitoring program is built, and monitored by the log output, the developer is relatively familiar, in general, in the development and testing phase relatively common, but in a production environment, the log is generally error
level or because of security concerns, comes Some monitor pages is likely to shut down. Accordingly, such monitoring not elaborate.
3.2 java comes with command-line monitoring tools
Jdk bin directory under the installation directory, and already offers a variety of command-line monitoring tools to monitor java application developers and for operation and maintenance personnel, and facilitate the development and operation and maintenance personnel to diagnose the problem, therefore, these tools java application is an important means of monitoring. In general, common command-line tools include jps
, jinfo
, jmap
, jstack
, jstat
, wherein
jps
View java process IDjinfo
Check and adjust the parameters of the virtual machinejmap
View Heap (heap) and the use of raw heap snapshotjstack
View thread running state and generates a snapshot of the threadjstat
Show the process of class loading, operating data memory, garbage collection and so on.
With these tools, you can basically understand the change in the state memory java application thread running status and other information, and then provide the basis for application monitoring and problem diagnosis. Future articles will these tools be described in detail.
3.3 java own visual monitoring tools
In addition to command-line tool in the windows platform, jdk also provides visual monitoring tool, more intuitive, more convenient way to monitor the health of java applications. Both tools are jconsole
and jvisualvm
can be found in the bin directory of jdk. They can monitor local and remote java applications, including heap usage, threads, cpu use, class loading situation, gc circumstances, jvisualvm
can also generate the corresponding heap and thread snapshots, but also can use the corresponding plug-in in order to further analysis. Future articles will use these two tools are described in detail.
3.4 third-party diagnostic tools
In addition to java own tools, some third-party tools are also monitoring and diagnostic analysis, performance tuning tool, including MAT
, BTrace
and Arthas
. among them
MAT
Iseclipse
a memory analysis plug-ins, byMAT
may be made to dump the heap snapshot analysis, and assisted analysis memory leak causes, quickly compute the recovery operation of the occupied size of the object in memory, the garbage collector, and by statements intuitive see an object that may cause such results.BTrace
Yes sun introduced a Java dynamic, secure tracking (monitoring) tool, you can monitor the operation of the system in case of non-stop, and do the least invasive, occupy minimal system resources. Especially suitable for monitoring of java application in a production environment, troubleshooting.Arthas
Ali is a Java open source online diagnostic tool, can also include memory, the thread situation, GC, the runtime data in case of non-stop monitoring system can also monitor the method parameters, return values, exception return data, called artifacts, very easy to use in a production environment.
Future articles will use three of these third-party tools are described in detail.
4 Summary
Hope that through this series of articles, so that we can understand the application of monitoring technology java, familiar with the various monitoring tools, and in order to face the problems encountered online (especially performance problems, oom problems, etc.), could easily deal with.
Reference material
- JVM specification:
https://docs.oracle.com/javase/specs/jvms/se8/html/index.html
- JDK Tools Reference document:
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
- Books: "In-depth understanding of the Java Virtual Machine -JVM advanced features and best practices" - Zhou Zhipeng