How does the Android Image Loading library optimize memory usage?

Authors
  • Amit Shekhar
    Name
    Amit Shekhar
    Published on
How does the Android Image Loading library optimize memory usage?

I am Amit Shekhar, Co-Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

Join Outcome School and get high paying tech job: Outcome School

Before we start, I would like to mention that, I have released a video playlist to help you crack the Android Interview: Check out Android Interview Questions and Answers.

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 Glide, Fresco, etc.

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.

Let's take 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 2000*2000, Glide down-samples the bitmap to 400*400, and then show it into the view.

We use 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 imageView.

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.

Prepare yourself for Android Interview: Android Interview Questions

That's it for now.

Thanks

Amit Shekhar
Co-Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.