java application monitoring (6) - third-party memory analysis tool MAT

tags: java,troubleshooting,monitor,mat


Summarized in one sentence: MATit is a powerful memory analysis tool that can quickly and effectively help us find memory leaks and reduce memory consumption analysis tool, as will be explained.

1 Introduction

Articles have mentioned before, heap memory usage is a key application performance monitoring, and for heap snapshot, you can dump out further analysis, in general, we usually have three ways to heap dump snapshot:

  • Add startup parameters automatically dump occurs when OOM: Start parameters java applications are generally the best plus -XX:+HeapDumpOnOutOfMemoryErrorand -XX:HeapDumpPath=logs/heapdump.hprofthat automatically dump heap snapshot OOM in the event, but this approach is quite lag (OOM need to wait until after the occurrence).
  • Use the command-demand manual dump: We can also use jmap -dump:format=b,file=HeapDump.hprof <pid>hand tools heap dump and thread dump
  • Use manual tools dump: such as the article on jconsoleand jvisualvmexplain the mentioned, jvisualvm have provided stack dump snapshot functionality, a click away.

So, for the dump file out, how to analyze them? jvisualvmSnapshot files can be loaded directly analyzed, and described in this article MATwhere, relatively speaking, more powerful memory analysis, it can automatically detect possible problems (especially memory overflow, memory leaks), you can also view detailed class memory occupation, calling the case method-level, is a rare tool for analyzing the memory.

2 MAT tool introduced

MAT (Memory Analyzer tool) is a memory analyzer tool, it is a fast and feature-rich heap analyzer, help us find the high memory leaks and analyze memory consumption issues. The official website is: https://www.eclipse.org/mat/are interested can look on. Use MAT, you can easily achieve the following functions:

  • Find the largest object because MAT provide a reasonable display the cumulative size ( retained size)
  • FIGS search target, including inboundand outboundreferences that refer to this object and this object is drawn.
  • Find the object can not be recycled can be calculated from the root to the garbage collector related objects path
  • Locate the memory waste, such as redundant String objects, empty collection objects, etc.

3 MAT installation tool

MATInstallation There are two ways, one is a eclipseplug-in installed, one is a stand-alone installation. In MATthe official documentation of the corresponding installation file download, download address is:https://www.eclipse.org/mat/downloads.php

  • If you use eclipse plug-in installed, help -> install new softclick on ADDthe pop-up box to add plug-ins Address: http://download.eclipse.org/mat/1.9.0/update-site/you can download the offline plug-in package directly to the download page for offline installation.
  • Independent installation, under Windows, the system according to the downloaded directly Windows (x86)or Windows (x86_64)can choose their own image downloading, installation can double-click.

4 MAT tools

MATPositioning is a memory analysis tool, its main function is to analyze memory snapshots to help us find possible memory overflow or memory leak place, therefore, to find a large memory for the object and identify the object can not be recycled is its main purpose. MAT official document at the following address: https://help.eclipse.org/2019-06/index.jsp?topic=/org.eclipse.mat.ui.help/welcome.html, for MATuse description, interested students may go up. The following mainly MATcommon concepts, common features are introduced.

