Analyze memory using Eclipse Memory Analyzer

Source: Internet
Author: User

1 How to troubleshoot memory leaks

Dalvik Debug Monitor Server (DDMS) is a ADT part of the 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 an analysis Java a specialized tool for heap data that can be used to locate the cause of a memory leak.

Tool Address  : http://www.eclipse.org/mat/downloads.php

1.2 Analyzing the memory heap with the mat

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

· Gethproffile 
on the toolbar, click  button to save the memory information as a file.   If the Dump file is obtained using the MAT Eclipse plugin , it is not required to be converted,Adt is automatically converted and then turned on.

·          convert  hprof file  
DDMS dump   mat recognition, android SDK provides this tool   hprof-conv  at  sdk/tools

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

· Open the converted hprof file with the MAT


1.3 Histogram Query

most of the features used are histogram, Click on the histogram item under Actions to get Histogram Results :


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


Right-click on an item to open the menu selection List Objects->with incoming refs will list instances of this class:


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

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


The resulting results are:


As you can see from the table Preferencemanager ---->homepage This line is quoting this. Homepage instance. In this way, you can quickly find the GC rootof an object, and a gc root object will not be Recycled .

1.4 Histogram contrast

to find a memory leak, you typically need two Dump the results are compared and open Navigator History panel that will of two tables Histogram the results are added to the Compare Basket in to  :


when you've added it, open it . Compare Basket panel to get results:


In the upper-right corner, click  ! button, you will 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 two object collections, which is similar to the two Dump the collection of objects in the result is added to the Compare Basket to the comparison. 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.& NBSP;&NBSP, DUMP   HPROF  file Span style= "Color:rgb (51,51,51)" >  (1.hprof,2.hprof)  mat open  histgram results

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. More than one homepagebefore 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 :

·           bitmap bitmap   The type in histogram  byte [],  histogram Span style= "Color:rgb (51,51,51)" in  byte[] object to find out which  bitmap There are differences

· Multi-use sorting function, useful for finding differences

2 cause analysis of memory leaks

summed up only one: There is an invalid reference !  
Good module design and reasonable use of design patterns help to solve this problem.

3 Tips

· Useandroid:largeheap= "true"Mark(API level >=) 
in theAndroidmanifest.xmlin theApplicationnode can be allocated to a larger heap memory by declaring it in the, Android:largeheaptag inAndroidThere are also a wide range of applications in system applications ,likeLauncher, Browserthese are used on large memory.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Analyze memory using Eclipse Memory Analyzer

Related Article

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.