当前位置:网站首页>LeakCanary 源码详解(2)
LeakCanary 源码详解(2)
2022-06-22 20:55:00 【Android_周志豪378623234】
接着上一篇说的源码部分,分析一下这图中核心代码的意思:
moveToRetained方法
然后是onObjectRetained方法
它其实是在下图的代码中这里添加的
这个方法又指向了scheduleRetainedObjectCheck,你也看到了接着调用了heapDumpTrigger.scheduleRetainedObjectCheck()
,我们看看 heapDumpTrigger.scheduleRetainedObjectCheck(),看看断点的方法 checkRetainedObjects()
重点3个,第一处是调用了gc,第二处,检查残留的object,第三处如果还有残留的,要通知用户,就是报内存泄露提醒
我们看看第三处的细节重点就是 这个PendingIntent,使用 FLAG_IMMUTABLE 标记构建了无法被修改的 PendingIntent。
调用 NotificationManagerCompat.notify() 之后工作就完成了。当系统显示通知,且用户点击通知时,会在我们的 PendingIntent 上调用 PendingIntent.send(),来启动我们的应用的广播,这个时候才会去heap dump ,这有点懒加载到意思吧,哈哈。都是细节。
(sample 中 点击 recreate 按钮的 有一个 checkRetainedObjects scheduleRetainedObjectCheck checkRetainedObjects 循环,不清楚什么用意)
下面图中,我们可以看到在我们点击通知后,看到代码做了什么动作

fun onDumpHeapReceived(forceDump: Boolean) {
backgroundHandler.post {
dismissNoRetainedOnTapNotification()
gcTrigger.runGc()
val retainedReferenceCount = objectWatcher.retainedObjectCount
if (!forceDump && retainedReferenceCount == 0) {
SharkLog.d {
"Ignoring user request to dump heap: no retained objects remaining after GC" }
@Suppress("DEPRECATION")
val builder = Notification.Builder(application)
.setContentTitle(
application.getString(R.string.leak_canary_notification_no_retained_object_title)
)
.setContentText(
application.getString(
R.string.leak_canary_notification_no_retained_object_content
)
)
.setAutoCancel(true)
.setContentIntent(NotificationReceiver.pendingIntent(application, CANCEL_NOTIFICATION))
val notification =
Notifications.buildNotification(application, builder, LEAKCANARY_LOW)
notificationManager.notify(
R.id.leak_canary_notification_no_retained_object_on_tap, notification
)
backgroundHandler.postDelayed(
scheduleDismissNoRetainedOnTapNotification,
DISMISS_NO_RETAINED_OBJECT_NOTIFICATION_MILLIS
)
lastDisplayedRetainedObjectCount = 0
return@post
}
SharkLog.d {
"Dumping the heap because user requested it" }
dumpHeap(retainedReferenceCount, retry = false, "user request")
}
重点在最后一行 dumpheap 真正开始收集hprof文件了。
Heap Dump也叫堆转储文件,是一个Java进程在某个时间点上的内存快照。Heap Dump是有着多种类型的。不过总体上heap dump在触发快照的时候都保存了java对象和类的信息。通常在写heap dump文件前会触发一次FullGC,所以heap dump文件中保存的是FullGC后留下的对象信息。
最后我们找到真正做事的是Debug去取的hprof文件。
未完,待续。。。
边栏推荐
- Why is yuancosmos so popular? Is the 10trillion yuan shouted by the market boasting or the truth?
- 【22暑期复建1】 Codeforces Round #791 (Div. 2)
- c语言---17 函数简介
- Mysql database DQL exercise
- Greedy interval problem (4)
- Mysql8 installation and environment configuration
- MySQL constraints
- Spark SQL Start(2.4.3)
- 2021-04-14
- ArcGIS应用(二十)Arcgis 栅格图像符号系统提示“This dataset does not have valid histogram required for classificati…”
猜你喜欢

LinkedList source code analysis
Mysql8 installation and environment configuration

China Mobile's mobile phone users grow slowly, but strive for high profit 5g package users

别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!

AutoCAD - five annotation shortcuts

Core and semiconductor "RF eda/ filter design platform" shines ims2022

2021-08-22
Learn redis with you (11) -- redis distributed lock

three.js模拟驾驶游览艺术展厅---打造超级相机控制器

数据库访问工具简介
随机推荐
2021-07-27
2021-07-27
Half optimized SQL
js图片分辨率压缩
2021-08-21
Palindromes (simple version)
Ensure database and cache consistency
启牛app下载证券开户,是安全的吗?有风险嘛?
Solution to cache inconsistency
2021-03-06
Spark RDD Programming Guide(2.4.3)
获取当前所在周的起始和结束的日期
Phantomjs实用代码段(持续更新中……)
Spark SQL Start(2.4.3)
The relationship between derivative and differential of function
2021-08-22
Summary of transport layer knowledge points
为 localStorage 添加过期时间
MySQL multi table operation exercise
Several soapdatamodules on Delphi soap WebService server side