Glide — How to Choose the Best Caching Preference

In a previous tutorial we've shown you the basics of Glide caches. In this tutorial we'll explain in more detail what the disk cache settings entail and how it can effect your loading times.

Glide Series Overview

Background of Disk Cache Settings

As we've discussed in the previous tutorial, there are four options for the enum parameters of the .diskCacheStrategy() method:

  • Glide 3.x & 4.x: DiskCacheStrategy.NONE caches nothing
  • Glide 4.x: DiskCacheStrategy.DATA, Glide 3.x: DiskCacheStrategy.SOURCE caches only the original full-resolution image
  • Glide 4.x: DiskCacheStrategy.RESOURCE Glide 3.x: DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) (default behavior of Glide 3.x)
  • Glide 4.x only: DiskCacheStrategy.AUTOMATIC intelligently chooses a cache strategy based on the resource (default behavior of Glide 4.x)
  • Glide 3.x & 4.x: DiskCacheStrategy.ALL caches all versions of the image

What we didn't discuss are the implications of the settings in various use cases! It's time to dive into it.

If you're using DiskCacheStrategy.NONE Glide will load the image without putting neither the original image nor the optimized image in cache.

If you utilize DiskCacheStrategy.DATA (Glide 3.x: .SOURCE), Glide will write the original image to disk, then decode and transform it to display in the ImageView.

If you use DiskCacheStrategy.RESOURCE (Glide 3.x: .RESULT), Glide will decode, transform and encode the image and write the transformed version to the disk cache. Unfortunately, the encode process for the transformed version can take quite a while in some situations. While encoding for images is pretty fast, the encoding can take a long time for Gifs (because it has to decode and encode every frame). This will result in a noticeable delay of displaying the Gif! Even if you're re-using a Gif multiple times it's faster to use DATA to avoid the encode process.

Tradeoffs & Recommendations

Overall, you've to consider between the following tradeoffs:

  • How large is the loaded image and in what resolution will it be displayed? If you're loading a very large image, but only display it in a small resolution, RESOURCE will save you disk space and disk writing & read time.
  • How often do you show an image once it's in the cache? If you re-use an image very often, RESOURCE will give you a small win every time you display it.
  • If you display the same image multiple times in different resolutions, RESOURCE will probably load the image multiple times from the network. DATA or ALL will avoid this.
  • How many images are in the disk cache and how large is the cache? RESOURCE writes smaller images (because of the resizing) to the disk cache, thus saving you disk space.
  • If you load images or Gifs from the local disk, you don't need disk caching and your fastest option is AUTOMATIC or NONE.

If you're currently optimizing displaying images in your app we recommend to specifically measure loading times and cache utilization for the usage scenarios. There is no "best" setting which is correct for all use cases. Generally the AUTOMATIC option is a good approximation to an ideal configuration.

Having that said, in most cases the RESOURCE option usually works well for photos loaded from the Internet.

On the other hand, if you load Gifs you're better of using DATA to avoid the costly and slow encode.

If none of the options help you to speed up the process you should check how large the images are you're trying to display. Gifs and even images can be larger than expected. Especially on mobile not everyone has a lightning fast Internet connection. The time it takes to download an image can be a bottleneck as well.

Outlook

In this tutorial we've given you some background information on the implications of using the different Glide cache settings. If you're experiencing slow loading times, make sure to evaluate your disk cache settings. It's an easy way to have a significant influence on your app smoothness.

We appreciate feedback and love to help you if there’s a question in your mind. Let us know in the comments or on twitter @futurestud_io.

Make it rock & enjoy coding!


Additional Resources

Explore the Library

Find interesting tutorials and solutions for your problems.