当前位置:网站首页>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文件。
未完,待续。。。
边栏推荐
- Greedy interval problem (1)
- 2021-05-02
- 对 cookie 的添加/获取和删除
- . Net 5.0 realizes the source code analysis of the oidc authentication part of single sign on through identityserver4
- Ensure database and cache consistency
- [redisson] source code analysis of multilock
- R language builds a binary classification model based on H2O package: using H2O GLM constructs regularized logistic regression model and uses H2O AUC value of AUC calculation model
- 5 minutes to quickly launch web applications and APIs (vercel)
- 多种方案实现图片的懒加载
- 保证数据库和缓存的一致性
猜你喜欢

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

2021-04-16

Huawei cloud recruits partners in the field of industrial intelligence to provide strong support + commercial realization

Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading

leetcode. 11 --- container with the most water

c语言---17 函数简介

JSBridge
Solution to cache inconsistency

The xinjietu x70s has been listed for 87900 times and has leapfrogged the class in space safety. It is worthy of being a 7-seat SUV of the National University of China

2021-04-05
随机推荐
2020-12-20
A case of misuse of append
Spark SQL accessing JSON and JDBC data sources
Use full function simulation design method
2021-03-06
Generate detailed API and parameters of QR code using qrcodejs2
R language data Table data import practice: data Rename the name of the table data column (rename)
Relationship between adau1452 development system interface and code data
How much do you know about the cause of amplifier distortion?
ArcGIS application (20) the ArcGIS grid image symbol system prompts "this dataset does not have valid histogram required for classificati..."
The relationship between derivative and differential of function
Experiment 4 operation comparison between NoSQL and relational database
Help customers' digital transformation and build a new operation and maintenance system
C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test
In the middle of the year, we will promote the integration of worry free, and the value-added package will be reduced by 6
为 localStorage 添加过期时间
Which securities company is the safest and best choice for stock trading account opening
Spark SQL Generic Load/Save Functions(2.4.3)
Lua -- iterator, module, meta table
Greedy interval problem (3)