All this in code
Key StrictMode.sExpectedActivityInstanceCount and incrementExpectedActivityCount and decrementExpectedActivityCount :
Thus, the limit less each time the action is destroyed, however if the instance is leaked, the real counter of the instances will be more than the limit, to determine if it has leaked, they perform some GC magic (in decrementExpectedActivityCount ).
System.gc(); System.runFinalization(); // added in https://github.com/android/platform_frameworks_base/commit/6f3a38f3afd79ed6dddcef5c83cb442d6749e2ff System.gc();
if after that the GC does not delete the activity from the application memory, this is considered a leak.
Conclusion
Based on the foregoing, the only way to prevent it is to make sure that there are no links to abusive activity after onDestroy . The problem is that some may be some WeakReference that are still accessible through some native objects that seem to have a different life cycle. This is how I came to this conclusion:
- after returning from
MyActivity and viewing the log message - make a bunch of dump (.hprof)
- open it in Eclipse Memory Analyzer
- run OQL:
select * from instanceof full.package.name.of.MyActivity - select everything with Ctrl + Click or Shift + Click
- right click and merge the shortest path to GC Roots> with all the links
Bypass
If we increase the amount initially , we will have more legroom before he reports a leak for certain 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);
Further reading
TWiStErRob Dec 28 '14 at 18:38 2014-12-28 18:38
source share