- Amit Shekhar
- Published on
I am Amit Shekhar, a mentor helping developers in getting high-paying tech jobs.
In this blog, we are going to learn how the Android Image Loading library optimizes memory usage.
This blog is a part of the series - "How does the Android Image Loading library work internally?"
- How does the Android Image Loading library use the bitmap pool for responsive UI? - READ FROM HERE
- How does the Android Image Loading library solve the slow loading issue? - READ FROM HERE
- How does the Android Image Loading library optimize memory usage? - YOU ARE HERE
The out-of-memory issue is the biggest nightmare that is attached to the life of all Android developers. As Android developers, we must have faced OutOfMemoryError(OOM Error). How do these libraries solve this OOM Error?
These concepts are important when it comes to Android Interviews.
So, I was reading the source code of various Image Loading libraries in Android like
These libraries do a lot of things internally for us so that we can just use them and get our tasks done easily. They make our life easier.
One of the things that they do is that they optimize memory usage. It means that they try to use as much less memory as they can use.
Let's learn how the Android Image Loading library optimizes memory usage.
Glide as an example. To optimize memory usage and use less memory,
Glide does downsampling.
Downsampling means scaling the bitmap(image) to a smaller size which is actually required by the view.
Assume that we have an image of size
2000*2000, but the view size is
400*400. So why load an image of
Glide down-samples the bitmap to
400*400, and then show it into the view.
Glide like this:
Glide.with(fragment) .load(url) .into(imageView);
As we are passing the
imageView as a parameter to the
Glide, it knows the dimension of the
Glide down-samples the image without loading the whole image into the memory.
This way, the bitmap takes less memory, and the out-of-memory error is solved. Similarly, other Image Loading libraries like
Fresco also do it.
This was all about how the Android Image Loading library optimizes memory usage.
We will learn more things that these libraries do in our upcoming blog post.
Master Kotlin Coroutines from here: Mastering Kotlin Coroutines
That's it for now.
You can connect with me on: