Recently, I try to figure out how WeakHashMap
works. As code shows, map.isEmpty()
will return true after System.gc()
and size
is strong referenced, so who change size?
WeakHashMap<UniqueKey, BigImage> map = new WeakHashMap<>();
UniqueKey key = new UniqueKey("abc");
map.put(key, new BigImage());
BigImage value = map.get(key);
assertNotNull(value);
key = null;
System.gc();
Thread.sleep(1000);
assertTrue(map.isEmpty());