6

i need to cache images (only 5 or up to 100) from the web and displayed in a listview. if the user selects a row of the listview the cache can be cleared. i had a look on some examples. some use external storage. some use internal and external. some objects..

so 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 > )?

a problem with softreferences seems to be that they may get gc'ed too fast ( SoftReference gets garbage collected too early) . what about the android internal storage? the reference sais "These files will be ones that get deleted first when the device runs low on storage.".

does it make any difference to use a object cache or the temporary internal storage? except for the object cache should be a bit faster

Community
  • 1
  • 1
207
  • 3,784
  • 2
  • 25
  • 22

2 Answers2

10

Here are the few differences between the two:

  • Object cache is faster than internal storage, but has lower capacity.
  • Object cache is transient in nature while internal storage has longer life span
  • Object cache takes the actual space in the heap. Internal storage doesn't. This is an important point, as making your object cache too large could cause the OutOfMemoryException even with SoftReference

Now given those differences, they are not totally mutually exclusive. A lot of we implemented is using multi layer caching especially related to the image loading. Here are the steps that we use:

  • If the image hasn't been cached, fetch from the URL and cache it in first level cache which is the SoftReference/WeakHashMap or even hard cache with limited size using LinkedHashMap
  • We then implement removeEldestEntry() in LinkedHashMap. Upon hitting the hard cache capacity, we move things to secondary cache which is the internal storage. Using this method, you don't have to refetch the image from the URL + it's still be faster and it frees up your memory
  • We ran a cleanup on timely basis on the background for the internal storage using LRU algorithm. You shouldn't rely on Android to clean this up for you.

We have made the multi layers caching a common component and have used it many of our projects for our clients. This technique is pretty much following to what L1, L2 cache in Computer Architecture.

momo
  • 21,233
  • 8
  • 39
  • 38
  • +1 they certainly aren't mutually exclusive! You'll need both, and this is a good approach. – David Snabel-Caunt Sep 01 '11 at 20:22
  • 2
    Good points. For the object cache you can solve the SoftReference problem as well as not having to worry as much about implementation by using `LruCache` found here: http://developer.android.com/reference/android/util/LruCache.html and also in the compatibility library. – kabuko Sep 01 '11 at 20:34
  • just another question. how do you determine the size/limit (10 or 50 or 100 objects) of the linked list? should depend on the size of the objects to store i suppose – 207 Sep 01 '11 at 20:42
  • @207 Yes, that's correct. I made that as a parameter to the common component so that I could customize that per application that we develop. – momo Sep 01 '11 at 20:44
  • and how do you determine the limit? for example..lets say..in worst case i'd like to cache up to 1000 bitmaps. each bitmap about 50kb. what is the approach to determine the limit of the first level cache (e.g. a linkedhashmap) – 207 Sep 01 '11 at 21:40
  • Most of that is an educated guess. We normally do the analysis by running dumpsys meminfo while watching the app memory consumption with few different numbers based on what we have encountered in the past. The data then serve as our basis for deciding which number we think would operate the best. We also found that setting the first level cache to lower number and have them stored in internal storage doesn't hinder the performance that much in many of our test phones. Depending on your app, being conservative and setting the first level cache to lower number might still work. – momo Sep 01 '11 at 21:50
0

You should look for the question "How do I lazy download images in ListView" and related.

Community
  • 1
  • 1
  • this is where i got the examples from. some use internal..some external..some objects... its about lazy loading not about storing – 207 Sep 01 '11 at 20:13