当前位置:网站首页>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));
}
}
边栏推荐
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
- Length of the longest integrable subarray
- What is the difference between Pipeline and Release Pipeline in azure devops?
- 【go】go 实现行专列 将集合进行转列
- 染色法判断二分图
- 二十多年来第一次!CVPR最佳学生论文授予中国高校学生!
- Double linked list: initialize insert delete traversal
- Impact relay zc-23/dc220v
- [go] go implements row column conversion of sets
- 蒹葭苍苍,白露为霜。
猜你喜欢

【go】go 实现行专列 将集合进行转列

Locking relay ydb-100, 100V
![奇偶链表[链表操作的两种大方向]](/img/4e/ce860bc172bb75f456427ba26a7842.png)
奇偶链表[链表操作的两种大方向]
![分割链表[先取next再斩断链表防止断链]](/img/eb/708ab20c13df75f4dbd2d6461d3602.png)
分割链表[先取next再斩断链表防止断链]

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

双位置继电器DLS-5/2 DC220V

Koa koa-combine-routers 分路由管理

Interpreting the scientific and technological literacy contained in maker Education

Golang treasure house recommendation

Practical shell knowledge
随机推荐
Listview in flutter application development
Problem solving: how to manage thread_local pointer variables
K210 access control complete
分割链表[先取next再斩断链表防止断链]
Windows环境下安装MongoDB数据库
Oracle data integrity
【原创】 PLSQL 索引排序优化
软硬件基础知识学习--小日记(1)
Poor students can also play raspberry pie
Day31-t1380-2022-02-15-not answer by yourself
High quality pump SolidWorks model material recommended, not to be missed
The longest selling mobile phone in China has been selling well since its launch, crushing iphone12
MATLAB 最远点采样(FPS改进版)
leetcode 474. Ones and zeroes (medium)
Analysis of blocktoken principle
双位置继电器ST2-2L/AC220V
Hoo research | coinwave production - nym: building the next generation privacy infrastructure
Set different background colors for the border and text of the button
(学习力+思考力) x 行动力,技术人成长的飞轮效应总结
Koa koa-combine-routers 分路由管理