Вопрос

I'm thinking about implementing the first layer of my caching in an Android app. I was considering SoftReferences to surely avoid OOM exceptions, but since there are many articles about how Android frees these up "too soon", I decided to look into android.util.LruCache cache.

Question: How do I size it up properly for the actual device? It all sounds very nice that an LRU cache is the real solution and not SoftReferences, but if you're really keen to avoid OOM Exceptions, it feel extremely unsafe to go with any number of megabytes of hard references. It's just unsafe if you ask me. Anyway, this seems to be the only option. I was looking into getMemoryClass to find out the heap size of the app on the actual device (+checking the free heap size before sizing the cache up). The base line is 16 Megs which sounds Ok, but I've seen devices (G1 for example in the old days) throwing OOM exceptions just around 5 Megabytes of heap size (according to Eclipse MAT). I know a G1 is very old, but the point is that my experiences don't really align with the 16 Megs baseline the documentation mentions. Therefore I'm completely uncertain how should I scale up an LRU cache if I need the most I can reasonably get. (would be happy with 8 Megs and would go with as small as 1 Meg on a low-spec device)

Thanks for any hints.

Edit: The Android LRU cache class I'm referring to: http://developer.android.com/reference/android/util/LruCache.html

Это было полезно?

Решение

I think a valid solution to calculate the LruCache size is outlined in the dev guide:

int memClass = ( ( ActivityManager )context.getSystemService( Context.ACTIVITY_SERVICE ) ).getMemoryClass();
int cacheSize = 1024 * 1024 * memClass / 8;

More information can be found here: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

Другие советы

From your question its a bit confusing to understand what you are asking. Let me give it a shot.

Various caching products AppFabric, memcached, ncache and scaleout have a 1M limitation on per object. I think scaleout does offer some kind of customization.

But all of these are server side products. So for a android device, which will most probably be a single host local cache only, I would probably go with a max of 64kb. I mean, why would anyone need more than 64kb per object on a device. Just my guess.

If I were you, I would study memcached (most famous open source caching solution). And may be scaleout since its easy to get a hello world working with scale out too. And proportionally decide.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top