当前位置:网站首页>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
边栏推荐
猜你喜欢

Exploration and practice of "flow batch integration" in JD

技术人进阶画业务大图,手把手教学来了

Service grid ASM year end summary: how do end users use the service grid?

Exploring the road of steam education innovation in the Internet Era

友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)

Orb-slam2 source code learning (II) map initialization

Analyzing the wisdom principle in maker education practice

Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images

解读创客教育所蕴含的科技素养

软件开发完整流程
随机推荐
MATLAB 最远点采样(FPS改进版)
【Qt5-基础篇_1】从0开始,德天老师和你一起学习——窗口简介
Solve idea:class' xxx 'not found in module' xxx‘
Open3D 点云颜色渲染
[learning notes] simple DP
蒹葭苍苍,白露为霜。
探索互联网时代STEAM教育创新之路
K210 access control complete
Unknown database连接数据库错误
基础知识之二——STA相关的基本定义
【模拟】922. Sort Array By Parity II
Inspire students' diversified thinking with steam Education
小程序自定义宫格
技术人进阶画业务大图,手把手教学来了
Why not two or four TCP handshakes
基础知识之三——标准单元库
【qt5-tab标签精讲】Tab标签及内容分层解析
neo4j安装、运行以及项目的构建和功能实现
Introduction and principle analysis of cluster and LVS
Document service design