Large memory chunk not garbage collected

大城市里の小女人 提交于 2019-12-03 05:13:20

Activity destruction does not imply class destruction, just because you don't see the class doesn't mean the OS (Android in this case) lost all references to it and end it. That is why even in the documentations they specify to clean out any handles and objects you no longer need to prevent memory leaks. Cheers.

A few things:

1) You can't judge whether or not your Activity is being leaked just by watching the GC logs; each JVM implementation is free to choose when to garbage-collect objects, even if nothing is referencing them. Note that it IS required to garbage-collect before it throws an OOM error... but if it has enough memory available, it may choose to keep 10 of your activities in memory and then collect them all at once.

2) There may be internal Android structures that keep a reference to your Activity for longer than the lifecycle of the Activity... and the developer has no control over that. For that reason, it's recommended that the Activity not reference any large amounts of data (or if it does, it should explicitly release those references in onDestroy (or even in onPause if you want to be more aggressive).

3) Some JVMs optimize at runtime, such that a chunk of memory that is never written to or accessed is never actually allocated in physical memory. Maybe for that reason, your test is not valid on newer versions of Android. To get around this, you can add a loop that sets some values in the array to random values, and then another loop somewhere else in the code that reads them; this way the JVM is forced to allocate the memory.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!