Memory usage analysis tool MAT [turn]

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

 

1 memory leak investigation methods

 

 

Dalvik Debug Monitor Server (DDMS) is part of the ADT plugin, which has two functions of memory available for inspection:

· Heap allocation heap of view

· Allocation tracker track memory allocation

DDMS Both of these features help to find the operating behavior of a memory leak.

Eclipse Memory Analysis Tools (MAT) is one of the reasons Java heap data professional tool, it can locate memory leak analysis.

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

 

1.1 observe Heap

 

· Run the program, and then enter the DDMS management interface, as follows:

 

 

PS: Click on the toolbar   to update statistics

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

 

 

We focus on two data:

O Heap Size stack size, when additional resources, the current stack is not enough free space, the system increases the size of the heap, if it exceeds the upper limit (e.g., 64M, depending on the specific model, and internet) will be killed

o Allocated allocated heap size, which is the memory size, resource recovery applications actually occupied, this data will be smaller

· View heap data before and after the operation to see if there is a memory leak 
to a single operation (such as adding pages, delete) operation may be repeated, if the heap size has been increased, there are risks memory leaks.


1.2 Analysis of memory heap utilization MAT

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

· Obtain hprof file 
button on the toolbar to save the information into a memory file. If it is acquired by MAT Eclipse plug-Dump file, does not require conversion, Adt will be automatically converted and then opened.

• Conversion hprof file 
DDMS Dump the files to go through the conversion to be recognized MAT, Android SDK provides the tools hprof-conv (located in the sdk / tools)

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

· After opening the file conversion MAT hprof 

 

1.3 Histogram inquiry

 

The most used function is Histogram, click Actions under the Histogram Histogram items will get results:

 

 On the right to open the menu to select an item list objects -> with incoming refs instance of that class will be listed:

It shows the relationship between the reference objects, such as the first child after the expansion indicates that the HomePage (0x420ca5b0) is referenced HomePageContainer mHomePage properties (0x420c9e40).

Quickly identify the cause of an instance has not been released, you can right-Path to GC Roots -> exclue all phantom / weak / soft etc. reference:

The result is:

 As can be seen from the table PreferenceManager -> ... -> HomePage this route with reference to this example HomePage. Objects can use this method to quickly find an object GC Root, an existing GC Root is not going to be recovered out of the GC.

 

1.4 Histogram contrast

 

为查找内存泄漏,通常需要两个 Dump结果作对比,打开 Navigator History面板,将两个表的 Histogram结果都添加到 Compare Basket中去 :

 

添加好后,打开 Compare Basket面板,得到结果:

 

 点击右上角的 ! 按钮,将得到比对结果

 

 

 

 

也可以对比两个对象集合,方法与此类似,都是将两个 Dump结果中的对象集合添加到Compare Basket中去对比。找出差异后用 Histogram查询的方法找出 GC Root,定位到具体的某个对象上。

 


1.5  例子

举例一个典型的分析内存泄漏的过程:

1.  使用 Heap查看当前堆大小为 23.00M

2.  添加一个页后堆大小变为 23.40M

3.  将添加的一个页删除,堆大小为 23.40M

4.  多次操作,结果仍相似,说明添加/删除页存在内存泄漏 (也应注意排除其它因素的影响)

5.  Dump 出操作前后的 hprof 文件 (1.hprof,2.hprof),用 mat打开,并得到 histgram结果

6.  使用 HomePage字段过滤 histgram结果,并列出该类的对象实例列表,看到两个表中的对象集合大小不同,操作后比操作前多出一个 HomePage,说明确实存在泄漏

7.  将两个列表进行对比,找出多出的一个对象,用查找 GC Root的方法找出是谁串起了这条引用线路,定位结束

PS :

·        很多时候堆增大是 Bitmap引起的,Bitmap在 Histogram中的类型是 byte [],对比两个 Histogram中的 byte[] 对象就可以找出哪些 Bitmap有差异

·        多使用排序功能,对找出差异很有用


2 内存泄漏的原因分析

总结出来只有一条: 存在无效的引用! 
良好的模块设计以及合理使用设计模式有助于解决此问题。


3 Tips

·    使用 android:largeHeap="true"标记 (API Level >= 11) 
在 AndroidManifest.xml中的 Application节点中声明即可分配到更大的堆内存, android:largeHeap标记在 Android系统应用中也有广泛的应用 ,比如 Launcher, Browser这些内存大户上均有使用.


4 参考

·    DDMS 官方教程 http://developer.android.com/tools/debugging/ddms.html

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

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

Guess you like

Origin www.cnblogs.com/1906859953Lucas/p/11220907.html