Johannes
Johannes

Reputation: 491

Android - Releasing memory of bitmaps

I think this question is asked often enough but after I did

bitmap.release;
bitmap = null;

in onDestroy of a Fragment, there is as much memory used as before.

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

Upvotes: 0

Views: 142

Answers (1)

Lorenzo Calisti
Lorenzo Calisti

Reputation: 91

Try using

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

For more info read a similar question

Upvotes: 1

Related Questions