当前位置:网站首页>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));
}
}
边栏推荐
- The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
- C # Generate PPK files in Putty format (passthrough support)
- 自定义注解实现校验
- 友盟(软件异常实时监听的好帮手:Crash)接入教程(有点基础的小白最易学的教程)
- ESP8266 RC522
- Exploring the road of steam education innovation in the Internet Era
- Implementation of date class
- Locking relay ydb-100, 100V
- Error msb8031: building an MFC project for a non Unicode character set is deprecated
- 双位置继电器ST2-2L/AC220V
猜你喜欢

The principle of journal node

High quality pump SolidWorks model material recommended, not to be missed

Golang treasure house recommendation
![[go] go implements row column conversion of sets](/img/d9/6272e55b2d9c6b6fbdf2537773bb83.png)
[go] go implements row column conversion of sets

Analyzing the wisdom principle in maker education practice

DX-11Q信号继电器

Basic knowledge of software and hardware -- diary (1)
![[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!]

集群与LVS介绍及原理解析

機器人編程的培訓學科類原理
随机推荐
Kongyiji's first question: how much do you know about service communication?
Koa koa-combine-routers 分路由管理
None of the following candidates is applicable because of a receiver type mismatch
Docker deployment MySQL 8
Dls-42/6-4 dc110v double position relay
关于Unity一般的输入操作方式
Dx-11q signal relay
High quality pump SolidWorks model material recommended, not to be missed
Authentication principle of Ranger plug-in
孔乙己第一问之服务通信知多少?
Gavin's insight on the transformer live broadcast course - rasa project's actual banking financial BOT Intelligent Business Dialogue robot system startup, language understanding, dialogue decision-mak
Cmu15445 (fall 2019) project 1 - buffer pool details
The communication mechanism and extension of Supervisor
Analyzing the wisdom principle in maker education practice
pytorch编程知识(2)
Fluent JSON serialization deserialization
Flutter Error: Cannot run with sound null safety, because the following dependencies don‘t support
A letter to 5000 fans!
2022 is half way through. It's hard to make money
用recyclerReview展示Banner,很简单