“This is the fourth day of my participation in the Gengwen Challenge. For details, see” Gengwen Challenge “.

The first part introduced the JVM monitoring and diagnostic tools – command line next part, next to give you a brief introduction to the JVM monitoring and diagnostic tools -GUI first part.

I. Overview of tools

Using the command-line tools or combinations from the previous chapter can help you get basic information about the performance of your target Java application, but they have the following limitations:

  1. Methot-level analysis data, such as the call relationships between methods, how many times each method was called, and when it was called, are not available (which is critical to locating application performance bottlenecks).
  2. Requiring the user to log in to the host of the target Java application is not very convenient to use.
  3. The analysis data is output through the terminal, the result is not intuitive.

To this end, the JDK provides some memory leak analysis tools, such as JConsole, JVisualVM, etc., to assist developers in locating the problem, but these tools are often not sufficient for quick locating. So the tools we introduce here are relatively more and richer.

Graphical comprehensive diagnostic tool

Tools that come with the JDK

  1. Jconsole: Visual monitoring tool that comes with the JDK. View a running overview of your Java application, monitor heap information, permanent-area (or meta-space) usage, class loading, and more. Location: the JDK \ bin \ jconsole. Exe
  2. Visual VM:Visual VM is a tool that provides a Visual interface for viewing detailed information about Java technology-based applications running on the Java Virtual Machine. Location: the JDK \ bin \ jvisualvm ext
  3. JMC:Java Mission Control, built-in Java Flight Recorder. Java virtual machine performance data can be collected at very low performance overhead.

Third party tools

  1. MAT: MAT (Memory Analyzer Tool) is an Eclipse-based Memory analysis Tool. It is a fast, rich Java Heap analysis Tool that can help us find Memory leaks and reduce Memory consumption, in the form of a plug-in for Eclipse.
  2. JProfiler: Commercial software, for a fee. Powerful. Similar to VisualVM
  3. Arthas: Alibaba’s open source Java diagnostic tool. Loved by developers.
  4. Btrace: Java runtime tracing tool. Information such as specified method calls, constructor calls, and system memory can be tracked without downtime.

Second, the jConsole

The basic concept

  1. Starting with Java5, the Java Monitoring and administration console is shipped with the JDK.
  2. It is a GUI performance monitoring tool based on Java Management Extensions (JMX) that monitors memory, threads, and classes in the JVM.
  3. The official tutorial: docs.oracle.com/javase/7/do…

Start the

  1. In JDK /bin, run the jconsole.exe command
  2. You do not need to use the JPS command to query

Third, Visual VM

The basic concept

  1. Visual VM is a powerful all-in-one Visual tool for troubleshooting and performance monitoring.
  2. It integrates multiple JDK command-line tools, using Visual VM can be used to display virtual machine process and process configuration and environment information (JPS, JINFO), monitor application CPU, GC, heap, method area and thread information (JSTAT, JStack), even replace JConsole.
  3. After JDK 6 Update 7, VisualVM was released as part of the JDK (VisualVM is in the JDK/bin directory), that is: it is completely free. In addition, the Visual VM can also be used as a independent software installation: visualvm. Making. IO/index. HTML

Plug-in installation

  1. One of the main features of Visual VM is that it supports plug-in extensions and is very easy to install plug-ins. We can either download the Plugin file *.nbm offline and then add the downloaded Plugin under the Downloaded page of the Plugin dialog box. Can also be under the available plug-ins page, plug-in address: visualvm. Making. IO/pluginscent…
  2. IDEA Install VisualVM Launcher plug-in Preferences –> Plugins –> Search VisualVM Launcher, install and restart.

The main function

  1. Generate/read a heap memory snapshot
  2. View JVM parameters and system properties
  3. View running VM processes
  4. Generate/read thread snapshots
  5. Real-time monitoring of program resources

Four, the eclipse MAT

