Tag Archive: LruCache


LruCache with Context

Per Google engineer’s comments last night, I submitted some new code to Android, for those that cannot wait the gits is:

https://gist.github.com/1770526

and the feature issue in Adnroid bugs is:

http://code.google.com/p/android/issues/detail?id=25287&thanks=25287&ts=1328715976

 

If you would like to see it added, obviously vote it up.

Advertisements

Error in LruCache

First up, we have somewhat an error of omission. Basically hasKey method is missing:


public synchronized boolean hasKey(K key) {
return map.containsKey(key);
}

Yeah we kind of need that little method.

Second things that is missing, when they re-factored LruCache I think they forgot the original reason why someone thought using weak-reference might be a good idea. We have two magical things to tell the GC what items not to do GC on right away, Soft Reference and Weak Reference.

Soft Reference is the stronger of the two and should be used when cache Drawables or anything else that has an activity context. David Wu coded a LruSoftcache with that in mind.  The key question now is do we really need ot keep a LruCache using Weak References around. The only use case I can think of is if you are downloading images for your application,large ones, and want the GC to operate on them sooner than a Soft Reference might allow. Also I can think of some game object pool cache use cases as well.

What is nice about the Android platform is that you can browse the git commits and get some historical idea of the re-factoring of a class which gives you clues on to what to track down and analyze. Its not like 1995 with java coming out where you had a black bock box and had to guess.