Closed GitJoBo closed 2 years ago
小伙子,这个内存泄漏我看了一下,是 ActivityThread
这个类出现的内存泄漏,并不是我的代码引起的。
我没看懂哪里泄漏了,不清楚如何修复。现在只要离开MainActivity,跳转任何其它Activity,返回后Activity就会有泄漏提示
应该是ImmersionBar使用不当导致的内存泄漏,我将ImmersionBar相关代码全部注释,泄漏提示就没有了
HEAP ANALYSIS RESULT
====================================
1 APPLICATION LEAKS
References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.
78252 bytes retained by leaking objects
Signature: c8eaf439cc7c979f9b554d1f672cb9a434ca4
┬───
│ GC Root: System class
│
├─ android.app.ActivityThread class
│ Leaking: NO (a class is never leaking)
│ ↓ static ActivityThread.sCurrentActivityThread
│ ~~~~~~~~~~~~~~~~~~~~~~
├─ android.app.ActivityThread instance
│ Leaking: UNKNOWN
│ Retaining 28.7 kB in 545 objects
│ mInitialApplication instance of com.hjq.demo.app.AppApplication
│ mSystemContext instance of android.app.ContextImpl
│ mSystemUiContext instance of android.app.ContextImpl
│ ↓ ActivityThread.mNewActivities
│ ~~~~~~~~~~~~~~
├─ android.app.ActivityThread$ActivityClientRecord instance
│ Leaking: UNKNOWN
│ Retaining 3.8 kB in 57 objects
│ activity instance of com.hjq.demo.ui.activity.GuideActivity with mDestroyed = false
│ ↓ ActivityThread$ActivityClientRecord.nextIdle
│ ~~~~~~~~
├─ android.app.ActivityThread$ActivityClientRecord instance
│ Leaking: UNKNOWN
│ Retaining 124.0 kB in 2122 objects
│ activity instance of com.hjq.demo.ui.activity.HomeActivity with mDestroyed = false
│ ↓ ActivityThread$ActivityClientRecord.nextIdle
│ ~~~~~~~~
├─ android.app.ActivityThread$ActivityClientRecord instance
│ Leaking: UNKNOWN
│ Retaining 122.9 kB in 2096 objects
│ activity instance of com.hjq.demo.ui.activity.StatusActivity with mDestroyed = true
│ ↓ ActivityThread$ActivityClientRecord.activity
│ ~~~~~~~~
╰→ com.hjq.demo.ui.activity.StatusActivity instance
Leaking: YES (ObjectWatcher was watching this because com.hjq.demo.ui.activity.StatusActivity received
Activity#onDestroy() callback and Activity#mDestroyed is true)
Retaining 78.3 kB in 1444 objects
key = c3ec5732-66ed-45c0-a3a7-7e270b67dbfe
watchDurationMillis = 5206
retainedDurationMillis = 200
mApplication instance of com.hjq.demo.app.AppApplication
mBase instance of androidx.appcompat.view.ContextThemeWrapper
====================================
0 LIBRARY LEAKS
我先点击“点我进入界面状态案例”,然后点击"点我进入引导页面"也会出现内存泄漏通知;Redmi 7A MIUI 12.5.1稳定版
在AppActivity、TitleBarFragment的onDestroy()函数里增加 immersionBar?.removeSupportAllView() immersionBar = null 可以解决泄漏问题
@GitJoBo 小伙子,我经过实验,加上之后发现还是不能阻止 LeakCanary 上报该内存泄漏
我也发现了这似乎只能解决LeakCanary报DialogActivity的内存泄漏,GC还是会频繁触发。
StatusActivity泄漏将isStatusBarEnabled()返回false,就解决了。建议将ImmersionBar换成UltimateBarX
2021-12-31 11:19:18.818 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: PARSING_HEAP_DUMP 2021-12-31 11:19:18.833 26711-26711/com.hjq.demo.debug D/LeakCanary: Watching instance of android.widget.FrameLayout (android.widget.FrameLayout received View#onDetachedFromWindow() callback) with key 19406ca4-dc80-4796-b766-a2e5658e5a33 2021-12-31 11:19:19.964 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: EXTRACTING_METADATA 2021-12-31 11:19:20.132 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: FINDING_RETAINED_OBJECTS 2021-12-31 11:19:20.135 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: FINDING_PATHS_TO_RETAINED_OBJECTS 2021-12-31 11:19:21.810 26711-26741/com.hjq.demo.debug D/LeakCanary: Setting up flushing for Thread[IntentService[HeapAnalyzerService],5,main] 2021-12-31 11:19:23.643 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: FINDING_DOMINATORS 2021-12-31 11:19:23.968 26711-27091/com.hjq.demo.debug D/LeakCanary: Found 5 paths to retained objects, down to 2 after removing duplicated paths 2021-12-31 11:19:23.968 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: INSPECTING_OBJECTS 2021-12-31 11:19:24.035 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: COMPUTING_NATIVE_RETAINED_SIZE 2021-12-31 11:19:24.465 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: COMPUTING_RETAINED_SIZE 2021-12-31 11:19:24.704 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: BUILDING_LEAK_TRACES 2021-12-31 11:19:24.708 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis in progress, working on: REPORTING_HEAP_ANALYSIS 2021-12-31 11:19:24.727 26711-27091/com.hjq.demo.debug D/LeakCanary: 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ==================================== 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: HEAP ANALYSIS RESULT 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ==================================== 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: 2 APPLICATION LEAKS 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: References underlined with "~~~" are likely causes. 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: Learn more at https://squ.re/leaks. 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: 167056 bytes retained by leaking objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: Signature: e1c76f0955a7d673de751fa83bd6a78af3abee8 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ┬─── 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ GC Root: System class 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread class 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: NO (a class is never leaking) 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ static ActivityThread.sCurrentActivityThread 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │
~~~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: UNKNOWN 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 239.0 kB in 2844 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mInitialApplication instance of com.hjq.demo.app.AppApplication 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mSystemContext instance of android.app.ContextImpl 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mSystemUiContext instance of android.app.ContextImpl 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ ActivityThread.mNewActivities 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread$ActivityClientRecord instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: UNKNOWN 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 217.7 kB in 2440 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ activity instance of com.hjq.demo.ui.activity.HomeActivity with mDestroyed = false 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ ActivityThread$ActivityClientRecord.nextIdle 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread$ActivityClientRecord instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: UNKNOWN 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 216.8 kB in 2420 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ activity instance of com.hjq.demo.ui.activity.DialogActivity with mDestroyed = true 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ ActivityThread$ActivityClientRecord.activity 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ╰→ com.hjq.demo.ui.activity.DialogActivity instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: Leaking: YES (ObjectWatcher was watching this because com.hjq.demo.ui.activity.DialogActivity received 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: Activity#onDestroy() callback and Activity#mDestroyed is true) 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: Retaining 167.1 kB in 1704 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: key = ab6130d5-08ca-4c2a-ae8a-16e0474e580b 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: watchDurationMillis = 5282 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: retainedDurationMillis = 276 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: mApplication instance of com.hjq.demo.app.AppApplication 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: mBase instance of androidx.appcompat.view.ContextThemeWrapper 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: 13770 bytes retained by leaking objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: Signature: 50acba0689afc849672bede23bb43bfdabd34b 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ┬─── 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ GC Root: System class 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread class 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: NO (a class is never leaking) 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ static ActivityThread.sCurrentActivityThread 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: UNKNOWN 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 239.0 kB in 2844 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mInitialApplication instance of com.hjq.demo.app.AppApplication 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mSystemContext instance of android.app.ContextImpl 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mSystemUiContext instance of android.app.ContextImpl 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ ActivityThread.mNewActivities 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread$ActivityClientRecord instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: UNKNOWN 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 217.7 kB in 2440 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ activity instance of com.hjq.demo.ui.activity.HomeActivity with mDestroyed = false 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ ActivityThread$ActivityClientRecord.nextIdle 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ android.app.ActivityThread$ActivityClientRecord instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: UNKNOWN 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 216.8 kB in 2420 objects 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ activity instance of com.hjq.demo.ui.activity.DialogActivity with mDestroyed = true 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ ActivityThread$ActivityClientRecord.window 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │~~ 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: ├─ com.android.internal.policy.PhoneWindow instance 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Leaking: YES (Window#mDestroyed is true) 2021-12-31 11:19:24.728 26711-27091/com.hjq.demo.debug D/LeakCanary: │ Retaining 13.8 kB in 280 objects 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mContext instance of com.hjq.demo.ui.activity.DialogActivity with mDestroyed = true 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mOnWindowDismissedCallback instance of com.hjq.demo.ui.activity.DialogActivity with mDestroyed = true 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: │ mWindowControllerCallback instance of com.hjq.demo.ui.activity.DialogActivity with mDestroyed = true 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: │ ↓ PhoneWindow.mDecor 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: ╰→ com.android.internal.policy.DecorView instance 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Leaking: YES (ObjectWatcher was watching this because com.android.internal.policy.DecorView received 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: View#onDetachedFromWindow() callback and View.mContext references a destroyed activity) 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Retaining 5.1 kB in 59 objects 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: key = bc38649f-787b-4cf7-a59e-a1f8f48a85f7 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: watchDurationMillis = 5248 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: retainedDurationMillis = 231 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: View not part of a window view hierarchy 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: View.mAttachInfo is null (view detached) 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: View.mWindowAttachCount = 1 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: mContext instance of com.android.internal.policy.DecorContext, wrapping activity com.hjq.demo.ui.activity. 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: DialogActivity with mDestroyed = true 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: ==================================== 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: 0 LIBRARY LEAKS 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: A Library Leak is a leak caused by a known bug in 3rd party code that you do not have control over. 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: See https://square.github.io/leakcanary/fundamentals-how-leakcanary-works/#4-categorizing-leaks 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: ==================================== 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: 0 UNREACHABLE OBJECTS 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: An unreachable object is still in memory but LeakCanary could not find a strong reference path 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: from GC roots. 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: ==================================== 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: METADATA 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Please include this in bug reports and Stack Overflow questions. 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Build.VERSION.SDK_INT: 28 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Build.MANUFACTURER: Xiaomi 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: LeakCanary version: 2.7 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: App process name: com.hjq.demo.debug 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Count of retained yet cleared: 3 KeyedWeakReference instances 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Stats: LruCache[maxSize=3000,hits=5704,misses=66084,hitRate=7%] 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: RandomAccess[bytes=4110624,reads=66084,travel=32053565719,range=20375371,size=25249073] 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Heap dump reason: 5 retained objects, app is visible 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Analysis duration: 5889 ms 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Heap dump file path: /storage/emulated/0/Download/leakcanary-com.hjq.demo.debug/2021-12-31_11-19-16_274.hprof 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Heap dump timestamp: 1640920764707 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: Heap dump duration: 2130 ms 2021-12-31 11:19:24.729 26711-27091/com.hjq.demo.debug D/LeakCanary: ==================================== 2021-12-31 11:19:24.763 26711-26711/com.hjq.demo.debug D/LeakCanary: Watching instance of leakcanary.internal.HeapAnalyzerService (leakcanary.internal.HeapAnalyzerService received Service#onDestroy() callback) with key 892cd880-6a5a-4c94-9847-cf59a55d0c97