LeakCanary是一个使用非常方便的内存泄漏工具,集成简单,分析准确,能够快速精确的定位到内存泄漏的位置。
集成方式
//内存泄漏检测工具
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.1'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.6.1'
// Optional, if you use support library fragments:
debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.1'
使用方法: 在application的onCreate()方法中-->
@Override
public void onCreate() {
super.onCreate();
//内存泄漏检测工具 LeakCanary
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);
//后面跟随其他的内容,但是一定要注意:
//不能在这后面再调用registerActivityLifecycleCallbacks()方法了
//不然会覆盖掉工具的内容
//如果一定要调用...还是想其他办法吧,没法一起调用
}
安装你的应用,工具会将你泄漏的activity还有分析都通过通知的方式通知你,点击通知你就能看到了



从分析中可以看出,是MultiWebViewAct变量引起的泄漏,进入代码查看
public class MultiWebViewActivity extends PureActivity {
//...
public static MultiWebViewActivity MultiWebViewAct;
//...
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
MultiWebViewAct = this;//不泄露才怪了...
}
网友评论