android - 背景可绘制对象出现 outOfMemoryError

标签 android memory out-of-memory analyzer mat

我读了很多关于位图内存泄漏的文章,但我无法解决我的问题。我的一个应用程序在我的手机中运行良好,但在其他应用程序中,我遇到了 outOfMemoryError 位图大小超过 VM 的情况。

我的问题是我不明白如何释放这些资源。我有一个游戏,其中包含多个 Activity 和对话框,其中 XML 文件中的背景可绘制,有时作为布局背景,有时作为全屏的 ImageView。我怎样才能释放这段内存?我无法使用回收,当我销毁 Activity 或关闭对话框时,我使用 unbindDrawables 方法,但这似乎还不够。

我的 MAT 概述: MAT overview

我怀疑 MAT 内存泄漏: Memory Leak Suspects 这是我的支配树:

 `Class Name                                                                 | Shallow Heap | Retained Heap | Percentage
-----------------------------------------------------------------------------------------------------------------------
android.graphics.Bitmap @ 0x42689c38                                       |           48 |     9.437.248 |     15,43%
class android.content.res.Resources @ 0x40daacb8 System Class              |           48 |     5.154.752 |      8,43%
android.graphics.Bitmap @ 0x41674f78                                       |           48 |     4.194.368 |      6,86%
android.graphics.Bitmap @ 0x41b1f978                                       |           48 |     4.194.368 |      6,86%
android.graphics.Bitmap @ 0x415954e8                                       |           48 |     4.096.064 |      6,70%
android.graphics.Bitmap @ 0x416565a8                                       |           48 |     4.096.064 |      6,70%
android.graphics.Bitmap @ 0x41ab4b70                                       |           48 |     4.096.064 |      6,70%
android.graphics.Bitmap @ 0x425218a8                                       |           48 |     4.096.064 |      6,70%
android.graphics.Bitmap @ 0x41664030                                       |           48 |     2.389.984 |      3,91%
android.graphics.Bitmap @ 0x41ab2b88                                       |           48 |     2.187.648 |      3,58%
android.graphics.Bitmap @ 0x42687718                                       |           48 |     1.740.864 |      2,85%
android.graphics.Bitmap @ 0x41ab2818                                       |           48 |     1.521.024 |      2,49%
android.graphics.Bitmap @ 0x40f796e8                                       |           48 |     1.127.584 |      1,84%
android.graphics.Bitmap @ 0x41a7f6a0                                       |           48 |     1.048.640 |      1,71%
android.graphics.Bitmap @ 0x41ab5348                                       |           48 |     1.048.640 |      1,71%
android.graphics.Bitmap @ 0x42687b08                                       |           48 |     1.048.640 |      1,71%
android.graphics.Bitmap @ 0x4268dce0                                       |           48 |     1.048.640 |      1,71%
android.graphics.Bitmap @ 0x426a59c0                                       |           48 |     1.048.640 |      1,71%
android.graphics.Bitmap @ 0x41bfce38                                       |           48 |       890.944 |      1,46%
android.graphics.Bitmap @ 0x416796d8                                       |           48 |       750.784 |      1,23%
android.graphics.Bitmap @ 0x41596ec0                                       |           48 |       262.208 |      0,43%
android.graphics.Bitmap @ 0x41694318                                       |           48 |       262.208 |      0,43%
android.graphics.Bitmap @ 0x41a98cb0                                       |           48 |       262.208 |      0,43%
android.graphics.Bitmap @ 0x41bfefc0                                       |           48 |       262.208 |      0,43%
android.graphics.Bitmap @ 0x42790e90                                       |           48 |       262.208 |      0,43%
class android.text.Html$HtmlParser @ 0x40e7ade0 System Class               |            8 |       126.632 |      0,21%
class java.lang.ref.FinalizerReference @ 0x40d0fef0 System Class           |           16 |       118.464 |      0,19%
class libcore.icu.TimeZones @ 0x40d152b0 System Class                      |           40 |       103.432 |      0,17%
android.graphics.Bitmap @ 0x40f450a0                                       |           48 |       100.288 |      0,16%
com.android.org.bouncycastle.jce.provider.BouncyCastleProvider @ 0x40e0e0d8|          112 |        80.096 |      0,13%
android.graphics.Bitmap @ 0x426a4718                                       |           48 |        66.624 |      0,11%
android.graphics.Bitmap @ 0x4158c1a0                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41592a58                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41655380                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41a90980                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41aa9718                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41ab4178                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41ab43b0                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41ab8440                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41adc988                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x41b0ae10                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x422e46c0                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x42520580                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x42684680                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x42686db8                                       |           48 |        65.600 |      0,11%
android.graphics.Bitmap @ 0x4268ab38                                       |           48 |        65.600 |      0,11%
android.graphics.drawable.StateListDrawable$StateListState @ 0x426a5800    |           80 |        63.064 |      0,10%
android.graphics.drawable.StateListDrawable$StateListState @ 0x426f3458    |           80 |        63.064 |      0,10%
android.graphics.drawable.NinePatchDrawable @ 0x426f2bc0                   |           72 |        62.464 |      0,10%
android.graphics.drawable.NinePatchDrawable @ 0x427404e8                   |           72 |        62.464 |      0,10%
android.graphics.NinePatch @ 0x426b5030                                    |           32 |        62.288 |      0,10%
Total: 51 of 15.374 entries                                                |              |               |           
-----------------------------------------------------------------------------------------------------------------------
`

我从 MAT 开始,我的内存力太差了吗?如果不使用位图,如何正确释放背景内存?

谢谢。

最佳答案

不使用时回收位图。蜂窝位图保留在堆上之后。

   bitmaps.recycle();

http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html 。 上面的链接有一个关于如何避免内存泄漏的主题。

此外,如果您有高分辨率图像,则应该缩小尺寸。请参阅将缩小版本加载到内存中

下的主题。

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

假设您从 Activity A 导航到 Activity B。 在onPause() 中回收位图。在 onResume() 中加载位图。对加载位图的每个 Activity 执行此操作。

既然你使用MAT,你应该查看下面的链接来查找和解决内存泄漏 http://www.youtube.com/watch?v=_CruQY55HOk

关于android - 背景可绘制对象出现 outOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15790156/

相关文章:

android - 加载程序集时出现 monodroid 异常 -> 无法加载程序集 System.Net

c# - 是否可以在不使用探查器的情况下跟踪 C# 应用程序中的内存使用情况?

python - 内存中似乎有什么东西被困住了

android - 位图内存管理

android - 显示在 recyclerview 中点击的项目的详细信息

Android系统主题为通知图标添加自己的背景

android - 我如何在Android中使用超声波通过语音传输数据?

c++ - 二维数组的内存问题

recursion - F# System.OutOfMemoryException 与递归调用

java - 使用 iText : java. lang.OutOfMemoryError:请求的数组大小超出 VM 限制