In 2016, I spent some time putting together some articles about the JVM, which I still look back on as incomplete, including how tools can be used to monitor performance changes before and after tuning. Tools as a graphical interface to show more intuitive found the problem, on the other hand some cost performance analysis (dump file analysis) generally will not directly in production analysis, often dump files up to 1 g, manual analysis efficiency is low, so the use of tools to analyze the JVM related problems, long can reach twice the result with half the effort.

JVM monitoring and analysis tools are generally divided into two categories, one is the JDK built-in tools, and one is a third-party analysis tool. JDK built-in tools are generally in the JDK bin directory, in the form of exe directly click can be used, including analysis tools have been very powerful, almost involved in all aspects, but we most often use only two: jconsole.exe and jVisualvm. There are many third-party analysis tools with different emphases, such as MAT(Memory Analyzer Tool) and GChisto.

For large JAVA applications, no amount of fine-grained testing can close all bugs, and even if we do a lot of good work in the testing phase, many of the problems are exposed in production and difficult to reproduce in the testing environment. The JVM’s ability to record some of the running state of the system at the time a problem occurs and store it in a Heap Dump file provides an important basis for problem analysis and diagnosis. Among them VisualVM and MAT are the analysis tools of dump file.

JDK built-in tools

jconsole

Java Monitoring and Management Console (Jconsole) is a Java Monitoring and Management Console built into the JDK from Java 5. It is used to monitor memory, threads, and classes in the JVM. JMX is a GUI performance monitoring tool based on Java Management Extensions (JMX). Jconsole uses the JVM’s extension mechanism to capture and present information about the performance and resource consumption of applications running in a virtual machine.

Click jconsole.exe directly in JDK /bin directory to start, the interface is as follows:

In the dialog box that is displayed, you can choose to monitor Java applications on the local server or remote Java services. To monitor remote services, add the following codes to the Tomcat startup script:

 -Dcom.sun.management.jmxremote.port=6969  

 -Dcom.sun.management.jmxremote.ssl=false  

 -Dcom.sun.management.jmxremote.authenticate=false

Once connected, you can see the JConsole overview diagram and the main features: Overview, memory, threads, classes, VM, MBeans

  • Overview, a graph showing heap memory usage, number of active threads, loaded classes, CPU usage line chart, you can very clearly observe the change in the process of program execution.

  • Memory, mainly shows the usage of memory, while you can view the heap and non-heap memory change value comparison, also can click on the Execute GC to punish GC execution

  • Thread, the main interface shows the active number and peak value of the number of threads. At the same time, click the thread in the lower left to view the detailed information of the thread, such as what is the state of the thread, stack content, etc. At the same time, you can also click “deadlock detection” to check whether there are deadlocks between threads.

  • Class, which displays information about loaded classes.

  • VM profile, which shows an overview of all INFORMATION about the JVM, including basic information, thread-related, heap-related, operating system, VM parameters, and so on.

  • Mbean, view Mbean properties, methods, and so on.

VisualVM

Introduction to the

VisualVM is a tool that provides a visual interface for viewing the details of Java technology-based applications (Java applications) running on the Java Virtual Machine (JVM). VisualVM organizes the DATA related to JVM software retrieved by Java Development Kit (JDK) tools and provides this information in a way that allows you to quickly view data about multiple Java applications. You can view data about local applications as well as applications running on remote hosts. In addition, you can capture data about JVM software instances and save that data to your local system for later viewing or sharing with other users.

VisualVM is javajDK comes with the most awesome tuning tool, it is also I usually use the most tuning tool, almost involved in all aspects of JVM tuning. Similarly, double-click JVisualvm. exe in JDK /bin directory to use it. After starting up, you can choose local and remote as well as JConsole. If you need to monitor remote, you also need to configure related parameters.

VisualVM can install different plug-ins as needed, each with a different focus, some mainly monitoring GC, some mainly monitoring memory, some monitoring threads, etc.

How to install:

1. Choose Tools > Plug-ins from the main menu. 2. On the Available Plug-ins TAB page, select the Install check box of the plug-in. Click Install. Step by step, complete the plug-in installation program.

