Search for killer memory caching mechanism for Android

Background

Android has a very limited maximum heap size, each device has a different maximum heap.

Some applications require the ability to cache things (usually images) in memory, and not just on internal / external storage.

Of course, there are many useful tips regarding processing bitmaps and using as little memory as possible, but caching is also a must.

Problem

I have read many possible caching solutions, but none of them offer a kind of caching that would be a caching solution. I would like to have a caching mechanism that has the following functions:

  • Unlimited heap usage without worrying about running out of memory. The application needs memory, and there is not enough free memory. therefore, release some (unbound) elements (and their keys).

  • Thread safety / concurrency.

  • Offer LRU-based caching so that items that were recently used have a higher chance of staying.

  • Stay alive as much as possible (but not cause glitches). However, unfortunately, on Android, GC-ed soft / weak links very quickly compare with Java.

  • The ability to process objects that hide their real size. On Android, on API 10 and lower, bitmaps did not use heap memory, but were considered as such, so the VM could not know when to free them, since it was believed that they should use the same amount of memory as one link (4 bytes or so Togo). That's why some solutions suggest artificially telling what the size of each element is, and what to do when you need to remove it.

Some good possible solutions.

  • LruCache is a class from API 12 (you can easily copy its code).

    Advantages: # 2 (?), # 3, # 5.

    Disadvantages: # 1, # 4, plus you need to copy the source code since it was introduced on API 12.

  • A hashmap with soft / weak reference for its meanings , as shown here on page 50, taken from this lecture .

    Advantages: # 1 (but does not delete keys), # 2 (you need to use ConcurrentHashMap )

    Minuses: # 3, # 4, # 5

  • MapMaker ( guava library available ), which is similar to an extended version of the previous solution.

    Advantages: # 1, # 2

    Minuses: # 3, # 4, # 5

  • caching solutions through the guava library. Advantages and disadvantages are based on your choice. Not sure which configurations fit the needs in the best way, and if it works great on Android. Unfortunately, I can't even compile the library for Android.

  • Android query - don't know how this works. It seems very easy to use, but not sure of its advantages and disadvantages.

Question

Does anyone know a killer caching mechanism?

I no longer like feature # 5, as it is quite advanced and will not be so necessary in the future, as more and more people have newer versions of Android.

+8
android caching out-of-memory bitmap heap-memory
source share
3 answers

As I can see, there is a practical problem with No. 1. You cannot release objects referenced by other parts of the application; therefore, it is impossible to create a construct that frees memory at will.

The only solution that I see is to create my own cache that supports LRU and is able to handle both weak and strong links. An element begins as a strong link, and if you do not use any time or memory limits, you change it to a weak link. This is not easy to create, and fine tuning must be done for sure in all applications.

+2
source share

You may be looking for a more general solution, but if you are mainly focused on images, look at ImageLoader , I have been using it for a while and it works great for what I need. At initialization, you can tell it to use LRUCache and say what% of free memory to use.

As a side note, HttpResponseCache makes caching data from the server very, very enjoyable.

0
source share

Take a look at Android BitMap Cache, which is a much improved version of LruCache.

http://www.senab.co.uk/2013/01/24/android-bitmapcache-v2-1/

0
source share

All Articles