java - Android - 释放位图内存

标签 java android memory memory-management

我认为这个问题经常被问到,但在我这样做之后

bitmap.release;
bitmap = null;

在 Fragment 的 onDestroy 中,使用的内存与以前一样多。

fragment :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    initializeViews();
    croppedBitmap = BitmapFactory.decodeFile("/.../0001.jpeg");
    imageView.setImageBitmap(croppedBitmap);
    fabCreate.setOnClickListener(...); //never called
}

@Override
public void onDestroy() {
    clearMemory();
    super.onDestroy();

}
void clearMemory(){

    fabCreate.setOnClickListener(null);
    imageView.setImageBitmap(null);
    imageView = null;
    croppedBitmap.recycle();
    croppedBitmap = null;
    java.lang.System.gc();
}

最佳答案

尝试使用

if(bitmap != null){
   bitmap.recycle();
   bitmap = null;
}

有关更多信息,请阅读similar question

关于java - Android - 释放位图内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51826681/

相关文章:

java - 如何在按键时重置处理 Canvas ?

java - 如何解决Android中的静态 fragment 需求?

c - 找出某个值存储在内存的哪一部分?

c++ - 分配约10GB的 vector -如何加快速度?

JavaFx Webview HTML5 拖放

java - 如何刷新选项卡框项目点击上的数据?

java - 参数化 h :commandButton

android - android 中 mediastore 中 MICRO_KIND 和 MINI_KIND 的区别?

android - 未设置文件名时读取 SharedPreferences

php - fatal error : Allowed memory size of 134217728 bytes exhausted (tried to allocate 3 bytes) after ini_set