Android does not free Bitmap from memory

I performed the following steps to process my bitmaps in the application:

  • LruCache for 1/8 memory Bitmaps
  • Using BitmapFactory.Options to calculate inSampleSize
  • Capturing OOM when creating Bitmaps , calling evictAll and System.gc() there
  • Also there is AsyncTask decoding sometimes for Bitmaps

I am using BitmapFactory.decodeFile , and it looks like the VM is not freeing Bitmaps fast enough from memory. I read somewhere that there might be an error using BitmapFactory.decodeFile , so I tried using BitmapFactory.decodeFileDescriptor , but there I accidentally get the following:

skia --- decoder-> decode returned false

So, there is something wrong with the FileInputStream that needs to be fixed here if I don't use BitmapFactory.decodeFileDescriptor or something else to do.

It took me too much time, and I read all the solutions based on this and how Google advises to handle Bitmap , and I came to a standstill.

Thanks.

+4
source share
4 answers

I ended up using SoftRefences with Bitmap . Now I see that the GC frees my unused Bitmaps all the time when the GridView scrolls quickly, which draws them.

Tested by setting my size size to LruCache size and still not getting OOM.

The penalty using this method is not so noticeable, my GridView scrolls quite smoothly, given that it draws a very individual image.

+2
source

Using a large bitmap always has a chance to get an Out Of Memory Exception .. So, to handle the browser through Android

http://developer.android.com/training/displaying-bitmaps/index.html

And always recycle Bimap

 ImageView mImage; Drawable toRecycle = mImage.getDrawable(); if ( toRecycle != null && toRecycle instanceof BitmapDrawable ) { if ( ( (BitmapDrawable) mImage.getDrawable() ).getBitmap() != null ) ( (BitmapDrawable) mImage.getDrawable() ).getBitmap().recycle(); } 
+2
source

Please view this link.

https://github.com/nostra13/Android-Universal-Image-Loader

I think this may help.

+2
source

System.gc () does not help you and does not guarantee anything.

If you are absolutely sure that the output raster images are no longer needed and there is no link to them anywhere (the exception β€œUnable to draw the processed raster image” will catch the exception), I would suggest you add EvictionListener to your LRU cache and call bitmap.recycle () for each the displayed value.

It is impossible to remember if, by default, the LRU cache provides convenient methods for installing an eviction listener, but if not, it is very simple to expand it and add the necessary functions.

PS I would advise WeakReferences, as you lose control of your bitmaps and the LRU target. Even if you download 8 bitmaps that fit well in 1/8 of the memory, but only 4 of them can be displayed on the screen at a time (ImageViews contains strong links to bitmaps) gc will clear the remaining 4 as soon as possible. And I really mean ultra fast. You will have to reload the bitmap for each new row (in the case of ListView) that you display. And every bitmap that appears on the screen should be reloaded again.

0
source

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


All Articles