Memory usage is not reduced, even I recycle bitmaps

I have actions A and B. When I start activity B from activity A, I set the static bitmap variable to activity B. I show this bitmap on the screen and rotate it.

When action B is complete, I recycle all the bitmaps using the onDestroy () method, but memory usage is not reduced.

@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(); } 
+5
source share
1 answer

From Android Developer

Free your own object associated with this bitmap and clear the link to the pixel data. This does not release pixel data synchronously; it just allows garbage collection if there are no other links . The bitmap is marked as "dead", which means that it throws an exception if getPixels () or setPixels () is called and does not draw anything. This operation cannot be undone, so it should be called only if you are sure that the bitmap is no longer used. This is an extended call, and generally does not need to be called, since the normal GC process will free this memory if there are no more references to this bitmap.

recycling only ensures that your bitmap will be recycled whenever the GC is called. The same goes for System.gc, it cannot guarantee that gc will start right now, it will just ask gc to start, but GC will only work when the system wants it to start.

So, just relax, if you are processing bitmap images, they will be processed, in the end, just give it some time.

+2
source

Source: https://habr.com/ru/post/1263048/


All Articles