Here are some differences between them:
- The object cache is faster than internal storage but has less capacity.
- The object cache is temporary and the internal storage has a longer life.
- The object cache takes the actual space on the heap. There is no internal storage. This is an important point, since an object cache that is too large can throw an OutOfMemoryException even with a SoftReference.
Now, given these differences, they are not completely mutually exclusive. We have implemented multilevel caching, especially related to image loading. Here are the steps we use:
- If the image has not been cached, extract from the URL and cache it in the first level cache, which is SoftReference / WeakHashMap or even a hard cache with a limited size using LinkedHashMap
- Then we implement removeEldestEntry () in LinkedHashMap. By pushing the capacity of the hard cache, we move things to the secondary cache, which is the internal storage. Using this method, you do not need to restore the image from URL +, it is still faster and it frees up your memory.
- We regularly cleaned up the internal storage using the LRU algorithm. You do not have to rely on Android to clean this for you.
We did multilevel caching of the common component and used it for many of our projects for our clients. This method is pretty much the same as the L1, L2 cache in computer architecture.
source share