android - Android 可能的内存泄漏

标签 android memory-leaks out-of-memory

我运行了这个命令 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

注意 ActivitiesAppContexts 计数,它们只是通过一次又一次地启动应用程序而不断增加。这是否意味着我有内存泄漏?

我阅读了这份文件 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/

相关文章:

java - Android 内存不足错误

python - Python 段错误中修改的 Ackermann 函数

java - 通过相同的代码或应用任何逻辑在所有设备中打开 File-Intent

COM:我可以在不调用 Release 的情况下调用 CoUninitialize 吗?

java - 一个带-Xmx1024m的Java进程怎么会占用3GB常驻内存呢?

java - 如何验证Java中的内存泄漏

java - 如何通过生成线程来命中 java.lang.OutOfMemoryError?

java - 我应该在 Android 上支持对象池而不是新对象吗?

android - OpenGL ES 1.1 : How to substitute glBlendFuncSeparate?

Android 字符串数组显示百分号