Welcome to github.com/hsfxuebao/j… , hope to help you, if you feel ok, please click on the Star
From: juejin. Cn/post / 684490…
In a word, the JDK’s own monitoring tools jConsole and JVisualVM make it easier and more intuitive to monitor the performance of Java applications. Here’s how to use them.
1 the introduction
The JDK’s command-line tools have been covered in previous articles (see “Related reading” below), but they are still relatively unintuitive to use and are generally needed on the machine. Is there a more convenient and intuitive way to monitor Java applications? In fact, the JDK already provides GUI tools for Java monitoring, namely JConsole and JVisualVM. The functions and usage of these two tools are described below.
2 use jconsole
Jconsole is a built-in Java performance analyzer in the JDK. In the bin directory of the JDK installation directory, you can run it from the command line (jconsole.exe) or double-click jconsole.exe. Readers are interested can refer to official jconsole tool using document: https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html
2.1 Two connection modes
When jConsole starts, it provides two connection modes: local process and remote process. It simply lists local Java processes to select from. To monitor remote Java processes, check remote processes and type:. This requires that JMX remote connection parameters be set when the remote Java process is started, otherwise it cannot be connected. You can refer to the official articles (https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html) parameters are the following:
# need to monitor the server IP - Djava. Rmi. The server hostname = 192.168.222.10 # provides monitoring the Java process of port - Dcom. Sun. Management jmxremote. Port = 9004 # Specify follow-up communication ports, in accordance with the above - Dcom. Sun. Management jmxremote. Rmi. The port = 9004 # does not use SSL login, if you have security requirements, Can be set up - Dcom. Sun. Management jmxremote. SSL = false # are not validated, if you have security requirements, can be set up - Dcom. Sun. Management jmxremote. Authenticate = false copy codeCopy the code
As shown below:
Note that since SSL is not used, the message “unsafe connection” will be displayed. Just click on it.
2.2 Using the JConsole Function
Once the Java process is started and connected, the JConsole interface is relatively compact, divided into six modules:
- Overview: An overview of Java processes, including a line chart of changes in heap, thread count, classes, and CPU usage.
- Memory: Displays heap and non-heap memory usage, similar to jmap and Jstat
- Threads: Displays thread usage information, similar to jStack
- Class: Displays class loading information
- VM Summary: Displays JVM information, similar to jInfo
- MBeans: Display MBeans information (rarely used)
2.2.1 overview
The overview mainly shows the heap, thread count, class, CPU usage change line chart, basically can directly view the application overview according to the line chart. If the heap is high, the number of active threads is high, and the CPU usage is high, you can go directly to the corresponding area to see the details to find the cause. In addition, right-click the corresponding graph to export the data to a CSV file for analysis. As shown in figure:
2.2.2 memory
Memory is the key area we monitor. You can look at heap memory, non-heap memory, memory pool health, GC times and times, and you can manually perform GC to see memory changes. The diagram below:
At the top of the graph, you can choose which memory changes to see (heap, non-heap, old, Eden, Survivor, Metaspace, etc.) or you can manually perform GC to see the changes. The bottom of the graph shows the size and usage of memory, GC frequency and usage time, as well as the changes of heap and non-heap in a bar chart. Therefore, for memory overflow, OOM these problems, see the monitoring data here is very suitable.
2.2.3 thread
For online applications, the main reasons for threads to pause for a long time are: waiting for external resources (database connection, network resources, device resources, etc.), dead loops, and lock waiting (live lock and deadlock). All of these need to monitor the running status of Java application threads, as shown in the following figure:
The number of threads change and click a thread to view the running status to analyze, which can also click “deadlock detection” function to deal with the deadlock problem.
2.2.4 class
This function is mainly used to see the total number of classes being loaded. If the number of classes being loaded is increasing, you need to see if your code has code that keeps producing classes. The diagram below:
2.2.5 VM profile
When we add a startup parameter (JAVA_OPTS) to a Java application, if we want to check the actual use of this application online, the parameter is effective, we use the command line tool jinfo, now you can see directly here, and includes system information, class information, heap information and related VM parameters. The diagram below:
3 jvisualvm use
Similar to JConsole, the JDK also provides jVisualVM tools in the bin directory. Jvisualvm is relatively more powerful and can be started from the command line (jVisualvm. exe) or directly double-click jVisualvm. exe under Windows. Readers are interested can refer to official jvisualvm tools using document: https://docs.oracle.com/javase/8/docs/technotes/guides/visualvm/index.html
3.1 Two connection modes
Like JConsole, JVisualVM can monitor local Java processes as well as remote Java processes. Local processes can be selected directly, while remote processes also require Java processes to add JMX startup parameters.
3.2 Use of JVisualVM functions
Jvisualvm has more powerful functions, including the following functions:
- This section describes how to display VM processes and their configuration and environment information (JPS and JINFO).
- Monitor the application’s CPU, GC, heap, method area (1.7 and older), meta-space (JDK1.8 and later), and thread information, equivalent to jmap,jstat, and jStack.
- Dump and analyze heap dump snapshots (JMAP, JHAT).
- Method-level program run performance analysis to find out which methods are called the most and run the longest.
- Offline program snapshot: collects program runtime configuration, thread dump, memory dump, and other information to create a snapshot
The functions on the interface are mainly divided into several modules, which are overview, monitoring, threads and sampler.
3.2.1 overview
Overview is similar to JPS and JInfo in Java command line tools, which can display processes and their configuration, system properties, startup parameters, etc., similar to jConsole’s “VM Profile “. The diagram below:
3.2.2 monitoring
Jmap-dump :file=./heap.hprof PID this function is equivalent to jConsole’s “overview” function, which also graphically displays CPU, heap changes, thread count changes, and loaded classes, but has a feature that can remotely dump snapshots of heap dumps (equivalent to jmap-dump :file=./heap.hprof PID). File storage location is selected during dump.
Dump heap snapshot, we can manually download the file, then use its “load snapshot” function to load into JVisualVM (select file type is “hprof” type when loading), further analysis of the heap memory condition. After loading, it contains the summary information and memory usage of the class and instance. Double-click the class to see the specific number of instances. If you find that the number of instances of some classes is too many or the memory usage is relatively high, you can know the problem. As follows:
3.2.3 thread
This function is equivalent to jConsole’s “thread” function, but it is richer. It displays the running status and running time of each thread in a graphical manner, and can also perform remote thread dump. This function is actually jstack-L function, once dumped, directly displayed in the interface. As follows:
3.2.4 sampler
Sampler is a unique function of JVisualVM, can sample CPU and memory display, every period of time to the memory information, thread information, can be very convenient to focus on the analysis of a period of time data changes (can be displayed in a hierarchical way down to the method execution time, class memory occupation, etc.), It also provides GC, memory dump, and thread dump functions. As follows:
4 summarizes
With jConsole and JVisualVM two visualization tools, you can reduce the command line input, in a more convenient, more intuitive way to monitor the Java application memory, thread, CPU and other information, is a good helper to deal with Java online problems. It is important to note, however, that you need to have some knowledge of the JVM, how Java programs work, and threads before using these tools. I hope this article has been helpful.
Parameter data
- Jconsole official usage documentation:
https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html
- Jvisualvm official usage documentation:
https://docs.oracle.com/javase/8/docs/technotes/guides/visualvm/index.html
- Jvisualvm document:
https://visualvm.github.io/documentation.html
- JMX remote monitoring technology:
https://docs.oracle.com/javase/8/docs/technotes/guides/management/agent.html
- Sample code address:
https://github.com/mianshenglee/my-example/tree/master/java-monitor-example
reading
- Java Application Monitoring (1)- Application monitoring techniques that Java programmers should know about
- Java Application Monitoring (2)- Secrets of Java commands
- Java Application Monitor (3)- Do you master these command line tools
- Java Application Monitoring (4)- Online problem detection routines