To answer the second part first, a Class
object is only eligible for garbage collection when the responsible Classloader
is in turn garbage collectied. See Section 12.7 of the JLS:
A class or interface may be unloaded if and only if its defining class loader may be reclaimed by the garbage collector as discussed in §12.6. Classes and interfaces loaded by the bootstrap loader may not be unloaded.
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 Map
entries to be reclaimed when the value Set
itself is no longer referenced, no, you don't need a weak HashMap
. (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 the Set
is discarded, the Map
entry will be reclaimed.
Note also that this will allow the Foo
s to be garbage-collected too; once you drop the reference to the Set<Foo>
, the Foo
s are only weakly reachable and can be discarded. See the Javadoc for java.lang.ref package:
An object is strongly reachable if it can be reached by some thread without traversing any reference objects. [...] An object is weakly reachable if it is neither strongly nor softly reachable but can be reached by traversing a weak reference. When the weak references to a weakly-reachable object are cleared, the object becomes eligible for finalization.
So if the only chain of reference is (Strong reference to Map
) → (Map
holds weak reference to Set
) → (Set
holds strong reference to Foo
) then the Foo
can 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 Foo
is no longer referenced; that you're not holding a reference to the Set<Foo>
itself, but rather the individual Foo
objects.
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 a MultimapMaker.weakValues()
. However, there is no MultimapMaker
at present: see guava-libraries issue #142 for a request to add this. Sorry!