问题描述
我有A和B活动.从活动A开始活动B时,我在活动B上设置了静态位图变量.我在屏幕上显示该位图并旋转它.
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.
活动B完成后,我在onDestroy()方法上回收了所有位图,但内存使用并未减少.
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();
}
推荐答案
来自 Android开发人员
recycle只是确保每次调用GC时都将回收您的位图.System.gc也是如此,它无法确保gc立即运行,它只会要求gc运行,而GC仅在系统希望其运行时运行.
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.
这篇关于即使我回收位图,内存使用量也不会减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!