zoukankan      html  css  js  c++  java
  • Android内存优化8 内存检测工具2 LeakCanary——直白的展现Android中的内存泄露

    之前碰到的OOM问题,终于很直白的呈现在我的眼前:我尝试了MAT,但是发现不怎么会用。直到今天终于发现了这个新工具:

    当我们的App中存在内存泄露时会在通知栏弹出通知:

    这里写图片描述

    当点击该通知时,会跳转到具体的页面,展示出Leak的引用路径,如下图所示:

    这里写图片描述

    LeakCanary 可以用更加直白的方式将内存泄露展现在我们的面前。

    以下是我找到的学习资料,写的非常棒:
    1、LeakCanary: 让内存泄露无所遁形
    2、LeakCanary 中文使用说明

    AndroidStudio (官方)上使用LeakCanary 请移步:
    https://github.com/square/leakcanary

    Eclipse 上使用LeakCanary 请移步我的:
    https://github.com/SOFTPOWER1991/LeakcanarySample-Eclipse

    Android studio (自己弄的)上使用LeakCanary也可以看这个:

    leakcanarySample_androidStudio

    工程包括:

    1. LeakCanary库代码
    2. LeakCanaryDemo示例代码

    使用步骤:

    1. 将LeakCanary import 入自己的工程

    2. 添加依赖:

      compile project(':leakcanary')

    3. 在Application中进行配置

      public class ExampleApplication extends Application {
      
        ......
        //在自己的Application中添加如下代码
      public static RefWatcher getRefWatcher(Context context) {
          ExampleApplication application = (ExampleApplication) context
                  .getApplicationContext();
          return application.refWatcher;
      }
      
        //在自己的Application中添加如下代码
      private RefWatcher refWatcher;
      
      @Override
      public void onCreate() {
          super.onCreate();
          ......
              //在自己的Application中添加如下代码
          refWatcher = LeakCanary.install(this);
          ......
      }
      
      .....
      }
      
    4. 在Activity中进行配置

    public class MainActivity extends AppCompatActivity {
    
        ......
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
                //在自己的应用初始Activity中加入如下两行代码
            RefWatcher refWatcher = ExampleApplication.getRefWatcher(this);
            refWatcher.watch(this);
    
            textView = (TextView) findViewById(R.id.tv);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startAsyncTask();
                }
            });
    
        }
    
        private void async() {
    
            startAsyncTask();
        }
    
        private void startAsyncTask() {
            // This async task is an anonymous class and therefore has a hidden reference to the outer
            // class MainActivity. If the activity gets destroyed before the task finishes (e.g. rotation),
            // the activity instance will leak.
            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Void doInBackground(Void... params) {
                    // Do some slow work in background
                    SystemClock.sleep(20000);
                    return null;
                }
            }.execute();
        }
    
    
    }
    
    1. 在AndroidMainfest.xml 中进行配置,添加如下代码
            <service
                android:name="com.squareup.leakcanary.internal.HeapAnalyzerService"
                android:enabled="false"
                android:process=":leakcanary" />
            <service
                android:name="com.squareup.leakcanary.DisplayLeakService"
                android:enabled="false" />
    
            <activity
                android:name="com.squareup.leakcanary.internal.DisplayLeakActivity"
                android:enabled="false"
                android:icon="@drawable/__leak_canary_icon"
                android:label="@string/__leak_canary_display_activity_label"
                android:taskAffinity="com.squareup.leakcanary"
                android:theme="@style/__LeakCanary.Base" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>

    5、测试结果

    a、Toast显示(大概10秒左右显示)



    b、通知显示



    c、桌面自动添加的图表



    d、内存泄露列表



    e、内存泄露详细



    LogCat可以看到日志日下(hprof文件可以用MAT打开进行分析):

    1. 01-04 11:49:41.815 12967-13004/com.micky.leakcanarysamples I/dalvikvm: hprof: dumping heap strings to "/storage/emulated/0/Download/leakcanary/suspected_leak_heapdump.hprof".  
    2. 01-04 11:49:42.020 12967-13004/com.micky.leakcanarysamples I/dalvikvm: hprof: heap dump completed (28850KB)  


    查看自动生成的AndroidManifest文件,LeakCanarySamples/app/build/intermediates/manifests/full/debug/AndroidManifest.xml

    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    3.     package="com.micky.leakcanarysamples"  
    4.     android:versionCode="1"  
    5.     android:versionName="1.0" >  
    6.   
    7.     <uses-sdk  
    8.         android:minSdkVersion="10"  
    9.         android:targetSdkVersion="23" />  
    10.   
    11.     <!-- To store the heap dumps and leak analysis results. -->  
    12.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
    13.   
    14.     <android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  
    15.   
    16.     <application  
    17.         android:name="com.micky.leakcanarysamples.BaseApplication"  
    18.         android:allowBackup="true"  
    19.         android:icon="@mipmap/ic_launcher"  
    20.         android:label="@string/app_name"  
    21.         android:supportsRtl="true"  
    22.         android:theme="@style/AppTheme" >  
    23.         <activity  
    24.             android:name="com.micky.leakcanarysamples.MainActivity"  
    25.             android:label="@string/app_name"  
    26.             android:theme="@style/AppTheme.NoActionBar" >  
    27.             <intent-filter>  
    28.                 <action android:name="android.intent.action.MAIN" />  
    29.   
    30.                 <category android:name="android.intent.category.LAUNCHER" />  
    31.             </intent-filter>  
    32.         </activity>  
    33.         <activity android:name="com.micky.leakcanarysamples.TestActivity" />  
    34.   
    35.         <service  
    36.             android:name="com.squareup.leakcanary.internal.HeapAnalyzerService"  
    37.             android:enabled="false"  
    38.             android:process=":leakcanary" />  
    39.         <service  
    40.             android:name="com.squareup.leakcanary.DisplayLeakService"  
    41.             android:enabled="false" />  
    42.   
    43.         <activity  
    44.             android:name="com.squareup.leakcanary.internal.DisplayLeakActivity"  
    45.             android:enabled="false"  
    46.             android:icon="@drawable/__leak_canary_icon"  
    47.             android:label="@string/__leak_canary_display_activity_label"  
    48.             android:taskAffinity="com.squareup.leakcanary"  
    49.             android:theme="@style/__LeakCanary.Base" >  
    50.             <intent-filter>  
    51.                 <action android:name="android.intent.action.MAIN" />  
    52.   
    53.                 <category android:name="android.intent.category.LAUNCHER" />  
    54.             </intent-filter>  
    55.         </activity>  
    56.     </application>  
    57.   
    58. </manifest>  

    如上所示LeakCanary给我们自动添加了两个Service和一个Activity,并添加了对SD卡的读写权限


    
    
    
    
    It 's so simple.

    注: 

    1、如果在Release模式下请使用RefWatcher.DISABLED

     2、在Activity或Fragment 的 Destroy方法中添加检测(很好理解,就是判断一个Activity或Fragment想要被销毁的时候,是否还有其他对象持有其引用导致Activity或Fragment不能被回收,从而导致内存泄露)

  • 相关阅读:
    八枚硬币问题
    找出诡异的Bug:数据怎么存不进去
    IKAnalyzer使用停用词词典进行分词
    【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类
    iOS 自我检測
    蓝桥杯 BASIC 29 高精度加法(大数)
    二叉树的非递归遍历
    [算法]有趣算法合辑[11-20]
    习惯的力量之四理直气壮的借口?
    《github一天一道算法题》:分治法求数组最大连续子序列和
  • 原文地址:https://www.cnblogs.com/ldq2016/p/8472721.html
Copyright © 2011-2022 走看看