Eclipse MAT 检测内存泄露

按:

仔细看了看,网上关于mat使用的资料比较少。尤其是中文的,基本上没有系统完整性的文档。本文主要列出提纲,给出有用的链接,开启思路。也会不断的补充case。


一,安装

     下载地址:http://www.eclipse.org/mat/downloads.php。

或者eclipse在线安装:Install new software


二,基本概念

1,cache

2,shallow

3,retained

4,heap dumps


三,分析

1,histroycharm

2,dominator tree

case 1:bitmap OutOfMemory


四,相关链接

http://android-developers.blogspot.com/2011/03/memory-analysis-for-android.html

Note:此链接信息量极大,可以认真看。


五,为简单起见,制造一个内存泄露的case,将分析的步骤列出来,代码在文末中。

5-1 制造内存泄露

运行示例项目,不断的点击各个项目,点击不同的图片,操作30s到1min左右。


5-2 定位指定app

打开ddms,选中device,选中对应的包com.example.android.hcgallery,选择Update Heap这个图标。此步骤即绑定对应的app,监视其内存使用情况。

mat-1

mat-2


5-3,分析准备

点击Update Heap图标右边第二位的gc图标,手动清理内存。这样,内存泄露部分的内存不会被清理掉,待会将会分析这部分。可以多执行几次gc。

mat-3


5-4,创建FPROF文件

点击Update Heap右边第一位的Dump HPROF file,这个过程中,将会生成HPROF文件,这个过程可能会比较耗时一些,耐心等待。等待弹出对话框,点击finish即可。

mat-4

mat-5


5-5,FPROF文件的图形化

接下来的工作其实是mat工具会HPROF文件的图形化及相应的处理。


5-5-1对应的饼图,是内存泄露总的概述。

mat-6


   5-5-2 接下来是按照百分比来分析。

饼图的图形化,并不能准确的定位内存泄露的真正原因,只是给出一个宏观的概念,问题的类型及权重。比如bitmap类型的占20%,比如linearlayout类型的占10%

mat-6-2


5-6 FPROF文件分析及定位错误

  5-6-1 创建Histogram

mat-7-1


  5-6-2 点击Shallow Heap列,使其按照大小从上到下排列

mat-7-2


  5-6-3 生成对象关系

此类型(byte[])泄露的对象调用关系。内存泄露大部分情况下,bitmap没有释放,而bitmap的存储是以byte[]的形式存在的。

mat-7-3

   5-6-4 点击shallowHeap,使object list按照大小从上到下排列

   5-6-5 点击开相应的行,定位到错误处。

   在进行内存分析的过程中,有时候,需要不断的向下点开,因为问题的最终起源可能比较深。

mat-7-4

定位到错误代码行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void  updateContentAndRecycleBitmap( int  category,  int  position) {
        if  (mCurrentActionMode !=  null ) {
            mCurrentActionMode.finish();
        }
                                                                         
        // Get the bitmap that needs to be drawn and update the ImageView.
                                                                         
        // Check if the Bitmap is already in the cache
        String bitmapId =  ""  + category +  "."  + position;
        mBitmap = sBitmapCache.get(bitmapId);
                                                                         
        if  (mBitmap ==  null ) {
            // It's not in the cache, so load the Bitmap and add it to the cache.
            // DANGER! We add items to this cache without ever removing any.
            mBitmap = Directory.getCategory(category).getEntry(position)
                    .getBitmap(getResources());
            sBitmapCache.put(bitmapId, mBitmap);
        }
        ((ImageView) getView().findViewById(R.id.image)).setImageBitmap(mBitmap);
    }

问题是bitmap没有被recycle掉。修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void  updateContentAndRecycleBitmap( int  category,  int  position) {
         mCategory = category;
         mCurPosition = position;
         if  (mCurrentActionMode !=  null ) {
             mCurrentActionMode.finish();
         }
         if  (mBitmap !=  null ) {
             // This is an advanced call and should be used if you
             // are working with a lot of bitmaps. The bitmap is dead
             // after this call.
             mBitmap.recycle();
         }
         // Get the bitmap that needs to be drawn and update the ImageView
         mBitmap = Directory.getCategory(category).getEntry(position)
                 .getBitmap(getResources());
         ((ImageView) getView().findViewById(R.id.image)).setImageBitmap(mBitmap);
     }


六:示例demo

github地址:https://github.com/mikewang0326/MAT-OutOfMemory-Example


一点小心得:

对象关系图可以使用QQL写入select 过滤想要的包对象点 红色' ! '执行;如果要定位内存泄露位置 ,通过Leak Suspects的Problem Suspect 1点击【Details »】,下一个博客有记录。


本文出自 “小新专栏” 博客http://mikewang.blog.51cto.com/3826268/1254306

MAT工具使用说明.docx MAT(Memory Analyzer Tool)工具入门 一MAT简介 MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速、功能丰富的JAVA heap分析工具,它可以帮助我们查找内存泄漏和减少内存消耗。使用内存分析工具从众多的对象中进行分析,快速的计算出在内存中对象的占用大小,看看是谁阻止了垃圾收集器的回收工作,并可以通过报表直观的查看到可能造成这种结果的对象。 二 使用MAT意义 当服务器应用占用了过多内存的时候,会遇到OutOfMemoryError。如何快速定位问题呢?Eclipse MAT的出现使这个问题变得非常简单。它能够离线分析dump的文件数据。 四 MAT操作流程 1先调用jdk的工具得到heap使用情况 我安装的是jdk1.6 C:/>java -version java version "1.6.0_11" Java(TM) SE Runtime Environment (build 1.6.0_11-b03) Java HotSpot(TM) Client VM (build 11.0-b16, mixed mode, sharing) 2调用jdk工具jps查看当前的java进程 C:/>jps 3504 Jps 3676 Bootstrap 3496 org.eclipse.equinox.launcher_1.0.201.R35x_v20090715.jar 3调用jmap工具得到信息 C:/>jmap -dump:format=b,file=heap.bin 3676 Dumping heap to C:/heap.bin ... Heap dump file created 这时,我们的C盘根目录,就生成了heap.bin文件。 4用eclipse的file---->open打开这个文件,首先是一个启动图: 这里可以选择查看 (1)内存泄露报表,自动检查可能存在内存泄露的对象,通过报表展示存活的对象以及为什么他们没有被垃圾收集; (2)对象报表,对可颖对象的分析,如字符串是否定义重了,空的collection、finalizer以及弱引用等。 我这里选择的是查看内存报表,以下是截的简略图: 通过报表展示,蛮清楚的,下面还有详细的说明,这里就没有帖图了,有兴趣的可以继续探究。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值