Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recycling bitmaps

I am working on project which involves operations on bitmaps. Because I don't want to have OutofMemoryError I'm trying to recycle each of unused bitmap. Unfortunatelly I've recycled too much and have 'trying to use a recycled bitmap' error.

I am using:

 Bitmap.createBitmap(bmp, x,y,w,h, matrix, false);
 Bitmap.createScaledBitmap(bmp, w, h,true);

Should I recycle bmp after this methods or it is recycled by them? Like:

Bitmap newBitmap = Bitmap.createBitmap(bmp, x,y,w,h, matrix, false);
bmp.recycle();

Can I just after imageView.setImageBitmap() recycle one which previously was used here? E.g.

myImageView.setImageBitmap(myBitmap);
myImageView.setImageBitmap(newBitmap);
myBitmap.recycle();

Thank you for help.

like image 935
ania Avatar asked Dec 27 '22 21:12

ania


2 Answers

You should only recycle a bitmap when you do not need it anymore. However, you do need a bitmap when you want to display it. If you don't want to display it, then you can recycle a bitmap.

like image 149
tiguchi Avatar answered Jan 08 '23 13:01

tiguchi


You only recycle bitmaps once you are done with them and are sure you never need to use the data in them again. It's not a magic method that you can use anywhere you like to give you more memory when dealing with bitmaps.

like image 45
kabuko Avatar answered Jan 08 '23 13:01

kabuko