liveBook · Manning

Reference cycle

As explained in Fast by Default

A retain cycle is a loop of strong object references that prevents the referenced objects from being released.

A reference cycle is a loop of strong object references that keeps every object in the loop alive, even after the application no longer needs it. On iOS, cycles commonly arise when closures capture their owner, delegates retain each other, or notification listeners remain registered. section 7.3.3

Detect and break cycles systematically:

  • Repeat an action such as navigating away from and back to a screen; growing object counts or memory indicate unreleased data. section 7.3.3
  • Use Allocations to identify growth and Leaks to inspect the object types, sizes, and reference paths sustaining them. section 7.3.3
  • Change one strong reference to weak when two objects retain each other, then verify that memory returns after navigation. section 7.3.3

Account overview navigation

Returning to an account overview adds 8MB because transaction data and decoded images remain retained after the screen disappears. A leak diagram reveals the cycle; weakening one reference lets memory recover. section 7.3.3

How Fast by Default covers it

How deep it goes
Explains strong-reference loops, traces them with leak diagrams, and demonstrates breaking one link with a weak reference.
Examples
Uses account-overview navigation, retained transaction data, decoded images, Allocations, and Leaks diagrams.
What it assumes
Builds from the idea that strong references keep objects alive and assumes familiarity with application objects and navigation.
What you can do after
Diagnose leaks with Allocations and Leaks, identify retaining references, and break cycles by weakening one reference.
How it connects
Connects reference cycles with memory profiling, closures, delegates, notification listeners, strong references, and weak references.

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.