leakcanary 检测内存泄漏

来源:互联网 发布:手机上怎么改淘宝差评 编辑:程序博客网 时间:2024/05/24 01:47

1、Android studio导包

分为debug、签名、测试

debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'

2、Application中初始化

 @Override    public void onCreate() {        super.onCreate();        //初始化其它代码        refWatcher=LeakCanary.install(this);    }    public static RefWatcher getRefWatcher(Context context) {      IndorLocationApplition application = (IndorLocationApplition)context.getApplicationContext();        return application.refWatcher;    }

3.在Activity中使用

最好在BaseActivity中的onDestroy()中使用,这样就不用在每个页面中写,它的原理就是在页面销毁的时候判断有没有该销毁而没有被销毁的,有的话就检测到内存泄漏了。

@Override    protected void onDestroy() {        super.onDestroy();        RefWatcher refWatcher = IndorLocationApplition.getRefWatcher(this);        refWatcher.watch(this);    }

4.在Fragment中的使用

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

5.有内存泄漏的情况

内存泄漏提示
内存泄漏详情
至于怎么解决,这个就需要根据自己写的代码的情况进行分析了。

6.gitHub原装版

github开源项目
使用方法的介绍

1 0
原创粉丝点击