java weakreference cache
Answers
Answered by
0
48down votefavorite20Java's WeakHashMap is often cited as being useful for caching. It seems odd though that its weak references are defined in terms of the map's keys, not its values. I mean, it's the values I want to cache, and which I want to get garbage collected once no-one else besides the cache is strongly referencing them, no?In which way does it help to hold weak references to the keys? If you do a ExpensiveObject o = weakHashMap.get("some_key"), then I want the cache to hold on to 'o' until the caller doesn't hold the strong reference anymore, and I don't care at all about the string object "some_key".Am I missing something?
Similar questions
Computer Science,
8 months ago
Math,
8 months ago
Science,
8 months ago
Computer Science,
1 year ago
Science,
1 year ago
Math,
1 year ago
Hindi,
1 year ago