I have A and B activities. When I start activity B from activity A, I set static bitmap variable on activity B. I show that bitmap on the screen and rotate it.
When activity B is finished, I recycle all bitmaps on onDestroy() method but memory usage is not decreasing.
@Override
protected void onDestroy() {
super.onDestroy();
if (bitmap90 != null) {
bitmap90.recycle();
bitmap90 = null;
}
if (bitmap180 != null) {
bitmap180.recycle();
bitmap180 = null;
}
if (bitmap270 != null) {
bitmap270.recycle();
bitmap270 = null;
}
if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}
if (((BitmapDrawable) ivOriginal.getDrawable()).getBitmap() != null) {
((BitmapDrawable) ivOriginal.getDrawable()).getBitmap().recycle();
ivOriginal.setImageDrawable(null);
}
if (((BitmapDrawable) ivOriginal90.getDrawable()).getBitmap() != null) {
((BitmapDrawable) ivOriginal90.getDrawable()).getBitmap().recycle();
ivOriginal90.setImageDrawable(null);
}
System.gc();
}
From Android Developer
recycle just makes sure that your bitmap will be recycled whenever GC is called. Same goes for System.gc, it cannot make sure that gc will run right now, it will just ask the gc to run but GC will only run when system want's it to run.
So just relax, if you are recycling the bitmaps they will get recycled eventually just give it some time.