即使我回收位图,内存使用量也不会减少 [英] Memory usage does not decrease even I recycle bitmaps

查看:130
本文介绍了即使我回收位图,内存使用量也不会减少的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

推荐答案

来自

释放与此位图关联的本机对象,并清除对像素数据的引用. 这不会同步释放像素数据;如果没有其他引用,它只是允许对其进行垃圾收集.该位图被标记为死",这意味着如果调用getPixels()或setPixels()它将抛出异常,并且不会绘制任何内容.此操作不能撤消,因此只有在您确定该位图没有其他用途的情况下,才应调用此操作.这是一个高级调用,通常不需要调用,因为当没有更多对此位图的引用时,正常的GC进程将释放此内存.

Free the native object associated with this bitmap, and clear the reference to the pixel data. This will not free the pixel data synchronously; it simply allows it to be garbage collected if there are no other references. The bitmap is marked as "dead", meaning it will throw an exception if getPixels() or setPixels() is called, and will draw nothing. This operation cannot be reversed, so it should only be called if you are sure there are no further uses for the bitmap. This is an advanced call, and normally need not be called, since the normal GC process will free up this memory when there are no more references to this bitmap.

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.

这篇关于即使我回收位图,内存使用量也不会减少的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