You can't, because it doesn't make sense to do so. There's just no good way to ask if something is reachable if you can't reach it. If there were a function
isStronglyReachable(foo)
then how would you pass it an unreachable foo
? There's no way to pass it a weak reference to foo
, and if you pass foo
directly, then you know it's strongly reachable because you just used a strong reference to it (and binding it to a parameter creates another strong reference or two). You could try something of the form
weakmap.isValueStronglyReachable(key)
but then you'd need a strong reference to the key, which would make the value strongly reachable. Even if you tried something of the form
weakmap.getArrayOfUnreachableKeys()
(which would make the returned objects reachable through the returned array), the runtime would have to run a full GC pass to figure out which values are unreachable - and if it's doing a GC pass, why wouldn't it just delete the entries for unreachable keys?