I. Preparation
1. What is Mat
A memory analysis tool provided by Eclipse. It is a feature-rich JAVA heap dump file analysis tool that can help you discover memory vulnerabilities and reduce memory consumption.
Android Studio does not integrate the plugin
Need you to download the standalone version
Android Studio's Ddms can generate hprof files, but you'll need to do a format conversion (. hprof files from Dalvik format to J2SE hprof format) before you can import mat standalone software.
What file is 2.hprof?
Heap dumps, Chinese translation, heap dump, snapshot. That's the case at some point in the heap memory.
Heap dumps, which is a more powerful memory analysis tool, a heap dump is a snapshot of a program heap, which is saved as a binary format called Hprof. Dalvik is a similar format, but not exactly the same, this is the Java hprof tool.
3. The whole process
Program run memory pair case-Program run heap memory allocation Snapshot (DDMS available, file format is hprof)--use mat to analyze the file--to determine if there is a memory leak--Modify the program
Two. MAT Analysis
1. Interface
2. Common terminology
Shallow heap
The shallow size is the amount of memory that the object itself occupies, and does not contain the object it references.
The shallow size of a regular object (not an array) has a decision about the number and type of its member variables.
The shallow size of an array is determined by the type of the array element (object type, base type), and the length of a group
Because unlike C + + objects themselves can store a lot of memory, Java object members are some references. The real memory is on the heap, it looks like a bunch of native byte[], char[], int[], so if we look at the object's own memory, the number is small. So we see that the histogram graph is sorted by shallow size, and the second in the first place is Byte,char.
Retained Heap
The concept of retained heap, which means that if an object is released, it reduces the size of the heap occupied by all objects (including the recursive release) that are being freed because of the release of the object. So, if a member of an object is new to a large array of int, the int array can also be computed into this object. Relative to shallow heap,retained heap can more accurately reflect the actual size of an object (because retained heap can be freed if the object is released).
Calculations are more complex, involving JVM garbage collection mechanisms: reference chains.
GC Root
The GC discovers that the object is reclaimed by any reference chain (reference chain) when it cannot access an object. The term 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 GC Roots 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.
Group
In the histogram and Domiantor tree interfaces, you can choose to display the results in another group (by default, group by Object) and switch to group by package to better see which of the packages the classes occupy memory. It's also easy to navigate to your application.
Path to GC Root
On an entry in histogram or Domiantor tree, right-click to view its GC Root Path: