abhishek maharajpet
abhishek maharajpet

Reputation: 626

Caching local vs caching in cache directory

Let's assume that we want to cache a set of bitmaps or any data. (I don't need the data once the app is killed) I can do it in two ways

Which is preferred and why? Also from the memory perspective which has more chance of out of memory exception?

Upvotes: 0

Views: 352

Answers (1)

Perraco
Perraco

Reputation: 17380

There are a few factors to be considered in order to follow the best approach, as both are valid and there is no preferred one, since is actually matter of efficiency and best approach based on specific scenarios. Some of the factors to consider are, how many bitmaps need to be cached, the size of the bitmaps, the devices targets (low, mid, high specs), etc.

That said, if for example, we are talking about a large set of bitmaps (big or small), then the best approach is always to use storage persistence. For that there are ready-made popular solutions used by most developers (even google or facebook), such as glide, fresco, and picasso. These libraries perform all the caching for you, handling even image scaling.

If we are talking just about only a few bitmaps, then there is no point to cache them to file, and you may hold them just in memory.

As a side note, if opting for caching into memory, then another point into consideration is how large are the bitmaps, as even only few large bitmaps can potentially raise an OutOfMemoryError. Follow this link for several tips about efficient bitmap loading.

If you are still unsure, then you may do the math. Compute the size of a single bitmap, so this will be as width * height * 4 (four bytes for argb), then multiply the result by the number of bitmaps that you need to manage. This will give you the amount of memory your bitmaps occupy. Consider that you still need memory for your app's runtime execution. And keep in mind there is a maximum memory space threshold given by the operation system to your app, as apps can't use all device available memory. You can increase the allowed memory usage by setting the largeHeap manifest flag.

Remarks answering your comments:

When talking about memory is always referred as the actual device RAM and not a cache file directory (storage). Caching into a DB or a file directory is unrelated to an OutOfMemoryError. Caching into a directory is used when there are lots of files, but at some point you still need to load some of the bitmaps into memory (RAM) to be able to use them, and at this point you need to think about memory management to avoid out of memory errors.

In addition, consider that loading a bitmap from file can be an expensive operation. This solved by libraries such as the ones mentioned above which perform the loading in a background thread.

For out of memory errors, you need to consider other factors, such as how many bitmaps you need to have ready in RAM for fast access, calculate all the bitmaps sizes, manage the bitmap deallocation (see the Bitmap.recycle method in Android's documentation), etc.

To summarize, when using lots of bitmaps, the approach is always to use storage (file) caching, such as by using the libraries shown above (glide, fresco, picasso, etc) which will manage for you everything, file loading and memory (RAM). If you need only a few bitmaps, then the best is to cache them directly into RAM and manage yourself the memory by releasing each bitmap dynamically when no longer used.

If instead of bitmaps, you need to manage a very large set of lets say for example strings, then the best approach would be to use a SQLite database, and access it through a cursor. For a small set then use a memory (RAM) cache.

Upvotes: 2

Related Questions