Use of the memory analysis tool MAT and the memory analysis tool mat

Source: Internet
Author: User

Use of the memory analysis tool MAT and the memory analysis tool mat

1. Basic Introduction

The Dalvik Debug Monitor Server (DDMS) is part of the ADT plug-in. There are two functions available for memory check:

· Heap view heap allocation

· Allocation tracker tracks memory allocation

The two DDMS functions help you find Memory leakage operations.

Eclipse Memory Analysis Tools (MAT) is a professional tool for analyzing Java heap data. It can be used to identify the cause of Memory leakage.

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

 

2. Specific Use

2.1 observe Heap

Run the program and enter the DDMS management interface as follows:

 

PS: Click Update statistics on the toolbar.

Click the Cause GC button on the right or the toolbar to view the current heap status, as shown below:

Focus on two items:

O Heap Size the Size of the Heap. When resources increase and the current free space of the Heap is insufficient, the system will increase the Size of the Heap. If the Size exceeds the upper limit (for example, 64 M, depending on the platform and the specific model) will be killed.

The Allocated size in the o Allocated heap, which is the actual memory occupied by the application. After resources are recycled, the data will decrease.

· Check the heap data before and after the operation to check whether memory leakage exists.
Perform repeated operations on a single operation (such as adding or deleting pages). If the heap size keeps increasing, there is a potential memory leakage.

 

2.2 Use MAT to analyze the memory heap

DDMS can Dump the current memory into an hprof file. After the MAT reads the file, it will provide easy-to-read information and use it for searching and comparing, you can find out the cause of Memory leakage.

· Obtain the 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 plug-in, it does not need to be converted. The Adt will automatically convert and then open it.

· Convert the hprof File
The files dumped by DDMS must be converted before they can be recognized by MAT. The Android SDK provides the hprof-conv tool (under sdk/tools)

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

· Use MAT to open the converted hprof File

 

2.3 Histogram Query

The most commonly used function is HistogramClick Histogram under Actions to get the Histogram result:

It is listed by all instance objects of the class name. You can click the header to sort the objects. In the first row of the table, you can enter a regular expression to match the results:

Right-click an item and choose list objects-> with incoming refs from the shortcut menu to list instances of this class:

It shows the reference relationships between objects. For example, the first sub-item after expansion indicates that the HomePage (0x420ca5b0) is referenced by the mhomepagecontainer (0x420c9e40.

To quickly find out why an instance is not released, right-click the Path to GC Roots --> exclue all phantom/weak/soft etc. reference:

The result is:

We can see from the table that PreferenceManager->... -> The line HomePage references this HomePage instance. This method can be used to quickly findGC RootAn object with GC Root will not be recycled by GC.

 

2.4 Histogram comparison

To find memory leaks, we usually need to Compare two Dump results. Open the Navigator History panel and add the Histogram results of the two tables to Compare Basket:

After adding the file, open the Compare Basket panel. The result is as follows:

Click!Button to obtain the comparison result:

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

Sort the comparison results to get the intuitive comparison results:

You can also Compare the two object sets by adding the object sets in the two Dump results to Compare Basket for comparison. Find the difference and use the Histogram query method to find the GC Root and locate a specific object.

 

2.5 example

An example of a typical Memory Leak analysis process is as follows:

1. Use Heap to check that the current Heap size is 23.00 MB.

2. After adding a page, the heap size will change to 23.40 MB.

3. Delete the added page. The heap size is 23.40 MB.

4. Multiple operations and the results are still similar, indicating that memory leakage exists in the Add/delete pages (you should also be aware of the impact of other factors)

5. Dump the hprof file (1. hprof, 2. hprof) before and after the operation, open it with mat, and obtain the histgram result.

6. Use the HomePage field to filter the histgram results and list the object instances of the class. The object sets in the two tables are of different sizes. A HomePage exists after the operation, indicating that there is a leak.

7. Compare the two lists to find out an extra object, and use the GC Root method to find out who has hooked up the reference line and locate the end.

PS:

· In many cases, the heap increase is caused by Bitmap. The Bitmap type in Histogram is byte []. Comparing the byte [] objects in two Histogram, we can find out which bitmaps are different.

· The sorting function is used to identify differences.

 

3. Cause Analysis of Memory leakage

An invalid reference exists!
Good module design and rational use of the design pattern can help solve this problem.

 

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.