android leakCanary检测内存泄露

来源:互联网 发布:炫浪网络社区看不了 编辑:程序博客网 时间:2024/05/02 02:04

1,添加依赖

debugCompile ‘com.squareup.leakcanary:leakcanary-android:1.3’
releaseCompile ‘com.squareup.leakcanary:leakcanary-android-no-op:1.3’

2 初始化

public class MyApplication extends Application {    private static RefWatcher mRefWatcher;    @Override    protected void attachBaseContext(Context base) {        super.attachBaseContext(base);        MultiDex.install(this);        mRefWatcher = LeakCanary.install(this);    }    public static RefWatcher getRefWatcher() {        return mRefWatcher;    }}

3 检测

     RefWatcher refWatcher = MyApplication.getRefWatcher();        refWatcher.watch(this);

在activity的onCreate中检测就可以了,如果出现内存泄露就会在状态栏有提示;