Jeroen Frijters wrote:
Archie Cobbs wrote:
Dumb question.. why wouldn't it work to just use a
WeakHashMap instead of a HashMap in all those places?
Not a dumb question at all. That *would* work. The problem
is that (at least on some runtimes) WeakHashMap is much more
expensive than this approach.
Actually, it would not work in all cases. For serialization it would
work, because there the key is a Class instance, but for both Proxy and
ResourceBundle, the key is more complex, so it wouldn't work to directly
use a WeakHashMap (you'd have to add a layer of indirection, effectively
keeping a separate cache per class loader and in that case this approach
is arguably better).
There may be other reasons, Roman also didn't like to use a WeakHashMap,
but I didn't ask why.
Using a WeakHashMap would also make it a little harder to implement the
ResourceBundle LRU policy, but that was not the motivation for this
approach.
Regards,
Jeroen