liveBook · Manning

Weak reference

As explained in Fast by Default

A weak reference is an object reference that does not keep its referenced object alive.

A weak reference points to an object without keeping that object alive. It can break a retain cycle when two objects otherwise hold strong references to each other. section 7.3.3

Use weak references when closures, delegates, or notification listeners can accidentally retain their owning object. After the cycle is broken, memory can return to its earlier level after navigation. section 7.3.3

Breaking a navigation leak

Two objects strongly reference each other, so neither is released. Changing one reference to weak allows both to be released after navigation instead of leaving the screen in memory. section 7.3.3

How Fast by Default covers it

How deep it goes
Explains strong-reference cycles and their practical fix, but does not develop weak-reference implementation syntax.
Examples
Uses mutually referencing objects and a screen retained after navigation to demonstrate the leak and its repair.
What it assumes
Assumes familiarity with objects and references while establishing strong references and retain cycles as the necessary foundation.
What you can do after
Diagnose/debug with memory profiling by identifying retained objects and breaking a strong-reference cycle with a weak reference.
How it connects
Connects weak references to strong references, retain cycles, closures, delegates, notification listeners, and memory leaks.

Also explained in

Compare how 3 books explain weak reference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast by Default. The section links point to the parts of the book it draws on.