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.

Advertisements