Swift Academy · Lesson

How ARC Counts References

What ARC does at compile time, retain/release calls and object lifetimes.

Lesson 1 of 412 steps

How ARC Counts References is a free Swift Academy lesson on CoddyKit. This is lesson 1 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

Automatic Reference Counting (ARC) manages class instance memory. Understanding how it counts references helps you write leak-free code.

What ARC Does

ARC tracks how many *strong* references point to each class instance. When the count reaches zero, it calls `deinit` and frees memory — automatically.

Strong References

```swift var a: Dog? = Dog(name: "Rex") // count = 1 var b = a // count = 2 a = nil // count = 1 b = nil // count = 0 → deinit ```

ARC is Compile-Time

ARC inserts retain/release calls at compile time — not at runtime like a garbage collector. This means: • No GC pauses • Deterministic deallocation • Slight code-size overhead from retain/release calls

Strong Reference Cycle Risk

```swift class Node { var next: Node? } var n1: Node? = Node() var n2: Node? = Node() n1?.next = n2 n2?.next = n1 // cycle! n1 = nil; n2 = nil // both stuck at count=1 — leaked! ```

Counting with Multiple Variables

```swift class Engine {} var car1: Engine? = Engine() // count = 1 var car2 = car1 // count = 2 var car3 = car2 // count = 3 car1 = nil // count = 2 car2 = nil // count = 1 // Engine still alive — car3 holds it car3 = nil // count = 0 → deinit ```

ARC in Collections

```swift var dogs: [Dog] = [] dogs.append(Dog(name:"A")) // count = 1 dogs.append(Dog(name:"B")) // count = 1 each dogs.removeAll() // both deinit called ```

ARC Does NOT Apply to Value Types

Structs, enums, and tuples are NOT reference-counted: ```swift struct Point { var x, y: Double } // no retain/release var p1 = Point(x:1,y:2) var p2 = p1 // copy — no ARC ```

Observing ARC with deinit

```swift class Cat { let name: String init(name: String) { self.name = name; print("\(name) born") } deinit { print("\(name) deallocated") } } var c: Cat? = Cat(name: "Luna") c = nil // "Luna deallocated" printed ```

ARC vs GC Performance

• GC: pauses execution periodically to collect; throughput/latency tradeoff • ARC: deallocates immediately when count reaches 0; no pauses • ARC overhead: every assignment through a strong reference costs a retain/release pair

Quick Check

When does ARC call `deinit` and free an object's memory?

Recap

Key takeaways: • ARC tracks strong references to class instances • Count 0 → `deinit` called → memory freed • ARC is compile-time — no GC pauses • Value types (struct/enum) are not ARC-managed • Cycles prevent count reaching 0 → use weak/unowned Next: retain cycles.
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 “How ARC Counts References” lesson free?

Yes — any 3 lessons of the Swift Academy learning path, including “How ARC Counts References”, 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 “How ARC Counts References”?

What ARC does at compile time, retain/release calls and object lifetimes. 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 1 of 4.

How long does the “How ARC Counts References” 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