I need to define a weak reference Map, whose value is a Set. I use Google collections’ MapMaker, like this:
Map<Class<? extends Object>, Set<Foo>> map = new MapMaker().weakKeys().weakValues().makeMap();
So, for Set<Foo>, can I use a normal HashSet? Or, do I have to create a weak HashSet, like this:
Collections.newSetFromMap(new WeakHashMap<Foo, Boolean>());
And why?
Another question, the key of my map is Class objects, when will a Class object become weakly reachable? In other words, what is the lifetime of a Class object?
Thanks.
Update: The 1st question is not really clear. My concern was that when I do map.get(key).add(foo) to add foo instance to the Set, will this also add a strong reference to foo instance and thus preventing it from being GCed? That’s why I was wondering if I should use a weak HashSet.
To answer the second part first, a
Classobject is only eligible for garbage collection when the responsibleClassloaderis in turn garbage collectied. See Section 12.7 of the JLS:So a
WeakReference<Class>is probably not likely to do what you’re hoping for (though it’s not 100% clear to me what you are looking for).Now, back to the first part — do you need a weak HashSet? That depends, again, on what you’re trying to do. If you want the
Mapentries to be reclaimed when the valueSetitself is no longer referenced, no, you don’t need a weakHashMap. (google-collections uses equality reference for weakly-referred values and keys, by the way, so there are no tricky equality issues here). Next GC after the last reference to theSetis discarded, theMapentry will be reclaimed.Note also that this will allow the
Foos to be garbage-collected too; once you drop the reference to theSet<Foo>, theFoos are only weakly reachable and can be discarded. See the Javadoc for java.lang.ref package:So if the only chain of reference is (Strong reference to
Map) → (Mapholds weak reference toSet) → (Setholds strong reference toFoo) then theFoocan be garbage-collected.However, I have a nagging suspicion this isn’t what you’re after. I suspect what you want is for the Map entry to be reclaimed when the last
Foois no longer referenced; that you’re not holding a reference to theSet<Foo>itself, but rather the individualFooobjects.If that’s the case, then no, this won’t do what you want. What you really need is a weakly-valued
Multimap— something like aMultimapMaker.weakValues(). However, there is noMultimapMakerat present: see guava-libraries issue #142 for a request to add this. Sorry!