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

ORB-SLAM2源码学习(二)地图初始化

基础知识之三——标准单元库

XJY-220/43AC220V静态信号继电器

Construction and beautification of personal blog

Looksrare team's "cash out" caused disturbance

使用 C# 创造 ASCII 艺术

gin 配置文件

个人博客搭建与美化

Orb-slam2 source code learning (II) map initialization
![[network packet loss and network delay? This artifact can help you deal with everything!]](/img/c4/f733b23327458b9266b9cbcccb6f14.png)
[network packet loss and network delay? This artifact can help you deal with everything!]
随机推荐
StrictMode分析Registion-StrictMode原理(4)
Dls-20 double position relay 220VDC
K210工地安全帽
Typora的使用
Why build a personal blog
Technical personnel advanced to draw a big picture of business, hand-in-hand teaching is coming
基础知识之一——STA基础概述
DX-11Q信号继电器
Interpreting the scientific and technological literacy contained in maker Education
Installing mongodb database in Windows Environment
文件服务设计
K210 site helmet
Open3D 点云包围盒
MFC TCP communication server client demo notes vs2019
C语言一点点(未来可会增加)
Analyzing the wisdom principle in maker education practice
Training discipline principle of robot programming
一些本质的区别
Digital IC design process summary
Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)