Swift Academy · Lesson

Retain Cycles: Causes and Detection

Identifying retain cycles between objects and closures with Instruments.

Lesson 2 of 412 steps

Retain Cycles: Causes and Detection is a free Swift Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Swift Academy learning path, and your progress syncs across the web and the CoddyKit app. The Swift Academy course includes 4 lessons in total.

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! ```

Parent-Child Cycle

```swift class Node { var children: [Node] = [] var parent: Node? // strong → cycle with parent } let root = Node() let child = Node() root.children.append(child) child.parent = root // cycle ```

Closure Capture Cycle

```swift class ViewController { var onComplete: (() -> Void)? func setup() { onComplete = { // closure strongly captures self self.doWork() // ← cycle! } } } ```

Delegate Pattern Cycle

```swift protocol TaskDelegate: AnyObject { func didFinish() } class Task { var delegate: TaskDelegate? // if strong → potential cycle } class Manager: TaskDelegate { var task = Task() init() { task.delegate = self } // Manager → Task → Manager } ```

Detecting with Instruments

Use Xcode Instruments → Leaks: 1. Run app in Instruments 2. Click Leaks instrument 3. Red marks indicate leaked objects 4. Drill down to see reference graph Alternatively: set `deinit { print(...) }` and check if it fires.

Detecting with Xcode Memory Graph

In Xcode debugger: Debug > Memory Graph Debugger. Click on a leaked object to see its reference chain — the cycle will be visible as a closed loop in the graph.

Common Cycle Patterns

• Delegate storing delegate strongly • Closure capturing `self` strongly • Parent/child bidirectional strong references • NotificationCenter observer holding closure that captures self • Timer's target is `self` (strong)

Timer Retain Cycle

```swift class Poller { var timer: Timer? func start() { timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [weak self] _ in self?.poll() // weak self breaks cycle } } deinit { timer?.invalidate() } } ```

NotificationCenter Cycle

```swift class Listener { var token: NSObjectProtocol? init() { token = NotificationCenter.default.addObserver(forName: .UIApplicationDidBecomeActive, object: nil, queue: .main) { [weak self] _ in self?.refresh() } } deinit { NotificationCenter.default.removeObserver(token!) } } ```

Quick Check

What is a retain cycle in ARC?

Recap

Key takeaways: • Retain cycles: mutual strong references → neither deallocates • Common in: delegates, closures, parent-child bidirectional links • Detect with Instruments Leaks or Xcode Memory Graph Debugger • Fix with `weak` or `unowned` references • Timers and NotificationCenter also cause cycles — break with `[weak self]` Next: weak and unowned references.
Free to start

Learn Swift with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
122
Lessons
409

Frequently Asked Questions

Is the “Retain Cycles: Causes and Detection” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “Retain Cycles: Causes and Detection”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Swift Academy course includes 4 lessons in total.

What will I learn in “Retain Cycles: Causes and Detection”?

Identifying retain cycles between objects and closures with Instruments. You practise Swift Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Swift Academy?

No prior experience is required. Swift Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Retain Cycles: Causes and Detection” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Swift Academy lesson?

Yes. Every Swift Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. How ARC Counts References
  2. Retain Cycles: Causes and Detection
  3. weak and unowned References
  4. Closure Capture Lists and [weak self]
← Back to Swift Academy