Android MAT (Memory Analyzer Tool)

Android memory optimization is a commonplace problem, but also a senior Android engineer’s necessary quality and skills, recently understand and practice the Android memory analysis tool MAT, which is a tool developed by Eclipse. Can help programmers quickly locate the common Android program memory leak, memory jitter, OOM and other problems, here I will introduce the usage: \

First we open the Profile tool in Android Studio to enable performance monitoring. Click the Memory option. For details about how to use a profile, see Android Studio Profile

As you can see, when you open the page, there will be a memory increase and then a memory decrease, which is normal (the activity will allocate memory when you open the activity), and then click dump Java Heap, which will download a heap dump file (a file that records the memory of the system over a period of time, including memory leaks).

Click the heap dump file on the left to download it to your path.

This is normal, because there are some problems with the hprofs file format, we also need to use another tool, open our Android SDK directory, Then open the platform-tools folder to see a hprof-conv.exe program

windows:

mac:

This program is specially used to do hprof file format conversion, and then open the command line in this path, with the command format conversion

windows:

Mac:

As you can see, we got the final 6.hprof file and opened it with our MAT tool

Introduction to the MAT tool

Go to file —-> Open heap dump, locate the 6.hprof file, and open the standard hprof file.

We will focus on Actions, which consists of four parts:

sdf sdf
histogram List the number and size of objects in memory
Dominator tree This view lists all instance objects as a percentage of total memory. Note that this is an object, not a class. This view is used to find large memory objects
Top Consumers This view shows possible memory leak points
Duplicate Classes This view displays information such as duplicate classes

1 Histogram

After clicking on Histogram, the following screen appears:

There are several ways to classify objects in this view, but we chose to classify them by package name for ease of analysis.

The following names are explained again:

The column name meaning
Object The number of objects of this class in memory
Shallow Heap The size of the memory occupied by the object itself, not including the size of the memory of the object it references
Retained Heap The amount of memory that will be freed after the object is collected by the garbage collector

Let’s take a look at the right-click menu options again:

1) List objects:

With incoming References: Checks which objects it is referenced by

2) The Show Objects by class option is similar to the List Objects option, except that the class name is listed.

3) Merge Shortest Paths to GC Roots We can choose to exclude some types of references:

2. Dominator Tree:

Through the “reference tree” way to show the memory usage, generally speaking, it is to observe the memory usage from the perspective of objects, mainly to see whether there are abnormal large memory objects

3. Check whether there are abnormal large memory objects:

Not very often, readers can use it by themselves

4. ### Histogram

To find memory leaks, it is common to Compare two Dump results. Open the Navigator History panel and add both table Histogram results to your Compare Basket:

After adding, open the Compare Basket panel and get the result:

Click on the top right! Button to get the comparison result:

Note that the above comparison result is not conducive to finding differences. You can adjust the comparison options:

Then the comparison results are sorted, and the intuitive comparison results can be obtained:

It is also possible to Compare two collections of objects in a similar way by adding collections of objects from two Dump results to Compare Basket. After finding out the differences, GC Root is identified by means of Histogram query and targeted to a specific object.

Reference: Android MAT explanation

Performance Optimization Tool (8) -MAT

Use of MAT tool for memory leaks