Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android cache > internal storage vs. object cache

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

like image 820
207 Avatar asked Sep 01 '11 19:09

207


People also ask

What's the difference between cache and storage?

Cache is distinguished by being an ephemeral copy of a relatively small amount of currently in-use data for rapid access or to buffer writes. Storage, by contrast, sits behind/below cache and provides the bulk, long term retention location for data and is addressed in blocks rather than bytes.

What is internal storage in Android?

Android Internal storage is the storage of the private data on the device memory. By default, saving and loading files to the internal storage are private to the application and other applications will not have access to these files.

What is internal storage and external storage in Android?

In short, Internal Storage is for apps to save sensitive data to which other apps and users cannot access. However, Primary External Storage is part of built-in storage which can be accessed (for read-write) by the user and other apps but with permissions.


1 Answers

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.

like image 197
momo Avatar answered Oct 14 '22 07:10

momo