LeakCanary 使用一

一概要:

LeakCanary是GitHub上著名的开源组织Square贡献的一个内存泄漏自动检测工具。

优点:自动化发现内存泄漏;配置非常的简单。

缺点:配置时集成到低版本的应用会有bug,这时尝试修改版本:compileSdkVersion 21。

配置请参考:https://github.com/square/leakcanary

#补充一点:内存泄漏往往发生在,生命周期较长的对象,直接或间接的持有了生命周期较短的对象的强引用,导致

生命周期较短的对象不能及时的释放。

二使用:

接入步骤:

1,在Gradle中添加依赖(LeakCanary的非常好的一点是,在debug版本时才会检测,在release版本会跳过检测)

    //leakcanary 检测内存泄漏
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
2,在Application中初始化LeakCanary

    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);
        }
    }
Ok,这样基本的接入就已经完成了。

这里为了测试,故意写一个内存泄漏的用法,一个单例SingleTon对象,持有Activity对象。

public class TestLeakSingleton {

    private TextView tv;
    private Context context;

    private static TestLeakSingleton singleton = null;

    public static TestLeakSingleton getInstance(Context context){
        if(singleton == null){
            singleton = new TestLeakSingleton(context);
        }
        return singleton;
    }

    private TestLeakSingleton(Context context){
        this.context = context;
    }

    public void setTvAppName(TextView tv){
        this.tv = tv;
        tv.setText(context.getString(R.string.app_name));
    }
}
在Activity中的使用:
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testsecond);
        TextView textView = (TextView) findViewById(R.id.tv_appname);
        TestLeakSingleton.getInstance(this).setTvAppName(textView);
    }
操作:1,启动这个Activity;2,退出activity。

结果:看到很多博客都说,会直接在在通知栏出现相关的通知。我的实际情况是:

1,APP安装启动之后,在应用菜单中发现图标。


执行上述操作后出现了一个类似的Toast的弹窗如:

LeakCanary弹框

2,点击这个Toast中的logo,然后在通知栏才会有相关通知。

3,点击通知后就有详细的说明。(第二方法是直接点击应用菜单中Leaks图片,也能进入到此详情页)


#第一部分:指明TestLeakSingeton的单例模式;

#第二部分:指明造成泄漏的引用context。

#第三部分:指明造成泄漏的类对象。



LeakCanary是一个用于检测Android应用程序内存泄漏的开源库。它可以帮助开发者在开发和测试过程中快速发现和解决内存泄漏问题。 要使用LeakCanary,您需要在您的Android项目中添加以下依赖项(在您的build.gradle文件的dependencies部分): ``` debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.x.x' releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:2.x.x' ``` 接下来,在您的Application类中初始化LeakCanary: ```java public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); if (LeakCanary.isInAnalyzerProcess(this)) { // 这个进程是用于LeakCanary分析的,不执行LeakCanary的初始化操作 return; } LeakCanary.install(this); } } ``` LeakCanary会自动检测您的应用程序中的内存泄漏,并在检测到泄漏时提供详细的报告。当您运行应用程序时,如果发现内存泄漏,LeakCanary会在通知栏中显示一个通知,并在您的应用程序退出时显示一个通知。 通过分析LeakCanary提供的报告,您可以确定内存泄漏的原因,并采取相应的措施来解决问题,例如释放对象引用、取消注册监听器等。 请注意,LeakCanary仅在调试构建中生效,不会影响发布版本的性能。因此,在发布应用程序之前,请确保将LeakCanary从releaseImplementation更改为leakcanary-android-no-op。 希望这个简单的介绍对您有帮助!如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值