我讀了很多關於Bitmap的內存泄漏,但我無法解決我的問題。我有一個應用程序在我的手機中工作正常,但在其他人中,我得到outOfMemoryError位圖大小超過虛擬機。outOfMemoryError與背景可繪製
我的問題是,我不知道如何釋放這些資源。我有一個遊戲,其中包含多個活動,並在XML文件中帶有可繪製背景的對話框,有時用作佈局背景,有時用作全屏的ImageView。我如何釋放這個記憶?我無法使用回收站,當我銷燬活動或解散對話框時,我使用unbindDrawables methot,但似乎還不夠。
我太概述:
和我太可疑內存泄漏: 這是我的支配樹:
`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,我怎樣才能正確釋放背景的內存?
謝謝。
我把我的IMGS中可繪製在XML。我應該從drawables創建位圖,並將它們作爲背景以bitmap.recycle()方式釋放後期? – k0nig
加載onResume()中的圖像並在OnPause()中回收它們。您可以在onCreate()中設置佈局。 OnCreate()在活動生命週期中被調用一次。所以加載到onResume()。這是減少內存使用 – Raghunandan
嘗試解開'onPause()'中的drawables。將它們綁定在'onResume()'中。 – Raghunandan