How to answer
ARC (Automatic Reference Counting) manages the lifetime of class instances: the compiler inserts retain and release calls, and when an object's strong reference count reaches zero its deinit runs and its memory is freed. A strong reference (the default; there is no strong keyword in Swift) keeps the object alive. A weak reference doesn't, and is automatically set to nil when the object goes away, so it must be optional. An unowned reference doesn't keep the object alive either, but it's non-optional and assumes the object outlives it; accessing it after deallocation traps. ARC is deterministic and has no garbage-collector pauses, but it can't break cycles on its own: two objects holding each other strongly never reach zero.
Key points
- Strong: default;
var ref: SomeClassincrements the count weak var(orweak let, Swift 6.2): optional, zeroed on deallocation; use when the other object can die first (delegates, parent back-references)unowned: non-optional, no zeroing; use only when the referenced object always outlives this one (e.g. a card that can't exist without its customer)- Applies to class instances (and closures, actors); structs and enums are values, though their class-typed fields are still reference counted
- Cycles: object ↔ object, or object → closure →
self; break them withweak/unownedor capture lists[weak self]
If they dig deeper
Swift doesn't guarantee an object lives until the end of its scope; the optimizer may release it right after its last use, so code that relies on a side effect in deinit should use withExtendedLifetime. unowned(unsafe) skips even the runtime check (like __unsafe_unretained); avoid it. weak let (SE-0481) lets a class with a fixed weak reference keep its properties immutable, which helps Sendable conformance when the referenced type is Sendable. Swift 6.2 adds isolated deinit so an actor-isolated class can clean up on its actor. In Swift Concurrency, a Task strongly captures self until it finishes; that's a delay, not a permanent cycle, unless the task never ends (an infinite for await loop) or self stores the task.
Common pitfalls
Making every closure [weak self] by reflex (non-escaping closures and short-lived tasks can't form lasting cycles). Using unowned when the object can be deallocated first, turning a leak into a crash. Strong delegate properties. Timers, NotificationCenter block observers and CADisplayLink that retain their target. Assuming a weak property in a struct makes the struct "weak".
How to use it
final class Owner {
let name: String
var pet: Pet? // strong (the default; there is no `strong` keyword)
init(name: String) { self.name = name }
deinit { print("\(name) freed") }
}
final class Pet {
weak var owner: Owner? // weak: optional, set to nil when Owner is freed
init() {}
}
final class Card {
unowned let customer: Owner // unowned: non-optional; Card must not outlive customer
init(customer: Owner) { self.customer = customer }
}
final class Badge {
weak let owner: Owner? // `weak let` (Swift 6.2): fixed reference, can still go nil
init(owner: Owner?) { self.owner = owner }
}
func demo() {
var alice: Owner? = Owner(name: "Alice") // count 1
let pet = Pet()
alice?.pet = pet // Owner → Pet strong
pet.owner = alice // Pet → Owner weak: no cycle
alice = nil // count 0 → deinit runs, pet.owner == nil
}Best practices
- Default to strong references; add
weakfor back-references and delegates,unownedonly with a guaranteed lifetime - Use
[weak self]in escaping closures stored byselfor its dependencies; skip it for non-escaping closures - Cancel long-running tasks (
.taskin SwiftUI cancels automatically) so they releaseself - Verify with the Memory Graph debugger and Leaks instrument rather than guessing
- Let ARC handle retain/release; never reason about exact retain counts
Related concepts
- Retain cycles, weak references, capture lists, Debug Memory Graph, Leaks instrument, autoreleasepool, heap vs stack, copy-on-write