我运行了这个命令 adb shell dumpsys meminfo <package_name>
并得到了
Objects
Views: 74 ViewRootImpl: 1
AppContexts: 3 Activities: 1
Assets: 4 AssetManagers: 4
Local Binders: 11 Proxy Binders: 20
Death Recipients: 0
OpenSSL Sockets: 2
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
按下后退按钮并再次启动应用程序并重复此过程几次后得到:
Objects
Views: 1408 ViewRootImpl: 8
AppContexts: 14 Activities: 12
Assets: 5 AssetManagers: 5
Local Binders: 13 Proxy Binders: 32
Death Recipients: 0
OpenSSL Sockets: 1
SQL
MEMORY_USED: 0
PAGECACHE_OVERFLOW: 0 MALLOC_SIZE: 0
注意 Activities 和 AppContexts 计数,它们只是通过一次又一次地启动应用程序而不断增加。这是否意味着我有内存泄漏?
我阅读了这份文件 https://developer.android.com/tools/debugging/debugging-memory.html#ViewingAllocations其中说:
AppContexts and Activities The number of app Context and Activity objects that currently live in your process. This can be useful to quickly identify leaked Activity objects that can’t be garbage collected due to static references on them, which is common. These objects often have a lot of other allocations associated with them and so are a good way to track large memory leaks.
最佳答案
Android 中内存泄漏的最常见原因是保留实例
- Activity
- 背景
- 位图/可绘制
- 资源
在一些比 Activity 本身生命周期更长的容器中。那些容器可能是
- 回调
- 静态变量
观看https://www.youtube.com/watch?v=_CruQY55HOk对于出色的分析,如何使用内存分析器工具和堆转储查找内存泄漏。
关于android - Android 可能的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26910668/