我有一個應用程序,顯示了很多活動,每個顯示一個位圖,適合100%的屏幕寬度,通常超過100%的高度。Bitmap OutOfMemoryError即使使用回收()
當你觸摸屏幕,新的活動被創建,並且在之前的活動被稱爲光潔度和回收爲位圖:
public void clean() {
if(this.myBitmap != null){
Log.d(DEBUG_TAG, " Cleaning "+this);
this.myBitmap.recycle();
this.myBitmap=null;
System.gc();
}
}
的問題是,所需的內存被不斷擴張和當我推出超過12-13的活動,這些消息在被logcat中顯示:
05-05 16:03:02.909: I/dalvikvm-heap(24794): Clamp target GC heap from 65.790MB to 64.000MB
05-05 16:03:02.969: D/dalvikvm(24794): GC_EXPLICIT freed 71K, 5% free 59008K/61680K, paused 2ms+13ms, total 55ms
05-05 16:03:03.559: I/dalvikvm-heap(24794): Clamp target GC heap from 67.197MB to 64.000MB
05-05 16:03:03.569: D/dalvikvm(24794): GC_EXPLICIT freed 418K, 2% free 60521K/61680K, paused 3ms+28ms, total 109ms
的一些活動經過我得到這個崩潰:
05-05 16:03:05.049: E/AndroidRuntime(24794): java.lang.OutOfMemoryError
05-05 16:03:05.049: E/AndroidRuntime(24794): at android.graphics.Bitmap.nativeCreate(Native Method)
05-05 16:03:05.049: E/AndroidRuntime(24794): at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
05-05 16:03:05.049: E/AndroidRuntime(24794): at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
05-05 16:03:05.049: E/AndroidRuntime(24794): at android.graphics.Bitmap.createBitmap(Bitmap.java:718)
我不知道是怎麼回事錯了,我在做回收()位圖,並且還我tryed有和沒有的System.gc(),在這兩種情況下我得到了崩潰
您是否嚮應用程序添加了largeHeapSize標誌? –
mmmm謝謝deathember似乎使用這已經改進了我的應用程序,但我認爲這不是最終的解決方案 – NullPointerException
讓我來解釋一下。當你在你的記憶中使用大的位圖時,你會給一個OME。更多的位圖 - 更多機會獲得OUME。添加標誌不是一個壞的方式,因爲Android讓你獲得最大的堆大小來將你的對象加載到內存中。谷歌它爲羅馬人的機器人的內存管理。谷歌推薦你使用這個標誌。但你有另一種方式。看看你的代碼,也許你可以改進它,也許這可以修復你的OME。 –