[Android Memory] Use of the RAM analysis tool MAT

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/aaa2832/article/details/19419679

1 How to troubleshoot memory leaks

Dalvik Debug Monitor Server (DDMS) is part of the ADT plug-in, where two features are available for memory checking:

· Heap view the allocation of heaps

· Allocation Tracker tracking Memory allocations

DDMS These two features help to find the operational behavior of a memory leak.

Eclipse Memory analysis Tools (MAT) is a professional tool for analyzing Java heap data that can be used to locate the cause of a memory leak.

Tool Address: https://www.eclipse.org/mat/

1.1 Observation Heap

· Run the program and then go to the DDMS management interface as follows:

PS: Click on the toolbar to update statistics

Click on the Cause GC button on the right or the toolbar to view the current heap situation as follows:

The main focus is on two data:

o Heap Size, when the resource is increased and the current heap is not empty enough, the system will increase the heap size, if the upper limit (such as 64M, depending on the platform and the specific model) will be killed

o The allocated size in the allocated heap, which is the actual memory size of the application, which becomes smaller after the resource is reclaimed

· See the heap data before and after the operation to see if there is a memory leak
Repeated operations on a single operation, such as adding a page, deleting a page, and if the heap size has been increasing, there is a hidden memory leak.

1.2 Analyzing the memory heap with the mat

DDMS can dump the current memory into a hprof format file, MAT read this file will give easy to read information, with its search, contrast function, you can locate the cause of memory leaks.

· Get hprof file
Click the button on the toolbar to save the memory information as a file. If the dump file is obtained using the MAT Eclipse plugin, then no conversion is required, and ADT will automatically convert and open.

· Convert hprof Files
DDMS Dump files are converted to be recognized by the mat, and the Android SDK provides the tool Hprof-conv (located under Sdk/tools)

· ./hprof-conv xxx-a.hprof xxx-b.hprof

· Open the converted Hprof file with the mat

1.3 Histogram Query

The most used feature is histogram, and clicking on the histogram item under actions will result in histogram:

It is listed by class name for all instance objects and can be sorted by clicking on the header of the table, and a regular expression can be entered in the first row of the table to match the result:

Right-click on an item to open the menu select list objects->with incoming refs will list an instance of the class:

It shows the reference relationship between objects, such as the first subkey after the expansion indicates that the homepage (0x420ca5b0) is referenced by the Mhomepage attribute in Homepagecontainer (0X420C9E40).

Quickly find out why an instance has not been released, you can right-roots-->exclue the Path to GC and all phantom/weak/soft etc. Reference:

The resulting results are:

As you can see from the table Preferencemanager.->homepage This line refers to this homepage instance. In this way, you can quickly find the GC rootof an object, and a GC root object will not be reclaimed by GC.

1.4 Histogram contrast

To find a memory leak, you typically need two dump results for comparison, open the Navigator history panel, and add histogram results from two tables to Compare basket:

Once added, open the Compare basket panel to get the results:

Click on the button in the top right corner to get the comparison result:

Note that the above comparison is not conducive to finding differences, you can adjust the comparison options:

Comparing the results of the comparison, we can get the direct comparison result:

You can also compare a collection of two objects, similar to this, by adding the collection of objects from the two dump results to the compare basket to compare. Find out the difference after using the Histogram query method to find the GC Root, to locate a specific object.

1.5 examples

For example, a typical process for analyzing memory leaks:

1. Use the heap to view the current heap size as 23.00M

2. Add a page after the heap size becomes 23.40M

3. Delete the added page with a heap size of 23.40M

4. Multiple operations, the results are still similar, indicating that there is a memory leak on the Add/Remove page (you should also be careful to exclude other factors)

5. Dump the Hprof file (1.hprof,2.hprof) before and after operation, open with mat and get Histgram result

6. Use the homepage field to filter the Histgram results and list the object instances of the class, and see that the collection of objects in the two tables is different in size, with one more homepage than before the operation, indicating there is a leak

7. Compare the two lists to find an extra object and find out who strung the reference line, locating it, using the method of finding GC root.

Ps:

· A lot of times the heap increase is caused by bitmap, bitmap in histogram type is byte [], compared to two histogram byte[] object can find out which bitmap are different

· Multi-use sorting function, useful for finding differences

2 cause analysis of memory leaks

summed up only one: there are invalid references!
Good module design and reasonable use of design patterns help to solve this problem.

3 Tips

· Use android:largeheap= "true" tag (API level >= 11)
The application node in Androidmanifest.xml is declared to be allocated to larger heap memory, and android:largeheap tags are widely used in Android applications, such as Launcher, Browser are used on all these large memory.

4 references

· DDMS Official Tutorial Http://developer.android.com/tools/debugging/ddms.html

· MAT Download http://www.eclipse.org/mat/downloads.php

· MAT Use http://android-developers.blogspot.tw/2011/03/memory-analysis-for-android.html

Colleagues drafting, sorting and sharing, can be reproduced included.

[Android Memory] Use of the RAM analysis tool MAT

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.