Eclipse MAT 使用说明

按:

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


一,安装

之前不知道什么奇怪的原因,安装不了,重新下载了最新的Kepler eclipse,在线正常安装。


二,基本概念

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

定位到错误代码行:

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.p_w_picpath)).setImageBitmap(mBitmap);
   }

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

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.p_w_picpath)).setImageBitmap(mBitmap);
    }


六:示例demo

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