2011-05-02 158 views
6

我正在使用相機應用程序。對於第一次,如果我捕捉圖像的工作正常,但如果我再次拍照它扔了錯誤如何清除堆?

ERROR/dalvikvm-heap(2398): 10077696-byte external allocation too large for this process." VM won't let us allocate 10077696 bytes" and finally"05-02 05:35:38.390: ERROR/AndroidRuntime(2398): FATAL EXCEPTION: main 05-02 05:35:38.390: ERROR/AndroidRuntime(2398): java.lang.OutOfMemoryError: bitmap size exceeds VM budget

和應用力closes..how來處理這個如何清除堆和VM? 請幫忙.. 在此先感謝..

回答

4

我找到了答案。
我用下面的代碼:

BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 

       bfOptions.inDither=false;      //Disable Dithering mode 

       bfOptions.inPurgeable=true;     //Tell to gc that whether it needs free memory, the Bitmap can be cleared 

       bfOptions.inInputShareable=true;    //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 

       bfOptions.inTempStorage=new byte[32 * 1024]; 
    CameraTricks.SdCardImage= BitmapFactory.decodeFile(CameraTricks.yu,bfOptions); 
CameraTricks.yu is my path to bitmap 
+0

我試過這個選項,這似乎爲API 2.3工作。但不在2.3以下的API中。這個問題似乎與這一行,'bfOptions.inTempStorage =新字節[32 * 1024]; VM不允許我們增加存儲容量。 – 2011-06-01 09:25:44

+0

還沒有工作2.2和2.1沒有任何問題 – Sando 2011-06-01 10:27:48

+0

什麼2.3和更高。 – RobinHood 2011-11-24 10:16:08

1

你不知道。你可以軟重置設備,但我懷疑這會有什麼好處。 Android的垃圾收集器應該照顧它。

很可能,您的應用程序在某些操作中使用的內存太多。您可以使用DDMS來檢查內存消耗(read about it here)。

可以在所有這些鏈接閱讀有關類似問題:

看起來像一個常見的主題是幾個大型圖像的加載。確保不保留對不再使用的圖像(或任何其他大對象)的引用,因此垃圾回收器可以恢復該內存。例如,使用Bitamp.recycle()

最後,請務必閱讀文章Avoiding Memory Leaks