Retain Cycles: Causes and Detection
Identifying retain cycles between objects and closures with Instruments.
Welcome
A retain cycle occurs when two or more objects hold strong references to each other, preventing ARC from ever reaching a zero count — causing a memory leak.
Simple Two-Object Cycle
```swift
class Owner { var pet: Pet? }
class Pet { var owner: Owner? }
var o: Owner? = Owner()
var p: Pet? = Pet()
o?.pet = p // Owner → Pet (strong)
p?.owner = o // Pet → Owner (strong)
o = nil; p = nil
// Neither deinit called — cycle!
```
All lessons in this course
- How ARC Counts References
- Retain Cycles: Causes and Detection
- weak and unowned References
- Closure Capture Lists and [weak self]