的OutOfMemoryError背景可绘制 [英] outOfMemoryError with background drawables
问题描述
我读了很多关于使用位图的内存泄漏,但我解决不了我的问题。我有一个应用程序工作正常,在我的手机,但在其他我发现了OutOfMemoryError异常位大小超过虚拟机。
I read a lot about memory leaks with Bitmaps but I can't solve my problem. I have an app works fine in my phone, but in others I'm getting the outOfMemoryError bitmap size exceeds VM.
我的问题是,我不知道如何释放这个资源。我有一个游戏有几个活动和对话与背景在XML文件中可绘制,有时作为布局的背景,有时作为一个ImageView的到全屏。我怎样才能释放此内存?我不能用循环,我使用unbindDrawables methot当我破坏活动或辞退一个对话框,但似乎还不够。
My problem is that I don't understand how to free this resources. I have a game with several activities and dialog with drawables in background in XML files, sometimes as a layout background and sometimes as a ImageView to fullscreen. How I can free this memory? I can't use recycle, I'm use unbindDrawables methot when I destroy an activity or dismiss a dialog, but it seems not enough.
我太概述:
My MAT overview:
和我MAT内存泄漏嫌疑人: 这是我的支配树:
and my MAT memory leak suspects: This is my dominator tree:
`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,太糟糕了我的记忆中使用?我怎样才能正确释放的背景的内存,如果我不使用位图?
I'm starting with MAT, is too bad my memory use? How can I free the memory of backgrounds correctly if I don't use BitMaps?
感谢。
推荐答案
回收位图时不使用。经过蜂窝位图留在堆上。
Recycle bitmaps when not in use. After honeycomb bitmaps stay on heap.
bitmaps.recycle();
http://android-developers.blogspot.de/ 2009/01 /避免内存-leaks.html 。 上面的链接对如何避免内存泄漏的话题。
http://android-developers.blogspot.de/2009/01/avoiding-memory-leaks.html. The link above has a topic on how to avoid memory leaks.
此外,如果您有高分辨率的图像,则应该缩小。见在该主题的加载一个缩小版到内存中。
Also if you have high resolution image , you should scale down. See the topic under Load a Scaled Down Version into Memory.
http://developer.android.com/training/displaying-位图/负载bitmap.html
假设你浏览的形式actiivty A到B.
在的onPause()
回收位图。在 onResume()
加载位图。这样做的每一个地方加载位图的活动。
Suppose you navigate form actiivty A to B.
In onPause()
recycle bitmaps. In onResume()
load bitmaps. Do this for every activity where you load bitmaps.
由于您使用垫,你应该看看下面的链接,发现并解决内存泄漏 http://www.youtube.com/watch?v=_CruQY55HOk
Since you use MAT, you should look at the link below to find and solve memory leaks http://www.youtube.com/watch?v=_CruQY55HOk
这篇关于的OutOfMemoryError背景可绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!