天天看点

Android 内存泄漏分析利器——leakcanary

leakcanary      一、简介      git地址(不用下载,使用gradle 引入便可): https://github.com/square/leakcanary      使用过程中发现,1.4,1.5, 1.6都会出现相应的 OOMcrash 无法获得相应的结果, 当前可正常使用的是 1.3版本。 (如果有相应问题的朋友建议使用1.3)      【错误提示】OutOfMemory Error thrown during Leak Analysis      这个项目是在应用相应的回退之后分析是否存在内存泄漏,如果存在内存泄漏,将进行相应的分析并处理,若没有则不会,不能做到MAT或者studio中相应的实时查看内存状态的,并且检测具有很大的延时,最少10s。       二、如何使用     测试demo可以采用官方的例子,地址为: https://github.com/square/leakcanary/tree/master/leakcanary-sample/src/main/java/com/example/leakcanary     具体操作如下:     step 1、配置build      在相应的build.gradle中加入如下代码       dependencies {              debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'              releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.3'        }     【错误1:】截图如下: Failed to resolve: com.squareup.leakcanary:leakcanary-android      

Android 内存泄漏分析利器——leakcanary

     需要在在build中再加入如下代码,并 clean Build。       

allprojects {   
          repositories {       
               jcenter()
          }
       } 
           

     如果还是出现上述问题,那么替换为如下的代码:      allprojects {              repositories {                       jcenter()                  mavenCentral()              }        }                   step 2、写入启动代码       在相应的application中加入(也可以在其他地方,但是传入install的context必须是application)       

onCreate(){
         super.onCreate();
         LeakCanary.install(this);
      }
           

     step3、应用安装

     安装应用,在debug版本的apk安装后,会出现如下两个图标:左边的是自己应用的图标,右边是启动应用后退出,自动安装的leakCancayDe图标。  

Android 内存泄漏分析利器——leakcanary

           【错误2】:但是有的人没有相应的图标,怎么办?       这个时候查看自己工程的库的引用,见下图:       

Android 内存泄漏分析利器——leakcanary

     因为gradle设置错误的原因,上述build分别设置了debugCompile 和 releaseCompile,具体的区别这里不细说了,需要有一定的gradle功底,才能改修完成。这里给出的最简易的方案,适用于该产品在加入的leakCancy仅仅在测试的时候使用,而在release包中手动去除相应的代码:【解决当前问题,但是不提倡】      1、debug 和 release 引用相同的lib

     dependencies {              debugCompile 'com.squareup.leakcanary:leakcanary-android:1.3'              releaseCompile ' com.squareup.leakcanary:leakcanary-android:1.3 '        }      2、使用compile 不再1区分debug 和 release      dependencies {              compile 'com.squareup.leakcanary:leakcanary-android:1.3'        }       step4、检测内存泄漏      采用官方demo,点击相应的测试按钮,会启动一个 AsyncTask,人为制造内存泄漏,之后back出去,等一段时间,看情况,一般会大于10s,有些会更长(不清楚具体的触发条件,什么时候开始检测,作者也没具体说明)如果是1.4以上,会有相应的一个启动提示,而1.3没有,这个时候可以看 studio中是否存在如下图的进程,并根据其相应的日志就才能知道是否启动了。

Android 内存泄漏分析利器——leakcanary

        step5、如何知道检测到内存泄漏      一旦检测到内存泄漏,将会有相应的通知栏的提示(也就是说,如果你的应用不存在相应的泄漏,则不会提示)              

Android 内存泄漏分析利器——leakcanary

     step6、查看内存泄漏信息

     点击通知栏图标或者点击应用icon都可以,可以得到如下的信息,根据这个信息就可以知道哪里出现了内存泄漏。

Android 内存泄漏分析利器——leakcanary

          此外,还可以测试具体的Fragment,可以使用如下代码:      

public abstract class BaseFragment extends Fragment {

  @Override public void onDestroy() {
    super.onDestroy();
    RefWatcher refWatcher = ExampleApplication.getRefWatcher(getActivity());
    refWatcher.watch(this);
  }
}
           

这时候,application修改为:  

public class ExampleApplication extends Application {

  public static RefWatcher getRefWatcher(Context context) {
    ExampleApplication application = (ExampleApplication) context.getApplicationContext();
    return application.refWatcher;
  }

  private RefWatcher refWatcher;

  @Override public void onCreate() {
    super.onCreate();
    if (LeakCanary.isInAnalyzerProcess(this)) {
      // This process is dedicated to LeakCanary for heap analysis.
      // You should not init your app in this process.
      return;
    }
    refWatcher = LeakCanary.install(this);
  }
}