Memory usage does not decrease even I recycle bitmaps

可紊 提交于 2020-01-01 03:37:08

问题


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

回答1:


From Android Developer

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 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.



来源:https://stackoverflow.com/questions/41740123/memory-usage-does-not-decrease-even-i-recycle-bitmaps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!