Android Memory Optimizer 9 Memory Detection Tool 3 mat more powerful than menmery monitor

Source: Internet
Author: User

In the basic concept of Android performance optimization (a)---the four references to Java, speaking of Gcroot, the second memory monitor to detect the leak only said Menmery monitor use, this blog talk about mat to look for memory leaks, In contrast, theMemory Monitor is not as strong as the mat, but before starting the introduction of the mat, the last two questions were not clearly stated first.

    • GC Recovery for the determination of recyclable objects
      What kind of objects can be recycled?
      Of course, the GC finds that the object is reclaimed by any referencechain (chain of reference) that cannot access an object. The noun GC Roots is the starting point for the analysis of the process, such as the JVM itself ensuring the accessibility of the object (then the JVM is GC Roots), so the gcroots is to keep the object reachable in memory and, once unreachable, is recycled. Typically GC roots is an object on the call stack (the calling stack) on the current thread (for example, method parameters and local variables), or either the thread itself or the System class loader (the ClassLoader) The class that is loaded and the active object that native code (local code) retains. So GC roots is a powerful tool for analyzing why objects still exist in memory.

GC Root Tracing algorithm for the determination of recyclable objects


The above paragraph, also wrote the basic idea of detecting memory leaks : The "GC Roots" object as the starting point to search down, the path formed by the search is called the reference chain, when an object to the GC Roots no reference chain connected (that is unreachable), The object is judged to be an object that can be recycled, and vice versa. also writes out the reason for memory leaks : objects are useless, but still up (not released), the garbage collector cannot be reclaimed.

Okay, here's the beginning. Mat,mat tools are all called memory Analyzer tool, (Mat download: http://eclipse.org/mat/ downloads.php) is a detailed analysis of the Java Heap Memory tool, the tool is very powerful, in order to use the tool, we need to hprof files. However, the file cannot be used directly by mat and requires a one-step conversion, which can be converted using the Hprof-conv command, but Androidstudio can be converted directly.


Export a standard hprof file


Then using the mat to open our exported file, I exported two files, test1.hprof and Test2.hprof, where Test1.hprof is a snapshot of the memory is not compromised, Test2.hprof is a snapshot of the memory has been compromised. We use Mat's histogram (histogram) and Dominator tree to analyze the memory situation. Histogram can list the name, number, and size of each object in memory. Dominator Tree sorts all in-memory objects by size, and we can parse the reference structure between objects.


Mat Tool main Interface One, histogram (histogram)

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


Histogram. png

This is the histogram of the test2.hprof. Now we have to say two noun explanations. Shallow heap/retained Heap

    • Shallow Heap
      Shallow size is the size of the object itself that occupies memory and does not contain its referenced object memory, which does little in the 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 diagram of the shallow heap, the first second bit is Byte,char
    • Retained Heap
      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 to the object. In other words, the retained size is the sum of the memory that the object can be recycled to after it has been GC. Retainedheap can more accurately reflect the actual size of an object (because if the object is released, the retained heap can be freed).

The number of objects can be displayed in histogram, so for example, we now suspect that there may be a memory leak in mainactivity, you can search for "mainactivity" in the Regular expression box in the first row, as follows:



You can see that the number of mainactivity is 2, this is not normal, to solve this, you need to see Mainactivity was referenced by WHO, can not be released. We right-click the exclude all Phantom/weak/soft etc.references, which means to view the reference chains that exclude virtual references/weak references/soft references (these references can eventually be killed by GC, so excluded)



There are other menus to choose from

    • List objects with (view as Dominator tree)
    • Incoming references the object to which the object is referenced
    • Outcoming References object referenced by this object
    • Show objects by Class (View as Class)
    • Incoming references the object to which the object is referenced
    • Outcoming References object referenced by this object

When you press the above operation, the murderer appears, originally is the example of Usermanger.


There are also histogram that can be viewed through the package name.


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



is not divided into minutes to detect 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.

II. Dominator tree (dominating trees)

Dominator tree is the Dominator tree between objects. If all paths from GC root to Y go through X, then we call x dominates Y, or x is y Dominator Dominator tree is calculated from the complex object graph in the system. From the Dominator tree of the mat, you can see the objects that occupy the most memory and the dominator of each object. We can also right-click immediate Dominator to see the Dominator of an object. It can sort all objects according to the heap size, so that large memory objects are ranked in the first few, we can search for large memory objects to the path of GC roots, because the higher the memory consumption of the object is more questionable, using the method is similar to histogram (histogram), I'm not going to do much about it here.

Three, memory snapshot comparison

We can compare the histogram of test1.hprof with the histogram of Test2.hprof, wherein Object#0 is Test1.hprof, Object#1 is test2.hprof, by comparing which objects are larger than the size of the difference.


Memory Snapshot comparison

Other information:
Official Help documentation
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

Android Memory Optimizer 9 Memory Detection Tool 3 mat more powerful than menmery monitor

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.