Question

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.

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 overview

and my MAT memory leak suspects: 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                                                |              |               |           
-----------------------------------------------------------------------------------------------------------------------
`

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?

Thanks.

Was it helpful?

Solution

Recycle bitmaps when not in use. After honeycomb bitmaps stay on heap.

   bitmaps.recycle();

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-bitmaps/load-bitmap.html

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.

Since you use MAT, you should look at the link below to find and solve memory leaks http://www.youtube.com/watch?v=_CruQY55HOk

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top