DefaultConnectivityMonitor 中的 Glide 内存泄漏

Glide memory leak in DefaultConnectivityMonitor

LeakCanary 报告了我们应用程序中的内存泄漏,泄漏跟踪如下:

05-24 08:41:05.380044 16534 19408 D LeakCanary: HeapAnalysisSuccess(heapDumpFile=/data/user/0/..../files/leakcanary/2020-05-24_08-24-07_502.hprof, createdAtTimeMillis=1590334865374, analysisDurationMillis=85525, applicationLeaks=[ApplicationLeak(className=<>.media.MediaControlActivity, leakTrace=
05-24 08:41:05.380044 16534 19408 D LeakCanary: ┬
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ android.provider.FontsContract
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: NO (HomeApplication↓ is not leaking and a class is never leaking)
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    GC Root: System class
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ static FontsContract.sContext
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ <>.application.HomeApplication
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: NO (Application is a singleton)
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    HomeApplication does not wrap an activity context
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ HomeApplication.mLoadedApk
05-24 08:41:05.380044 16534 19408 D LeakCanary: │                      ~~~~~~~~~~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ android.app.LoadedApk
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ LoadedApk.mReceivers
05-24 08:41:05.380044 16534 19408 D LeakCanary: │                ~~~~~~~~~~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ android.util.ArrayMap
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ ArrayMap.mArray
05-24 08:41:05.380044 16534 19408 D LeakCanary: │               ~~~~~~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ java.lang.Object[]
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ array Object[].[9]
05-24 08:41:05.380044 16534 19408 D LeakCanary: │                     ~~~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ android.util.ArrayMap
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ ArrayMap.mArray
05-24 08:41:05.380044 16534 19408 D LeakCanary: │               ~~~~~~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ java.lang.Object[]
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ array Object[].[58]
05-24 08:41:05.380044 16534 19408 D LeakCanary: │                     ~~~~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ ayf
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ ayf.a
05-24 08:41:05.380044 16534 19408 D LeakCanary: │          ~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ ayg
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ ayg.b
05-24 08:41:05.380044 16534 19408 D LeakCanary: │          ~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ als
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ als.b
05-24 08:41:05.380044 16534 19408 D LeakCanary: │          ~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ├─ alt
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    Leaking: UNKNOWN
05-24 08:41:05.380044 16534 19408 D LeakCanary: │    ↓ alt.b
05-24 08:41:05.380044 16534 19408 D LeakCanary: │          ~
05-24 08:41:05.380044 16534 19408 D LeakCanary: ╰→ <>.media.MediaControlActivity
05-24 08:41:05.380044 16534 19408 D LeakCanary: ​     Leaking: YES (Activity#mDestroyed is true and ObjectWatcher was watching this)
05-24 08:41:05.380044 16534 19408 D LeakCanary: ​     key = 758a249e-d3c7-44e3-b633-4649a5574735
05-24 08:41:05.380044 16534 19408 D LeakCanary: ​     watchDurationMillis = 5397
05-24 08:41:05.380044 16534 19408 D LeakCanary: ​     retainedDurationMillis = 393
05-24 08:41:05.380044 16534 19408 D LeakCanary: , retainedHeapByteSize=43842), ApplicationLeak(className=aym, leakTrace=
05-24 08:41:05.380044 16534 19408 D LeakCanary: ┬

查看映射文件后,我发现这些文件已被反混淆 类:

com.bumptech.glide.manager.DefaultConnectivityMonitor -> ayf

com.bumptech.glide.RequestManager$RequestManagerConnectivityListener -> als

com.bumptech.glide.manager.DefaultConnectivityMonitor -> ayg

com.bumptech.glide.RequestManager -> alt

事实证明,这是与 https://github.com/bumptech/glide/issues/3631 相同的问题。

我在 github 问题 link 上没有看到任何解决方案,所以在这里创建一个问题。

避免此泄漏的可能解决方案是什么?

解决方案是要么停止使用 Glide,要么为库提供修复。问题评论提供了有关如何阻止泄漏发生的指导:https://github.com/bumptech/glide/issues/3631#issuecomment-488296849