Android的StrictMode InstanceCountViolationAndroid、StrictMode、InstanceCountViolation

由网友(☆歲yuè庩γΙη)分享简介:我正在我与StrictMode开发启动应用程序的记录在这里 StrictMode较低的平台版本并注意到,我不知道该怎么去想我也不能找到任何引用的错误消息。I am running my app with StrictMode activated in development as documented here S...

我正在我与StrictMode开发启动应用程序的记录在这里 StrictMode较低的平台版本 并注意到,我不知道该怎么去想我也不能找到任何引用的错误消息。

I am running my app with StrictMode activated in development as documented here StrictMode for lower platform versions and noticed an error message that I do not know what to think about nor can I find any reference.

我收到了 android.os.StrictMode $ InstanceCountViolation 实例值如:

实例= 3;极限= 2

instances=3; limit=2

现在我想知道:

A)是如何计算的限制 B)怎么能这样违反实际发生,那么我会考虑规避动作。

任何想法?

推荐答案

关键是StrictMode.sExpectedActivityInstanceCount和 incrementExpectedActivityCount decrementExpectedActivityCount

It's all in the code

The key is StrictMode.sExpectedActivityInstanceCount and incrementExpectedActivityCount and decrementExpectedActivityCount:

在增量被称为ActivityThread.performLaunchActivity 刚过创建活动实例。 在递减被称为在ActivityThread.performDestroyActivity 后的活性已经从应用程序中删除。 Increment is called in ActivityThread.performLaunchActivity just after creating the Activity instance. Decrement is called in ActivityThread.performDestroyActivity after the activity has been removed from the application.

因此​​,限制少每次的活动被破坏,但是如果一个实例被泄露了真正的实例数量将超过极限,以检测它是否泄露他们做一些GC魔术(在 decrementExpectedActivityCount ):

So the limit is less each time an activity is destroyed, however if an instance is leaked the real instance count will be bigger than the limit, to detect if it's leaked they do some GC magic (in decrementExpectedActivityCount):

    System.gc();
    System.runFinalization(); // added in https://github.com/android/platform_frameworks_base/commit/6f3a38f3afd79ed6dddcef5c83cb442d6749e2ff
    System.gc();

如果在此之后在GC未从应用程序的存储器它被认为是泄漏除去活性

if after this the GC didn't remove the activity from the app's memory it is considered a leak.

根据以上至prevent的唯一方法是确保有后的onDestroy 没有提到违规活动。问题是,有些可能有一些的WeakReference ■哪些仍然通过一些本地对象,这似乎有不同的生命周期进行访问。以下是我得出这样的结论:

Based on the above the only way to prevent is to make sure there are no references to the offending activity after onDestroy. The problem is that some there may be some WeakReferences which are still accessible through some native objects, which seem to have a different lifecycle. Here's how I came to this conclusion:

MyActivity 打了退堂鼓,看到日志消息之后 请堆转储(.hprof) 在打开的的Eclipse内存分析器的 运行OQL: SELECT *的instanceof full.package.name.of.MyActivity 选择全部用按Ctrl +点击或 Shift +单击 右键单击和合并到GC根的最短路径>所有引用的 after backing out from MyActivity and seeing the log message make a heap dump (.hprof) open it in Eclipse Memory Analyzer run OQL: select * from instanceof full.package.name.of.MyActivity select all with Ctrl+Click or Shift+Click right click and Merge Shortest Path to GC Roots > with all references

如果我们增加计数最初,我们将有更多的腿部空间就报告泄漏的具体上课前:

If we increase the count initially we'll have more legroom before it reports the leak for specific classes:

// Application.onCreate or nearby where you set up StrictMode detectActivityLeaks
Method incrementExpectedActivityCount = StrictMode.class.getMethod("incrementExpectedActivityCount", Class.class)
incrementExpectedActivityCount.invoke(null, MyActivity.class);
incrementExpectedActivityCount.invoke(null, MyActivity2.class);

进一步阅读

WeakReferences 的Eclipse内存分析器(MAT) History对 StrictMode

Further reading

WeakReferences Eclipse Memory Analyzer (MAT) History of StrictMode

阅读全文

相关推荐

最新文章