This is what I sum up here to prove you really understand reference?
Q: what is reference and referent?
Q: what are the gc roots?
Q: during gc mark and sweep phases, how collector handle different kinds of reference?
Q: explain the interaction between gc and reference ?
Q: how apps get involved in gc lifecycle? notification?
Q: what is relation between finalizer and gc? how can finalizer pause gc process, unintentionally?
Q: when we deal with weakreference, we need to check null from get method, why? then holds strong reference to the value?
Q: what is ReferenceHandler? why handler.setPriority(Thread.MAX_PRIORITY); is this the consumer of referencequeue?
Q: then, who is the product of referencequeue? how productor and consumer sync?
Q: after you poll reference out of referencequeue, what you should do?
Q: how to reconstruct object during gc, equalskey?
Q: why weakhashmap entry overrides equals and hashcode?
Q: what is purpose of IdentityHashMap?
Q: how to avoid reconstruct value?
Q: why phantomReference get always return null?
Q: why not use weakreference to cache instead of softreference?
Q:when weakhashmap null its key? does it automatically null its value and entrys?
Q: how to trigger weakhashmap null stale entries? expungeStaleEntries()?
Q: detail the relationship between key, value, entry, reference in weakhashmap?
Q: explain the following method in WeakHashMap, why we don't need expunge entries? doesn't entry still hold strong reference to value?
"cut stale values from entry link", every objects referenced only by entry will be collected! so, values will be collected.
public void clear() {
// clear out ref queue. We don't need to expunge entries
// since table is getting cleared.
while (queue.poll() != null)
;
modCount++;
Arrays.fill(table, null);
size = 0;
// Allocation of array may have caused GC, which may have caused
// additional entries to go stale. Removing these entries from the
// reference queue will make them eligible for reclamation.
while (queue.poll() != null)
;
}
Q: tell me the typical poll pattern? code snippets?
always trace objects from gc roots. :)
source code links: