LeakCanray不需要手动初始化的秘密(2.7源码解析)

前言

需要初始化的版本中,我们需要在application中初始化leakcanary,为什么新版本的leakcanary不需要了呢?本文带你探究

什么是LeakCanray

LeakCanary is a memory leak detection library for Android.
LeakCanary’s knowledge of the internals of the Android Framework gives it a unique ability to narrow down the cause of each leak, helping developers dramatically reduce Application Not Responding freezes and OutOfMemoryError crashes.

LeakCanary是一个适用于Android的内存泄漏检测库。
LeakCanary对安卓框架内部的了解使其能够独特地缩小每次泄漏的原因,帮助开发人员显著减少应用程序未响应冻结和OutOfMemoryError崩溃。

A small leak will sink a great ship.” - Benjamin Franklin

“一个小漏洞会击沉一艘大船。”-本杰明·富兰克林

摘录翻译自 LeakCanray官网

最新使用

在app的 build.gradle中添加

    //leakcanary内存泄漏检测
    debugImplementation "com.squareup.leakcanary:leakcanary-android:$version"
  • $version是版本号

在2.0版本过后,不需要添加多余的代码,就这样就好了

为什么

需要初始化的版本

需要初始化的版本中,我们需要在application中初始化leakcanary

不需要初始化的版本

这需要对APP的启动流程中去寻找答案。在APP启动过程中,ActivityThread会收到一 “BIND_APPLICATION” 的消息,收到这条消息后在handleMessage进行处理,执行handleBindApplication方法,源码如下

  public void handleMessage(Message msg) {
            if (DEBUG_MESSAGES) Slog.v(TAG, ">>> handling: " + codeToString(msg.what));
            switch (msg.what) {
                case BIND_APPLICATION:
                    Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "bindApplication");
                    AppBindData data = (AppBindData)msg.obj;
                    //这个方法
                    handleBindApplication(data);
                    Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
                    break;
                    }
                    ......
                    					}

通过这个方法调用了mInstrumentation.callApplicationOnCreate(app);,源码如下

			...
            try {
            	//调用application的oncreate
                mInstrumentation.callApplicationOnCreate(app);
            } catch (Exception e) {
                if (!mInstrumentation.onException(app, e)) {
                    throw new RuntimeException(
                      "Unable to create application " + app.getClass().getName()
                      + ": " + e.toString(), e);
                }
            }
			...

源码的注释
在这里插入图片描述
而在这之前,会先执行installContentProviders方法

 private void installContentProviders(
            Context context, List<ProviderInfo> providers) {
       ...
            ContentProviderHolder cph = installProvider(context, null, cpi,
                    false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
     ...

在其中执行了installProvider方法

在这里插入图片描述
通过这个方法,初始化ContentProvider。

讲了那么多,我们就可以说结论了,LeakCanray通过以上的流程,通过继承ContentProvider,在ContentProvider的onCreate方法中进行了初始化

2.0后不同版本实现的细微差别

改变
LeakSentryInstaller
AppWatcherInstaller

初始化类的名字发生了改变

  • 在2.0版本中初始化源码为
internal class LeakSentryInstaller : ContentProvider() { 
    override fun onCreate(): Boolean {
        CanaryLog.logger = DefaultCanaryLog()
        val application = context!!.applicationContext as Application
        InternalLeakSentry.install(application)
        return true
    }
}

在2.7版本中初始化源码为

/**
 * Content providers are loaded before the application class is created. [AppWatcherInstaller] is
 * used to install [leakcanary.AppWatcher] on application start.
 */
internal sealed class AppWatcherInstaller : ContentProvider() {

  /**
   * [MainProcess] automatically sets up the LeakCanary code that runs in the main app process.
   */
  internal class MainProcess : AppWatcherInstaller()

  /**
   * When using the `leakcanary-android-process` artifact instead of `leakcanary-android`,
   * [LeakCanaryProcess] automatically sets up the LeakCanary code
   */
  internal class LeakCanaryProcess : AppWatcherInstaller()

  override fun onCreate(): Boolean {
    val application = context!!.applicationContext as Application
    AppWatcher.manualInstall(application)
    return true
  }

总结

主要是考察的其实是通过对LeakCanray源码的熟悉程度考察APP的启动流程,掌握这两个知识点那么LeakCanray不需要手动初始化的秘密对你来说就不是秘密啦

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我怀里的猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值