当前位置:网站首页>StrictMode分析Activity泄漏-StrictMode原理(3)
StrictMode分析Activity泄漏-StrictMode原理(3)
2022-07-01 00:40:00 【怪叔叔萝莉控】
3. Activity的泄漏
StrictMode对于Activity的泄漏检测也是有的。
泄漏的日志:
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的activity泄漏检测,用的是引用计数法,大致思路分为三步:
- 使用map存储Activity的类与计数Map<Class,Count>
StrictMode.java
private static final HashMap<Class, Integer> sExpectedActivityInstanceCount = new HashMap<>();
- 在Activity启动阶段增加计数
ActivityThread.java
/** Core implementation of activity launch. */
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
StrictMode.incrementExpectedActivityCount(activity.getClass());
}
- 在Activity的回收阶段减少计数
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 增加计数实现
- 是否开启了检测开关
- 是否有计数,如果有+1
- 放回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 减少计数实现
- 是否开启了检测开关
- 是否有计数,如果有-1
- GC
- 重新寻找是否有引用,有几个引用
- 引用大于计数,则认为有泄漏
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();
//计算有几个引用
long instances = VMDebug.countInstancesOfClass(klass, false);
if (instances > limit) {
onVmPolicyViolation(new InstanceCountViolation(klass, instances, limit));
}
}
边栏推荐
猜你喜欢

Principes de formation de la programmation robotique
![Split the linked list [take next first and then cut the linked list to prevent chain breakage]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
Split the linked list [take next first and then cut the linked list to prevent chain breakage]

Docker deployment MySQL 8

K210 site helmet

Left join displays the specified value when the left join matching data is null

The quantity and quality of the devil's cold rice 101; Employee management; College entrance examination voluntary filling; Game architecture design

Dx-11q signal relay

K210工地安全帽
![[go] go implements row column conversion of sets](/img/d9/6272e55b2d9c6b6fbdf2537773bb83.png)
[go] go implements row column conversion of sets

Poor students can also play raspberry pie
随机推荐
Oracle data integrity
Installing mongodb database in Windows Environment
Dls-42/6-4 dc110v double position relay
K210 access control complete
酒旅板块复苏,亚朵继续上市梦,距离“新住宿经济第一股“还有多远?
The principle of journal node
Length of the longest integrable subarray
ORB-SLAM2源码学习(二)地图初始化
Analyzing the wisdom principle in maker education practice
Training discipline principle of robot programming
ESP8266 RC522
Kongyiji's first question: how much do you know about service communication?
A proper job is a good job
Green, green the reed. dew and frost gleam.
[daily record] - bug encountered in BigDecimal division operation
06.论Redis持久化的几种方式
分割链表[先取next再斩断链表防止断链]
Pytorch installs and uses GPU acceleration
Practical shell knowledge
Web interface testing of software testing