Memory Management & ARC
Swift frees class instances automatically with ARC (Automatic Reference Counting). Learn how reference counting works, what a retain cycle is, and how weak , unowned , and [weak self] prevent memory leaks.
Learn Memory Management & ARC in our free Swift course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Swift course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ ARC — Counting References
Each strong reference to a class instance raises its count; each one removed lowers it. At zero, ARC frees the instance and runs deinit . Watch the count rise and fall below.
2️⃣ The Retain Cycle Problem
When two instances strongly reference each other, neither count can reach zero — so ARC never frees them and the memory leaks . The tell-tale sign is a deinit that never runs.
3️⃣ Breaking Cycles with weak
Make one side of the relationship weak . A weak reference doesn't raise the count and is automatically set to nil when its target is freed — so it must be an optional var . Now both objects can deallocate.
4️⃣ Closures and [weak self]
Closures capture self strongly by default. If an object stores such a closure, you get a cycle. A capture list [weak self] makes self a weak optional inside the closure — unwrap it with guard let self = self .
Your turn. Fill in the keyword that makes the parent reference non-retaining.
📋 Quick Reference
No blanks this time — just a brief and an outline. Build two classes that reference each other, then make one side weak so both deinitialize.
Practice quiz
What does ARC stand for in Swift?
- Automatic Recompilation Cycle
- Active Resource Control
- Automatic Reference Counting
- Allocated Region Cleanup
Answer: Automatic Reference Counting. ARC = Automatic Reference Counting, Swift's memory management for class instances.
ARC manages memory for which kind of types?
- Class instances (reference types)
- Structs and enums
- Only Ints
- Only optionals
Answer: Class instances (reference types). ARC tracks reference (class) types; value types like structs and enums are not reference-counted.
When does ARC deallocate a class instance?
- After a fixed timeout
- Only at app exit
- When you call free()
- When its strong reference count drops to zero
Answer: When its strong reference count drops to zero. ARC frees an instance the moment its strong reference count reaches zero.
What is a retain cycle (strong reference cycle)?
- A loop that retains an array
- Two instances holding strong references to each other so neither is ever freed
- A CPU caching strategy
- A type of optional
Answer: Two instances holding strong references to each other so neither is ever freed. When two objects strongly reference each other, their counts never reach zero, leaking memory.
Which keyword creates a reference that does NOT increase the reference count and becomes nil when the target is freed?
- weak
- strong
- lazy
- open
Answer: weak. A 'weak' reference doesn't retain its target and is automatically set to nil when the target is deallocated.
A 'weak' reference in Swift must be declared as what?
- A constant (let)
- A non-optional
- An optional var
- A computed property
Answer: An optional var. weak references must be optional vars because they can become nil at any time.
When is 'unowned' appropriate instead of 'weak'?
- When the reference may outlive the target
- When the referenced object is guaranteed to outlive the reference, so it never becomes nil
- Always, it is safer
- Only for value types
Answer: When the referenced object is guaranteed to outlive the reference, so it never becomes nil. Use unowned when the target is expected to always be alive during the reference's lifetime; it is non-optional and crashes if accessed after deallocation.
In a closure that captures self, how do you commonly avoid a retain cycle?
A capture list [weak self] (or [unowned self]) breaks the cycle between the object and the closure it stores.
After 'capture list [weak self]', what is the type of self inside the closure?
- Self (non-optional)
- Self? (optional) — you typically unwrap it
- Int
- Never
Answer: Self? (optional) — you typically unwrap it. [weak self] makes self optional inside the closure, so you usually write 'guard let self = self else { return }'.
Do structs and enums participate in ARC retain cycles?
- Yes, always
- Only if they contain Ints
- No — they are value types and are copied, not reference-counted
- Only inside classes
Answer: No — they are value types and are copied, not reference-counted. Value types are copied rather than referenced, so they don't create reference cycles on their own.