爲了解決您的困境,我相信這是預期的行爲。
如果你想釋放的內存,你可以偶爾叫System.gc()
,但實際上,你應該在大多數情況下讓它管理垃圾收集本身。
我的建議是,你保持某種其通過計算每個位被佔用的字節數跟蹤它自己的內存使用的一個簡單的緩存(URL /文件名,位圖)。
/**
* Estimates size of Bitmap in bytes depending on dimensions and Bitmap.Config
* @param width
* @param height
* @param config
* @return
*/
public static long estimateBitmapBytes(int width, int height, Bitmap.Config config){
long pixels=width*height;
switch(config){
case ALPHA_8: // 1 byte per pixel
return pixels;
case ARGB_4444: // 2 bytes per pixel, but depreciated
return pixels*2;
case ARGB_8888: // 4 bytes per pixel
return pixels*4;
case RGB_565: // 2 bytes per pixel
return pixels*2;
default:
return pixels;
}
}
然後你查詢的應用程序是使用多少內存,多少是可用的,可能採取一半,並儘量保持下,總的圖像緩存大小,通過簡單地移除(解引用)舊圖像你的名單,當你是在這個極限來臨,不能回收。讓垃圾收集器清理位圖時,它們都從緩存中取消並且沒有被任何視圖使用。
/**
* Calculates and adjusts the cache size based on amount of memory available and average file size
* @return
*/
synchronized private int calculateCacheSize(){
if(this.cachedBitmaps.size()>0){
long maxMemory = this.getMaxMemory(); // Total max VM memory minus runtime memory
long maxAllocation = (long) (ImageCache.MEMORY_FRACTION*maxMemory);
long avgSize = this.bitmapCacheAllocated/this.cachedBitmaps.size();
this.bitmapCacheSize = (int) (maxAllocation/avgSize);
}
return this.bitmapCacheSize;
}
我會建議你使用recycle()
走就走,它會導致很多斷斷續續的例外(比如當看似敲定觀點嘗試訪問再生位圖),並在一般看來馬車。
你看過這個不錯的視頻嗎? http://www.youtube.com/watch?v=_CruQY55HOk我認爲你的代碼有內存泄漏 – HitOdessit
你必須告訴我們這是蜂窩前還是蜂窩 –