tags: java,troubleshooting,monitor,mat
Summarized in one sentence:
MAT
it 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:+HeapDumpOnOutOfMemoryError
and-XX:HeapDumpPath=logs/heapdump.hprof
that 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
jconsole
andjvisualvm
explain the mentioned, jvisualvm have provided stack dump snapshot functionality, a click away.
So, for the dump file out, how to analyze them? jvisualvm
Snapshot files can be loaded directly analyzed, and described in this article MAT
where, 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
inbound
andoutbound
references 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
MAT
Installation There are two ways, one is a eclipse
plug-in installed, one is a stand-alone installation. In MAT
the 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 soft
click onADD
the 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)
orWindows (x86_64)
can choose their own image downloading, installation can double-click.
4 MAT tools
MAT
Positioning 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 MAT
use description, interested students may go up. The following mainly MAT
common 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 boot
controller
user/oom
service
List
User
OOM
-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 heap
andretained 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 memberint i
, then this classshallo size
is 12 bytes, since the object header is 8 bytes, the member variableint
is 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 references
andincoming 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 Tree
can help us to quickly find the largest block of memory footprint, but also help us analyze the dependencies between objects. Dominator Tree
There 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 tree
Using the object tree to build it. InDominator tree
every object is his directDominator
child node.
Object tree and Dominator tree
the 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 tree
time, 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 Root
connected. A GC root
refers to the object can be accessed from outside the heap, for the following reasons can make an object become GC root
object.
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 parameterJNI Global
: JNI method global variablesThread Block
: Thread inside the variable, the thread object inside a living certainly can not be recoveredThread
: Thread in the active stateBusy Monitor
After 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 objectsNative Stack
: Java method parameter variables or methods.Finalizable
: Object waiting to run the finalizerUnfinalized
: 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 frameUnknown
4.2 Leak Suspects automatically analyze leakage
In the event of OOM
the acquired heapdump.hprof
after manually dump file or a file, used MAT
to 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 UserService
taking 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 Details
can view more details:
And in detail, Shortest Paths To the Accumulation Point
may exhibit the GC roots
shortest path, and thereby may be due to analysis which GC root
is connected to the current lead Retained Heap
occupy a considerable large objects can not be recovered, in the present example, GC root
a thread-local variables ( java local
). Accumulated Objects in Dominator Tree
With Dominator Tree
a 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 ArrayList
in the ArrayList
case there is Object
an array, the array is next User
object. 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 User
reason for the excess.
Note: In the original directory heap dump file
MAT
has 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 Overview
a page Actions
in 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 heap
and Retained内存
size, you can separately Sort display. As shown below:
Shallow Heap
And Retained Heap
in 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 User
a lot number of objects, and sometimes not easy to see, can be used in the toolbar group result by
can be super class
, class loader
like 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 objects
or Merge Shortest Paths to GC roots
other functions continue to drill data. Which list objects
were there outgoing references
and incoming references
, you can find out by the object references and references to the object which passed. Merge Shortest Paths to GC roots
You can not exclude all strong references to find the object to GC root
the 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 Tree
view
Click on Overview
the page Actions
"Dominator Tree View" or click on the toolbar in the area "open Dominator Tree" button, you can enter the Dominator Tree
view. This view shows instances of an object to dimension the current heap memory Retained Heap
occupied 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 Heap
size, Retained Heap
size, and the current object of Retained Heap
proportion 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 Dominator
of significance, when the parent node will lead to the recovery of child nodes also recovered. This view can easily find occupy Retained Heap
up 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 UserService
the ArrayList
array stores excess referenced User
object.
4.5 thread view to see the thread stack operation
Click on the toolbar "gear" button, you can open the Thread Overview
view, you can see the thread stack frame information, including thread object / thread stack information, thread name, Shallow Heap
, , Retained Heap
class 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-12
is GC-root
occupies 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 WithOOM
method, the use of variables UserService
, and variables used userList
, which contains a large number of User
objects, occupy retained heap
large.
5 summary
For memory analysis java applications, the need for memory java applications dump operation, after a memory snapshot, use MAT
analysis to identify large objects, to find a place of memory leak or spill, to analyze the code, solve the problem. In this paper, MAT
the 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
MAT
Official website:https://www.eclipse.org/mat/
MAT
download:http://www.eclipse.org/mat/downloads.php
MAT
manual: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
- java application monitoring (1) -java programmer should be aware of the application monitoring technology
- java application monitoring secret (2) -java command
- java application monitoring (3) - These command-line tools you have mastered yet
- java application monitoring (4) - online troubleshooting routine
- java application monitoring (5) - visual monitoring tools