Mat----mat more powerful than Menmery monitor _ memory Leak detection

Source: Internet
Author: User
Tags first row

In the Android Performance Optimization Chapter (a)---basic concepts of Java's four references, said Gcroot, the second memory monitor detect memory leaks only said Menmery monitor use, this blog talk about mat to find memory leaks, In contrast, Memory Monitor is not mat strong, but before the introduction of Mat, two of the questions did not make it clear that first. Determination of recyclable objects by GC recovery
What kind of objects can be recycled.
Of course, the GC discovers that the object is reclaimed by any referencechain (reference chain) when it cannot access an object. The noun GC Roots is the starting point for analyzing this process, for example, the JVM itself ensures the object's accessibility (then the JVM is GC Roots), so gcroots is the way to keep object accessibility in memory, which is reclaimed once it is unreachable. Typically, GC roots is an object (such as method parameters and local variables) on the call stack (the calling stack) on current thread (currently threads), or either the thread itself or the System class loader (systems ClassLoader) The loaded class and the active object reserved by the native code (local codes). So GC roots is a sharp tool to analyze why objects still exist in memory.
GC Root Tracing algorithm for the determination of recoverable objects


The above passage also writes out the basic idea of detecting memory leaks: Search down as the starting point of the object "GC Roots", where the path is called a reference chain, and when an object to the GC Roots is not connected to any reference chain (that is, unreachable), the object is judged to be a recoverable object. The reverse cannot be recycled. It also writes out the reason for the memory leak: The object is useless, but it is still available (not released) and the garbage collector cannot reclaim it.

All right, here's the introduction to the Mat,mat tool, all called memory Analyzer Tool, Mat download: http://eclipse.org/mat/ downloads.php) is a detailed analysis of Java heap memory tools, the tool is very powerful, in order to use this tool, we need hprof files. However, the file can not be directly used mat, need to be a step conversion, you can use the Hprof-conv command to transform, but Androidstudio can be directly converted.

Export a standard hprof file


Then with mat to open the file we exported, I exported two files, test1.hprof and Test2.hprof, where the test1.hprof is a snapshot of the memory leak, TEST2.HPROF is the memory has been leaked snapshots. We use mat histogram (straight square) and Dominator tree to analyze the memory. Histogram can list the name, number, and size of each object in memory. Dominator Tree will sort all the objects in memory by size, and we can parse the reference structure between objects.

Mat Tool main Interface One, histogram (straight square chart)

You can list the number of instances of each class. A regular expression lookup is supported, or the retained size of all objects in the class can be computed. The default is shown by category Class (Group by Class).

Histogram. png

This is the histogram of the test2.hprof. Now let's say two noun explanations. Shallow heap/retained Heap Shallow Heap
Shallow size is the amount of memory consumed by the object itself, and does not contain the object memory it references, which is of little use in actual analysis. On the heap, it looks like a bunch of native byte[], char[], int[], and the object itself has very little memory. So we can see that in the histogram graph with shallow Heap, the first place in the second place is the Byte,char retained Heap
The retained size is the object's own shallow size, plus the sum of the shallow size from which the object can be accessed directly or indirectly. In other words, the retained size is the sum of memory that the object can reclaim after GC. Retainedheap can more accurately reflect the actual size of an object (because retained heap can be freed if the object is released).

Histogram can display the number of objects, so for example, we now suspect that there might be a memory leak in the mainactivity, you can search for "mainactivity" in the first row of the regular expression box, as follows:


Can see the number of mainactivity is 2, this is not normal, to solve this, you need to see mainactivity by who cited, can not be released. We right-click on exclude all Phantom/weak/soft etc.references, meaning to view reference chains that exclude virtual references/weak references/soft references (these references can eventually be eliminated by GC, so exclude)


There are other menus to select List objects with (view in Dominator tree) incoming references the object referenced to the object outcoming references the object referenced by the object show Objec TS by Class (view in Class) incoming references objects referenced to the object Outcoming references objects referenced by the object

When you press the above operation, the murderer appears, the original is a Usermanger instance.

You can also view histogram by the package name.

There are more powerful, queries through OQL statements, a bit like writing SQL statements.


is not a minute to find out Mainactivity has two objects. Ha ha.
For example: Find size=0 and unused ArrayList

SELECT * from Java.util.ArrayList where size=0 and modcount=0

This place can be studied more. Second, dominator tree (ruling trees)

Dominator Tree is the dominator relationship between objects. If all of the path to Y from GC root passes through X, then we call x dominates Y, or X is y Dominator Dominator tree is computed from the complex object graph in the system. From Mat's dominator tree, you can see the most memory-consuming objects and the dominator of each object. We can also right-click on the dominator of an object by right-clicking immediate dominator. It can sort all the objects by heap size, so that the large memory object is in the top, we can search the path of the large memory object to the GC roots, because the higher the memory footprint of the object is more doubtful, the use of the method and histogram (straight square diagram) is similar, I will not make too many introductions here. Three, memory snapshot comparison

We can compare the histogram of the test1.hprof with the histogram of Test2.hprof, where the object#0 is Test1.hprof, Object#1 is test2.hprof, by comparing the size of some objects is too large.

Memory Snapshot comparison

Other information:
The official Help document
Http://wiki.eclipse.org/index.php/MemoryAnalyzer

Using Eclipse Memory Analyzer for heap dump file analysis
Http://www.vogella.de/articles/EclipseMemoryAnalyser/article.html

Mat Use Tutorial
Http://www.vogella.de/articles/EclipseMemoryAnalyser/article.html

Author: looperjing
Link: HTTP://WWW.JIANSHU.COM/P/2D47D1CF5CCF
Source: Jianshu
Copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.