Here I take Eclipse(PID 22296) as an example. Double-click it and expand it directly. The main interface displays two large pieces of content: system and JVM.

Because there are so many plug-ins for VisualVM, I will introduce three that I mainly use: monitoring, threading and Visual GC

The home page of the monitor is essentially a chart of CPU, memory, classes, and threads

Threads and JConsole functionality are not much different

Visual GC is a frequently used feature that allows you to clearly see the memory changes of the young generation and the old generation, as well as the GC frequency, GC time, etc.

In fact, jconsole almost also has the above functionality, VisualVM is more comprehensive and more intuitive, in addition to the VisualVM very many other features, can analyze the dump memory snapshot, dump out of the thread snapshot and analysis, and many other plug-ins you can explore

Third-party tuning tools

MAT

What is MAT?

The MAT(Memory Analyzer Tool), an Eclipse-based Memory analysis Tool, is a fast, feature-rich Java Heap analysis Tool that helps us find Memory leaks and reduce Memory consumption. Use the memory analysis tool to analyze from many objects, quickly calculate the size of the object in memory, see who is blocking the garbage collector’s recycling work, and can intuitively view the possible result of the object through the report.

Memory leak analysis is often considered a difficult task and is usually performed by senior members of the team. However, MAT (Eclipse Memory Analyzer) is considered a “dumb-ass” heap dump file analysis tool that can generate professional analysis reports with just a click of the mouse. Compared to other memory leak analysis tools, MAT is very easy to use, and can be almost one-click, even novice users can quickly get used to it.

MAT is installed in the form of Eclipse plug-in, the specific installation process is not described, you can use VisualVM or jmap command to produce heap file, import eclipse MAT to generate analysis report:

Zip, dump_Leak_Suspects. Zip, and dump_Top_Components. Zip are the same directories as the dump file. Easy to send to relevant colleagues to view.

Focus on the following Actions, Reports, Step by Step areas:

  • Histogram: Lists the number and size of objects in memory, and supports regular expression search. Also, the retained size of all objects can be calculated

  • Dominator Tree: Lists the largest objects and their surviving dependencies (size is ordered by Retained Heap).

  • Top Consumers: Lists the largest object by graph

  • Duplicate Classes: Detects classes loaded by multiple class loaders

  • Leak Suspects: Memory Leak analysis

  • Top Components: Lists reports that are greater than 1 percent of the total heap.

  • Component Report: Analysis objects belong to the same package or are loaded by the same classloader

This is just a preliminary introduction, but MAT also has more powerful uses, such as comparing heap memory. In production environments, it often dumps snapshots of memory every few minutes to locate problems, and then compares changes in heap memory at different times to find problems.

GChisto

GChisto is a professional gc log analysis tool, you can analyze gc logs: Minor GC, full GC time, frequency, etc., through lists, reports, charts and other different forms to reflect the GC situation. The interface is a little rough, but the functionality is good.

After configuring the local JDK environment, double-click gchisto.jar and select gC.log from the input box that pops up by clicking Add

  • GC Pause Stats: You can view the number of GC, GC time, GC overhead, maximum GC time, minimum GC time, and corresponding bar charts

  • GC Pause Distribution: Displays the detailed Distribution of GC pauses. On the X-axis, the garbage collection Pause times and on the Y-axis, the number of pauses.

  • GC Timeline: Displays garbage collection for the entire Timeline

However, the tool is no longer maintained and does not recognize the latest JDK log files.

gcviewer

GCViewer is also an analysis gadget for visualizing garbage collector logs generated by Sun/Oracle, IBM, HP, and BEA Java virtual machines. GCViewer personally feels that the interface displayed is a bit messier than GChisto.

GC Easy

This is a Web tool that is very easy to use online.

Address: http://gceasy.io

Go to the official website and upload a packed zip or gz file. You will get the analysis results in a few minutes.

It is recommended to use this tool for GC analysis.

Jinjiu Yinshi recruitment season is coming soon, I recommend a public account, this public account mainly push Java interview related content, interested students can follow.