首页 > 解决方案 > Android - 释放位图的内存

问题描述

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

bitmap.release;
bitmap = null;

在片段的onDestroy中,使用的内存和以前一样多。

片段:

@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();
}

标签: javaandroidmemorymemory-management

解决方案


尝试使用

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

有关更多信息,请阅读类似的问题


推荐阅读