当前位置:网站首页>Strictmode analysis activity leakage -strictmode principle (3)
Strictmode analysis activity leakage -strictmode principle (3)
2022-07-01 01:28:00 【Strange uncle Lori Kong】
3. Activity Leakage of
StrictMode about Activity There is also a leak detection .
Leaked logs :
D/StrictMode: StrictMode policy violation: android.os.strictmode.InstanceCountViolation: class com.ifreedomer.strictmode.activity.TestLeakedActivity; instances=3; limit=1
at android.os.StrictMode.setClassInstanceLimit(StrictMode.java:1)
StrictMode Of activity Leak detection , It's counting by reference , The general idea is divided into three steps :
- Use map Storage Activity Class and count of Map<Class,Count>
StrictMode.java
private static final HashMap<Class, Integer> sExpectedActivityInstanceCount = new HashMap<>();
- stay Activity The start-up phase increases the count
ActivityThread.java
/** Core implementation of activity launch. */
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
StrictMode.incrementExpectedActivityCount(activity.getClass());
}
- stay Activity The recycle phase of reduces the count
ActivityThread.java
/** Core implementation of activity destroy call. */
void performDestroyActivity(ActivityClientRecord r, boolean finishing,
int configChanges, boolean getNonConfigInstance, String reason) {
StrictMode.decrementExpectedActivityCount(activityClass);
}
3.1 Increase the count to achieve
- Whether the detection switch is turned on
- Whether there is counting , If there is +1
- Put back map
public static void incrementExpectedActivityCount(Class klass) {
if (klass == null) {
return;
}
synchronized (StrictMode.class) {
if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
return;
}
// Use the instance count from InstanceTracker as initial value.
Integer expected = sExpectedActivityInstanceCount.get(klass);
Integer newExpected =
expected == null ? InstanceTracker.getInstanceCount(klass) + 1 : expected + 1;
sExpectedActivityInstanceCount.put(klass, newExpected);
}
}
3.2 Reduce count to achieve
- Whether the detection switch is turned on
- Whether there is counting , If there is -1
- GC
- Look again for references , There are several references
- Reference is greater than count , It is considered that there is leakage
public static void decrementExpectedActivityCount(Class klass) {
if (klass == null) {
return;
}
final int limit;
synchronized (StrictMode.class) {
if ((sVmPolicy.mask & DETECT_VM_ACTIVITY_LEAKS) == 0) {
return;
}
Integer expected = sExpectedActivityInstanceCount.get(klass);
int newExpected = (expected == null || expected == 0) ? 0 : expected - 1;
if (newExpected == 0) {
sExpectedActivityInstanceCount.remove(klass);
} else {
sExpectedActivityInstanceCount.put(klass, newExpected);
}
// Note: adding 1 here to give some breathing room during
// orientation changes. (shouldn't be necessary, though?)
limit = newExpected + 1;
}
// Quick check.
int actual = InstanceTracker.getInstanceCount(klass);
if (actual <= limit) {
return;
}
System.gc();
System.runFinalization();
System.gc();
// Calculation has several references
long instances = VMDebug.countInstancesOfClass(klass, false);
if (instances > limit) {
onVmPolicyViolation(new InstanceCountViolation(klass, instances, limit));
}
}
边栏推荐
- StrictMode卡顿与泄漏检测-StrictMode原理(2)
- TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to
- 【Qt5-基础篇_1】从0开始,德天老师和你一起学习——窗口简介
- 双位置继电器DLS-5/2 DC220V
- (学习力+思考力) x 行动力,技术人成长的飞轮效应总结
- 软件开发完整流程
- 自定义注解实现校验
- The liquor and tourism sector recovers, and Yaduo continues to dream of listing. How far is it from "the first share of the new accommodation economy"?
- 蒹葭苍苍,白露为霜。
- JS方法大全的一个小文档
猜你喜欢
基础知识之三——标准单元库
Split the linked list [take next first and then cut the linked list to prevent chain breakage]
WIN11中MathType编辑中“打开数学输入面板”是灰色不可编辑
孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
数字IC设计流程总结
ESP8266 RC522
Basic knowledge II - Basic definitions related to sta
友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
gin_gorm
Koa koa combine routes sub route management
随机推荐
gin_gorm
JS方法大全的一个小文档
Service grid ASM year end summary: how do end users use the service grid?
奇偶链表[链表操作的两种大方向]
基础知识之三——标准单元库
K210工地安全帽
小程序自定义宫格
使用StrictMode-StrictMode原理(1)
K210 access control complete
Opencv basic operation 2 realizes label2rgb and converts gray-scale images into color images
DLS-20型双位置继电器 220VDC
ESP8266 RC522
Construction and beautification of personal blog
MATLAB 最远点采样(FPS改进版)
【office办公-pdf篇】pdf合并与拆分让我们摆脱付费软件的功能限制好不好
qt5-MVC:数据可视化的层次揭秘
JS to convert numbers into Chinese characters for output
【模拟】922. Sort Array By Parity II
【qt5-tab标签精讲】Tab标签及内容分层解析
用Steam教育启发学生多元化思维