In the following, in order , for example, this example is a simple project, a inside the interface call objects by constantly adding members objects, eventually leading to the occurrence of startup parameters of the application is .java-monitor-example(https://github.com/mianshenglee/my-example/tree/master/java-monitor-example)spring bootcontrolleruser/oomserviceListUserOOM-Xms64m -Xmx64m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${APP_HOME}/logs/heapdump.hprof

4.1 MAT related concept note

4.1.1 memory leak and memory overflow

  • Memory leak: the object has been useless (not to be required of any program logic), there is a case cited root element can not be automatically recovered by the garbage collector, the code need to find out the location and cause leakage, determined to do solution;
  • Out of memory: memory objects are still alive, lack of JVM heap space allocation, you need to check the heap size settings (-Xmx and -Xms), the existence of the code object life cycle is too long, too long the situation of the state holding time.

4.1.2 reference (reference strong, soft, weak, reference phantom reference)

  • Strong Ref(Strong reference): Strong accessibility of reference objects stored in memory, up to only remove the strong, the object was only recovered, the code we write is usually Strong Ref.

  • Soft Ref(Soft references): soft correspondence accessibility, as long as there is enough memory, it remains an object until you find only tight memory and no Strong Ref recycled objects. Generally used to implement the cache, implemented by java.lang.ref.SoftReference class.

  • Weak Ref(Weak references): weaker than Soft Ref, when found not to exist Strong Ref, immediately recover objects without waiting for the tight memory time. Achieved by java.lang.ref.WeakReference and java.util.WeakHashMap class.

  • Phantom Ref(Phantom reference): will not retain any object in memory, you can only use the Phantom Ref itself. Generally used for special cleaning process after entering finalize () method, implemented by java.lang.ref.PhantomReference.

4.1.3 shallow heapandretained heap

  • shallow heap: Memory size of the object itself, which is the sum of the object member variable plus head (not the value of the member variables), such as a reference to 32 or occupied 64bit, an integer representing 4bytes, Long accounting 8bytes like. As a simple variable class there is only one member int i, then this class shallo sizeis 12 bytes, since the object header is 8 bytes, the member variable intis four bytes. Conventional objects (non-array) Shallow size has a variable number and type of members of the decision, shallow size arrays are array element type (object type, base type) and decision array length.
  • retained heap: If an object is freed, it will release the object because of reduced references to all objects and then be released (including recursive release) heap size occupied by that object X can be garbage collected GC and all of the objects removed from memory. With respect to the shallow heap, Retained heap more accurately reflect the size of an object actually occupied (if the object is released, retained heap can be released).

4.1.4 outgoing referencesandincoming references

  • outgoing references : Indicates a node (the object referenced object) of the object.
  • incoming references : Indicates that the ingress of the object (the object reference to the object).

4.1.5 Dominator Tree

Convert an object into a tree Dominator Treecan help us to quickly find the largest block of memory footprint, but also help us analyze the dependencies between objects. Dominator TreeThere are several definitions:

  • Object X Dominator(disposable) object Y, if and only if all the paths in the object tree reaches Y must both X
  • Y is a direct target Dominator, the distance Y is in the nearest object treeDominator
  • Dominator treeUsing the object tree to build it. In Dominator treeevery object is his direct Dominatorchild node.

Object tree and Dominator treethe correspondence is as follows:

Above, since the A and B refer to C, so that when releasing A, C memory is not released. Therefore, this memory will not be counted into the Retained Heap A or B, therefore, the object tree is converted into Dominator treetime, will be A, B, C are three same level.

4.1.6 Garbage Collection Roots(GC root)

In performing the GC, the object is judged by whether the reachability recovery object, an object is reachable, i.e. attached to see whether the reference to the object and GC Rootconnected. A GC rootrefers to the object can be accessed from outside the heap, for the following reasons can make an object become GC rootobject.

  • System Class: The bootstrap system class loader to load a class /, as in rt.jar java.util *.
  • JNI Local: JNI method of process variable or parameter
  • JNI Global: JNI method global variables
  • Thread Block: Thread inside the variable, the thread object inside a living certainly can not be recovered
  • Thread: Thread in the active state
  • Busy MonitorAfter the current object called wait (), notify () method, or a synchronization object, such as calling synchronized (Object) or into a synchronized method:
  • Java Local: Local variables, such as input parameters of the method or methods to create an internal thread stack still inside objects
  • Native Stack: Java method parameter variables or methods.
  • Finalizable: Object waiting to run the finalizer
  • Unfinalized: There finalize method, but not be finalized, and not in the object finalizer queue.
  • Unreachable: By not reachable objects in other root, MAT marks it as root analysis in order to recover.
  • Java Stack Frame: Java stack frame
  • Unknown

4.2 Leak Suspects automatically analyze leakage

In the event of OOMthe acquired heapdump.hprofafter manually dump file or a file, used MATto open the file. Open default prompt report whether memory leak detection (if open Dump skipped, then, can enter the inlet from another toolbar Run Expect System Test -> Leak Suspects), as shown below:

Select after entering the contents of the report, the contents of this report will help local memory leak may have suspected our analysis, it will accumulate a large memory footprint by pie charts displayed. As shown below:

As shown above, the report has pointed out that UserServicetaking up 76.73 percent of memory, and these memory in Object [] this array. Thus, one possibility is a large array excessive number of this object. Click Detailscan view more details:

And in detail, Shortest Paths To the Accumulation Pointmay exhibit the GC rootsshortest path, and thereby may be due to analysis which GC rootis connected to the current lead Retained Heapoccupy a considerable large objects can not be recovered, in the present example, GC roota thread-local variables ( java local). Accumulated Objects in Dominator TreeWith Dominator Treea view, you can easily see that the object affected by the current target "dominant" in which large occupancy Retained Heap. FIG objects can be seen in ArrayListin the ArrayListcase there is Objectan array, the array is next Userobject. This problem can know where we are. The need for this position, look at the code, find out what caused the array to store the Userreason for the excess.

Note: In the original directory heap dump file MAThas been compressed contents of the report packaged into a zip file named "xxx_Leak_Suspects.zip", the entire report is an HTML file format, you can use the browser to open direct view, you can facilitate report distribution share.

4.3 histogram view to see the number of objects and the size of the

Click on Overviewa page Actionsin the region "Histogram View" or click on the toolbar "histogram" button, you can display a histogram of the list, it shows the number of dimensions Class Class Class instances of each class of existing, occupied Shallow heapand Retained内存size, you can separately Sort display. As shown below:

Shallow HeapAnd Retained Heapin front of the concept has been talked about. For java objects, whose members are basically references. Real memory in the heap, appears to be a bunch of native byte [], char [], int [], so if you look at the memory of the object itself, the number is very small, but in most cases, look at the view Histogram to compare the number of instance of the object class are some basic type (usually the top side), as char [], String, byte [ ], it can not be judged only from the class or method of specific memory leak. From the above figure you can directly see Usera lot number of objects, and sometimes not easy to see, can be used in the toolbar group result bycan be super class, class loaderlike sort, custom require special attention classLoader, as shown below:

Suspect class or classes usually, if Histogram view shows the number of instances of an object type is not a basic, but user-defined, we should focus on the view. For further analysis, you can right, choose to use List objectsor Merge Shortest Paths to GC rootsother functions continue to drill data. Which list objectswere there outgoing referencesand incoming references, you can find out by the object references and references to the object which passed. Merge Shortest Paths to GC rootsYou can not exclude all strong references to find the object to GC rootthe reference path. The ultimate goal is to find the maximum memory for the object and the object can not be recycled, is calculated from the root to the garbage collector path related objects, according to the code in order to check the object path, identify the problem.

4.4 Dominator Treeview

Click on Overviewthe page Actions"Dominator Tree View" or click on the toolbar in the area "open Dominator Tree" button, you can enter the Dominator Treeview. This view shows instances of an object to dimension the current heap memory Retained Heapoccupied by the largest objects, and these objects depend on the survival of the tree structure of objects. As shown below:

View shows an example of an object name, Shallow Heapsize, Retained Heapsize, and the current object of Retained Heapproportion in the whole heap. The figure is a tree structure of objects when the parent is recovered, then it refers to the child object will be recycled, which is Dominatorof significance, when the parent node will lead to the recovery of child nodes also recovered. This view can easily find occupy Retained Heapup to several objects in memory and indicating the reasons for which certain objects are objects because survive. In this example, it can be seen due to UserServicethe ArrayListarray stores excess referenced Userobject.

4.5 thread view to see the thread stack operation

Click on the toolbar "gear" button, you can open the Thread Overviewview, you can see the thread stack frame information, including thread object / thread stack information, thread name, Shallow Heap, , Retained Heapclass loader, if Daemon thread and other information. Memory Dump combined analysis, see the thread stack frame of local variables, local variables can still see property values in the lower left area of the object properties. As the above example by ( shortest paths to GC root), is known because the thread Thread-12is GC-rootoccupies a large memory, in thread view, focus can look at the case of its properties, as follows:

From the above chart you can see this thread calls the WithOOMmethod, the use of variables UserService, and variables used userList, which contains a large number of Userobjects, occupy retained heaplarge.

5 summary

For memory analysis java applications, the need for memory java applications dump operation, after a memory snapshot, use MATanalysis to identify large objects, to find a place of memory leak or spill, to analyze the code, solve the problem. In this paper, MATthe use of scenarios, basic concepts, installation and use are described in detail, you can install it yourself, write or take a small example of the examples in this article, about the practice. In this article, we hope to help you more easily, more efficiently analyze memory, Memory Problem solving java applications.

Data sheet

  • MATOfficial website:https://www.eclipse.org/mat/
  • MATdownload:http://www.eclipse.org/mat/downloads.php
  • MATmanual:https://help.eclipse.org/2019-06/index.jsp?topic=/org.eclipse.mat.ui.help/welcome.html
  • Shallow and retained sizes:https://www.yourkit.com/docs/java/help/sizes.jsp
  • Use Eclipse Memory Analyzer Tool (MAT) Analysis fault line (a) - View & articles Function:https://www.cnblogs.com/trust-freedom/p/6744948.html
  • Sample Code Address:https://github.com/mianshenglee/my-example/tree/master/java-monitor-example

Related Reading

Guess you like

Origin juejin.im/post/5d67c9d6f265da03913529f6