D
D
Daniel2017-08-27 22:02:51
Java
Daniel, 2017-08-27 22:02:51

How to unload image from memory in ImageView on Android 5?

Hello.
I ran into such a problem that I can’t unload images loaded into ImageView on Android 5 from memory.
Before that, I tested it on Android 7 and in the form in which the application is now the memory is consumed slightly, but on Android 5 it just leaks through your fingers.
There is a method through which I unload a picture from memory, I wrote the following in it:

cardview_tumbnile_imageview.setImageResource(0);
cardview_tumbnile_imageview.setImageBitmap(null);
cardview_tumbnile_imageview.setImageDrawable(null);
System.gc();

I wrote all this for Android 5 and still no result.
For Android 7, it's easy enough to write
setImageDrawable(null)

I did Dump Memory Heap and saw a bunch of byte[] there after loading images.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
aol-nnov, 2017-08-27
@aol-nnov

not doing business, as it seems to me.

A
Alexander Varakosov, 2017-08-28
@thelongrunsmoke

First, gc is run as the hip is exhausted. And if a lot of pictures do not cause OutOfMemory, then it is better to leave this optimization for later.
Secondly, if your images are not prepared for different resolutions, then it makes sense to load them via Bitmap.createScaledBitmap(). This will reduce memory consumption.

A
Andrei K, 2017-08-28
@1110001111

Did Dump Memory Heap and saw a bunch of byte[] there after loading images

And what object holds the pictures?
Why not use a dedicated image loading library? Glide or Fresco for example. There is in-memory and on-disk caching, they take care of all the work with memory. As I didn't mock any OOM I couldn't catch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question