当前位置:网站首页>Thinking brought by strictmode -strictmode principle (5)
Thinking brought by strictmode -strictmode principle (5)
2022-07-01 01:28:00 【Strange uncle Lori Kong】
5. StrictMode The thinking that comes with
StrictMode in general , It is a reflection of the idea of inserting piles , It's also AOP Commonly used technology , Put stakes in various functions of the system that may cause problems , Wait for the problem to happen , Expose problems to StrictMode. The same idea can be applied to calculate the execution time of functions , Exposure occurs when a certain value is exceeded
There are two common ways to insert code , They are similar StrictMode This kind of static pile insertion , The other is at compile time , adopt ASM Other framework , Automatic pile insertion .
5.1 Automatic pile insertion
ASM Dynamic pile insertion , It can prevent developers from inserting piles into each function , Let's take the example of calculating function time , We can write two static functions , Then insert before and after the function starts .
fun sum(){
CalculateTime.start()
for loop(0 .. 100)
CalculateTime.start()
}
Similar to this , For some functions that do not need statistics , You can uninstall the blacklist of the configuration file , To filter , Of course, when it is actually used , More details , But in general
5.2 Dynamic pile insertion
Dynamic pile insertion , It can be understood as dynamic proxy , It can also be understood as Hook technology , We analyze the memory LeakCanary For example , analysis Service In case of leakage , In fact, it applies to dynamic agents , When a leak occurs , The stake works , Let's take a look at the source code
// Get one ActivityThread object
private val activityThreadClass by lazy {
Class.forName("android.app.ActivityThread") }
// perform ActivityThread Of currentActivityThread Method
private val activityThreadInstance by lazy {
activityThreadClass.getDeclaredMethod("currentActivityThread").invoke(null)!!
}
// The function variables , Storage uninstall uninstallActivityThreadHandlerCallback function
private var uninstallActivityThreadHandlerCallback: (() -> Unit)? = null
// The function variables , Storage uninstallActivityManager function
private var uninstallActivityManager: (() -> Unit)? = null
// Replace callback
swapActivityThreadHandlerCallback {
mCallback -> // Returned from the call mCallback
// assignment uninstallActivityThreadHandlerCallback This function variable , Used to do uninstall
uninstallActivityThreadHandlerCallback = {
swapActivityThreadHandlerCallback {
mCallback
}
}
// take Handler.Callback As swap The return value of , Pass to swapActivityThreadHandlerCallback
//kotlin Implicit return of , The last sentence
Handler.Callback {
msg ->
if (msg.what == STOP_SERVICE) {
val key = msg.obj as IBinder
activityThreadServices[key]?.let {
onServicePreDestroy(key, it)
}
}
mCallback?.handleMessage(msg) ?: false
}
}
In fact, the code here is mainly , To his own CallBack Replace ActivityThread Inside CallBack To monitor ActivityThread It monitors STOP_SERVICEd The news of , After that, an analysis of memory leakage is carried out
Okay ,StrictMode The whole series is finished . Hope you can get something
边栏推荐
- mysql数据库基础:流程控制
- [leetcode] sum of two numbers [1]
- 【学习笔记】倍增 + 二分
- Basic knowledge of software and hardware -- diary (1)
- About the general input operation mode of unity
- gin_gorm
- Basic knowledge 3 - standard unit library
- 【学习笔记】简单dp
- 分割链表[先取next再斩断链表防止断链]
- Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
猜你喜欢
奇偶链表[链表操作的两种大方向]
Principes de formation de la programmation robotique
基础知识之三——标准单元库
解析创客教育实践中的智慧原理
Note d'étude du DC: zéro dans le chapitre officiel - - Aperçu et introduction du processus de base
Poor students can also play raspberry pie
Call the classic architecture and build the model based on the classic
冲击继电器ZC-23/DC220V
Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.
K210 site helmet
随机推荐
Principes de formation de la programmation robotique
[Deepin] 常用集合
Orb-slam2 source code learning (II) map initialization
06. on several ways of redis persistence
Exploration and practice of "flow batch integration" in JD
Construction and beautification of personal blog
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
【队列】933. Number of Recent Calls
Training discipline principle of robot programming
日志 logrus第三方库的使用
技术人进阶画业务大图,手把手教学来了
【栈】921. Minimum Add to Make Parentheses Valid
使用StrictMode-StrictMode原理(1)
用recyclerReview展示Banner,很简单
ORB-SLAM2源码学习(二)地图初始化
Visual studio 2019 Download
DC学习笔记正式篇之零——综述与基本流程介绍
mustache语法
Use of typora
2021电赛F题openmv和K210调用openmv api巡线,完全开源。