views:

234

answers:

1

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.

+4  A: 

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 Foos to be garbage-collected too; once you drop the reference to the Set<Foo>, the Foos 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!

Cowan
Thanks for the help and clarification. Now I can word my question in a better way: my concern is 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.
Tong Wang
Have edited + expanded the middle section of my answer to clarify this (see paragraph starting 'Note also')
Cowan
Thanks for the great answer! It cleared all my doubts.
Tong Wang