2016-03-27 28 views
2

我使用下面的代码来减少位图大小并保存它的内存。但是当我使用它时,消耗的RAM内存不是释放。什么是解决方案?我在usting下面的方法后调用System.gc(),但不会影响。回收后未释放位图消耗RAM内存

public static void compress_save(Bitmap input, String path, int dim) throws IOException { 
    FileOutputStream out = new FileOutputStream(path); 
    input = Bitmap.createScaledBitmap(input, Generals.img_width, Generals.img_height, true); 
    input.compress(Bitmap.CompressFormat.JPEG, dim, out); 

    out.flush(); 
    out.close(); 
    input.recycle(); 
} 

我致电上述功能的活动成果

Uri selectedImageUri = data.getData(); 
       imgPath = Util.getRealPathFromURI(MainActivity.this, selectedImageUri); 
       Bitmap bitmap = BitmapFactory.decodeFile(imgPath); 
       try { 
        imgPath = Generals.App_Directory + "/temp/" + SystemClock.currentThreadTimeMillis(); 
        ImageUtil.compress_save(bitmap, imgPath, 50); 
        bitmap.recycle(); 
        System.gc(); 
        confirmDialog(); 
       } catch (IOException e) { 
        FileLogException.print(e); 
       } 
+0

你没有回收更大的位图('Bitmap input'参数) – pskink

+1

你有没有试过用'MAT'来检查谁仍然持有参考? – thepoosh

+0

再次使用MAT并查看谁拥有对位图的引用,调用System.gc实际上并不会导致gc – thepoosh

回答

0

我不知道我说的事情,但我认为,当你打电话的循环,如果你的设备有足够的RAM内存保持位图的RAM,当RAM空间不足时,位图RAM存储器将释放。

我在两个设备上测试了这些代码。当我的设备在回收位图之后有很多内存时,RAM空间不会释放,但是当设备在回收位图RAM内存释放之后具有低内存时。