Android cache> internal storage versus object cache

I need to cache images (total 5 or up to 100) from the Internet and display as a list. if the user selects a list line, the cache can be cleared. I looked at a few examples. some use external storage. some use internal and external. some objects ..

what are the advantages / disadvantages of internal storage ( http://developer.android.com/guide/topics/data/data-storage.html#filesInternal via getCacheDir ()) and object cache (something like WeakHashMap or HashMap <String, SoftReference <Drawable>)?

the problem is with softreferences, it looks like they can get gc'ed too fast ( SoftReference gets garbage collected too soon ). how about android internal storage? help sai "These files will be those that are deleted the first time the device is started in storage."

It doesn't matter if you use the object cache or temporary internal memory? except that the object cache should be a little faster

+6
source share
2 answers

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.

+10
source

Source: https://habr.com/ru/post/896445/


All Articles