2011-05-02 74 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