转载:http://blog.youkuaiyun.com/android_zhengyongbo/article/details/69951010
添加依赖
compile 'com.squareup.leakcanary:leakcanary-android:1.5'
在Application中初始化添加下面代码
正式发布的时候注销下面代码
if (LeakCanary.isInAnalyzerProcess(this)) {
// This process is dedicated to LeakCanary for heap analysis.
// You should not init your app in this process.
return;
}
LeakCanary.install(this);
ok这样已经可以用了,在安装你的App的时候桌面还会出现一个Leaks的App,里面有内存泄露的日志,可以查看并且解决问题。
如果有static变量造成的泄露可以在onDestory方法中设置为null
例如:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(mStatic!=null){
mStatic=null;
}