11.2 Weak References and Cleaners
References to objects around us in everyday life are called strong references because automatic garbage collection would never release a used object. However, in addition to strong references, with weak references , the garbage collector will remove the objects. What sounds crazy at first becomes interesting when implementing caching data structures: If an object is in the cache, that’s fine and access is fast; if the object is not in the cache, that’s fine too, but access takes a little longer. Thus, you can use weak references to create cached objects that automatic garbage collection is allowed to clear up when memory is running low.
Thus, weak references interact with automatic garbage collection ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access