Basic overview

  1. MAT (Memory Analyzer Tool) is a powerful Java heap Memory Analyzer. Can be used to find memory leaks and to see memory consumption.
  2. MAT is developed on the basis of Eclipse and can be used not only alone but also as a plug-in embedded in Eclipse. Is a free performance analysis tool, very convenient to use. You can be in www.eclipse.org/mat/downloa…

Get the heap dump file

  1. Dump file contents
  2. Get dump files

Method 1: Use the jMap tool described in the previous chapter to generate dump files of any Java process.

Method 2: Generate it by configuring JVM parameters. Options “- XX: + HeapDumpOnOutOfMemoryError” or “- XX: + HeapDumpBeforeFullGC” options “- XX: + HeapDumpPath” represented by the meaning is when there is OutofMemory program, A dump file is generated in the corresponding directory. If “-xx :HeapDumpPath” is not specified, the dump file is generated in the current directory.

Comparison: The jMAP +MAT tool is the most common combination given that it is almost impossible to analyze it online in a production environment and most of the analysis is done offline.

Method three: With VisualVM, you can export the heap dump file

Method 4: Use MAT to either open an existing heap snapshot or use MAT to export the heap snapshot directly from an active Java program. This feature lists currently running Java processes with the help of JPS for selection and snapshot.

Analyze the heap dump file

histogram

MAT’s histogram, like jMap’s -histo subcommand, shows the number of instances of each class and the total Shallow heap of those instances. However, MAT’s histogram also counts Retained heap and supports a ranking based on number of instances or Retaiined heap (Shallow heap by default).

In addition, MAT can group the classes in the histogram by superclass, classloader, or package name.

When a Class is selected, the Inspector window in the upper left corner of the MAT interface displays information about the Class instance of that Class, such as the Class loader.

Shows the number of instances of each class and the sum of their Shallow heap or Retainedheap

thread overview

  1. Look at the Java threads in your system
  2. View information about local variables

Gets the relationship between objects that reference each other

  1. with outgoing references
  2. with incoming references

Shallow heap and deep heap

shallow heap

  1. Shallow Heap is the amount of memory consumed by an object. In a 32-bit system, an object reference takes 4 bytes, an int takes 4 bytes, a long takes 8 bytes, and each object header takes 8 bytes. Depending on the heap snapshot format, the size of the object may be aligned to 8 bytes.
  2. Take a String as an example: two int values contain eight bytes, an object reference contains four bytes, and the first eight bytes of an object are 20 bytes in total, aligned to eight bytes, so it contains 24 bytes. () in the jdk7
  3. This 24 bytes is the shallow heap size of String. Regardless of the actual value of the String, the shallow heap size is always 24 bytes, regardless of the length of the String. Okay

retained heap

  1. Retained Set: the Retained Set of object A refers to the collection of all objects (including A) that can be released when A is garbage collected. The Retained Set of object A can be considered as A collection of all objects that can only be accessed directly or indirectly through object A. Colloquially, this is A collection of objects held only by object A.
  2. Retained Heap: A deep Heap is the sum of the shallow Heap sizes of all objects in the Retained set of an object.
  3. Note: Shallow heap refers to the memory occupied by the object itself, not the size of its internal reference objects. The deep heap of an object is the sum of the shallow heap of all objects that can only be accessed (directly or indirectly) through the object. It is the real space that can be freed after the object is reclaimed.

Actual object size

  1. The actual size of an object is defined as the sum of the shallow heap sizes of all the objects that an object can touch. It may seem like this is more intuitive and accepted in everyday development than deep heaps, but in reality, this concept has nothing to do with garbage collection.
  2. The following figure shows A simple object reference diagram, where object A refers to C and D, and object B refers to C and E. The shallow heap size of object A is just A itself, without C and D, and the actual size of object A is the sum of A, C and D. The deep heap size of A is the sum of A and D. Since object C can also be accessed through object B, object A is no longer in the deep heap of object A.

Five, the summary

The first part introduces the GRAPHICAL monitoring command GUI of the JVM. The next part introduces the MONITORING and diagnosis tool of the JVM.

Welcome to follow the public account (MarkZoe) to learn from each other and communicate with each other.