1.内存泄露
LeakCanary
集成至项目当中监测内存泄露,可以实时检测,比较好用!
2.检测到的问题
当项目中存在内存泄漏问题时,LeakCanary
会通过弹窗的形式,通知你检测到的问题。可以打开LeakCanary
的app
会出现弹窗中的泄露位置;
此时在Logcat
中也会打印LeakCanary
检测到的问题:
打印的日志代码如下:
====================================
HEAP ANALYSIS RESULT
====================================
1 APPLICATION LEAKS
References underlined with "~~~" are likely causes.
Learn more at https://squ.re/leaks.
234941 bytes retained by leaking objects
Signature: 52415049a0113986014314100d10ae85c9ab46ee
┬───
│ GC Root: Input or output parameters in native code
│
├─ android.os.MessageQueue instance
│ Leaking: NO (MessageQueue#mQuitting is false)
│ HandlerThread: "main"
│ ↓ MessageQueue[2]
│ ~~~
├─ android.os.Message instance
│ Leaking: UNKNOWN
│ Retaining 235.1 kB in 3424 objects
│ Message.what = 1
│ Message.when = 1717029504 (109 ms after heap dump)
│ Message.obj = null
│ Message.callback = null
│ Message.target = instance @326657336 of android.os.CountDownTimer$1
│ ↓ Message.target
│ ~~~~~~
├─ android.os.CountDownTimer$1 instance
│ Leaking: UNKNOWN
│ Retaining 235.0 kB in 3423 objects
│ Anonymous subclass of android.os.Handler
│ ↓ CountDownTimer$1.this$0
│ ~~~~~~
├─ (此处为你的包名...).CodeLoginActivity$initListener$5$1 instance
│ Leaking: UNKNOWN
│ Retaining 235.0 kB in 3422 objects
│ Anonymous subclass of android.os.CountDownTimer
│ this$0 instance of (此处为你的包名...).CodeLoginActivity with mDestroyed = true
│ ↓ CodeLoginActivity$initListener$5$1.this$0
│ ~~~~~~
╰→ (此处为你的包名...).CodeLoginActivity instance
Leaking: YES (ObjectWatcher was watching this because (此处为你的包名...).CodeLoginActivity received
Activity#onDestroy() callback and Activity#mDestroyed is true)
Retaining 234.9 kB in 3421 objects
watchDurationMillis = 5710
retainedDurationMillis = 702
mApplication instance of (此处为你的包名...)BaseApplication
mBase instance of androidx.appcompat.view.ContextThemeWrapper
====================================
3.问题分析
从上面的日志中可以看出整了链表,执行的顺序过程,其中Leaking会有三种状态:NO
(无问题)、UNKNOWN
(可能有问题)、YES
(有问题),从上面的链表当中寻找到;
状态:NO
(无问题)
├─ android.os.MessageQueue instance
│ Leaking: NO (MessageQueue#mQuitting is false)
│ HandlerThread: "main"
│ ↓ MessageQueue[2]
状态:UNKNOWN
(可能有问题)
├─ android.os.CountDownTimer$1 instance
│ Leaking: UNKNOWN
│ Retaining 235.0 kB in 3423 objects
│ Anonymous subclass of android.os.Handler
│ ↓ CountDownTimer$1.this$0
状态:YES
(有问题)
╰→ (此处为你的包名...).CodeLoginActivity instance
Leaking: YES (ObjectWatcher was watching this because (此处为你的包名...).CodeLoginActivity received
Activity#onDestroy() callback and Activity#mDestroyed is true)
Retaining 234.9 kB in 3421 objects
watchDurationMillis = 5710
retainedDurationMillis = 702
mApplication instance of (此处为你的包名...)BaseApplication
mBase instance of androidx.appcompat.view.ContextThemeWrapper
此处存在问题,然后去代码中找就可以了
4.我的问题
我的问题是在进行获取验证码的时候,设置的倒计时时间为60秒,当很快获取到验证码,登录成功之后,所引用的倒计时TextView
还在引用,导致了内存泄露!