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

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

Openmv and k210 of the f question of the 2021 video game call the openmv API for line patrol, which is completely open source.

Orb-slam2 source code learning (II) map initialization

ESP8266 RC522

DLS-42/6-4 DC110V双位置继电器

Xjy-220/43ac220v static signal relay

neo4j安装、运行以及项目的构建和功能实现

日志 logrus第三方库的使用

fluttertoast

孙宇晨接受瑞士媒体Bilan采访:熊市不会持续太久
随机推荐
mysql数据库基础:流程控制
【go】go 实现行专列 将集合进行转列
Double position relay dls-5/2 dc220v
ORB-SLAM2源码学习(二)地图初始化
visual studio 2019 快捷键备忘
fluttertoast
使用StrictMode-StrictMode原理(1)
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
DLS-20型双位置继电器 220VDC
Why build a personal blog
DX-11Q信号继电器
用Steam教育启发学生多元化思维
Interpreting the scientific and technological literacy contained in maker Education
软硬件基础知识学习--小日记(1)
dc_ Study and summary of labs--lab1
XJY-220/43AC220V静态信号继电器
Exploration and practice of "flow batch integration" in JD
dc_labs--lab1的学习与总结
Two position relay st2-2l/ac220v
解读创客教育所蕴含的科技素养