当前位置:网站首页>Leakcanary source code (2)
Leakcanary source code (2)
2022-06-22 23:22:00 【Android_ Zhouzhihao 378623234】
Then the source code of the previous article , Analyze the meaning of the core code in this figure :
moveToRetained Method
And then there was onObjectRetained Method 
It is actually added here in the code below
This method points to scheduleRetainedObjectCheck, As you can see, it calls heapDumpTrigger.scheduleRetainedObjectCheck()
, Let's see. heapDumpTrigger.scheduleRetainedObjectCheck(), Look at the breakpoint method checkRetainedObjects()
a key 3 individual , The first place is to call gc, The second place , Check for residual object, If there is any residue in the third place , To inform users , Is a memory leak alert
Let's look at the details in the third place. The key point is This PendingIntent, Use FLAG_IMMUTABLE The tag builds a that cannot be modified PendingIntent.
call NotificationManagerCompat.notify() Then the work is finished . When the system displays a notification , And when the user clicks the notification , It will be in our PendingIntent On the call PendingIntent.send(), To start the broadcast of our application , Only then will I go heap dump , This is a bit lazy to load into the meaning of it , ha-ha . It's all details .
(sample in Click on recreate Button on the There is one checkRetainedObjects scheduleRetainedObjectCheck checkRetainedObjects loop , I don't know what the intention is )
In the picture below , We can see that after we click on the notification , See what the code does 

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")
}
Focus on the last line dumpheap Really start collecting hprof The file .
Heap Dump Also called heap dump file , It's a Java Memory snapshot of the process at a certain point in time .Heap Dump There are many types of . But on the whole heap dump It is saved when the snapshot is triggered java Information about objects and classes . Usually writing heap dump It will trigger once before the file FullGC, therefore heap dump What's in the file is FullGC The object information left after .
In the end, we found that what we really did was Debug To pick up hprof file .
unfinished , To be continued ...
边栏推荐
- SSH method 2 for adding node nodes in Jenkins
- Freshman girls' nonsense programming is popular! Those who understand programming are tied with Q after reading
- 'dare not doubt the code, but have to doubt the code 'a network request timeout analysis
- Greedy interval problem (1)
- Spark SQL Generic Load/Save Functions(2.4.3)
- Safe and reliable! Tianyi cloud data security management platform passed the evaluation
- flutter外包,承接flutter项目
- Introduction and example application of PostgreSQL string separator function (regexp\u split\u to\u table)
- Greedy interval problem (2)
- c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试
猜你喜欢
随机推荐
【Kubernetes 系列】Kubernetes 概述
flink同步mysql数据到ES
Is it safe to open a securities account by downloading the qiniu app? Is there a risk?
Greedy interval problem (3)
剑指 Offer 05. 替换空格
2020-12-20
1. class inheritance (point)
2021-08-21
OJ每日一练——删除单词后缀
wallys/WiFi6 MiniPCIe Module 2T2R 2 × 2.4GHz 2x5GHz
WebRTC系列-网络传输之4Connection排序
OJ每日一练——找第一个只出现一次的字符
Greedy distribution problem (1)
2020-12-04
SSH method 2 for adding node nodes in Jenkins
How to continuously improve performance| DX R & D mode
输出字符串中最长的单词
2021-03-06
2021-01-29
OJ每日一练——跨越2020








