ARC, weak references and ownership cycles
ARC, weak references and ownership cycles
A class instance should remain alive while the program owns it and be released when its owners no longer need it. Automatic Reference Counting, or ARC, manages class-instance lifetime through ownership references. A strong reference keeps an instance alive; a weak reference does not.
What you’ll learn
- Trace strong ownership and a weak observer.
- Recognize why a remaining strong reference or cycle prevents release.
ARC, weak references and ownership cycles in the example
Strong owner
A class instance should remain alive while the program owns it and be released when its owners no longer need it.
Strong ownerWeak observer
Weak references are optional because the referenced object can disappear, at which point the weak reference becomes nil.
Weak observerRelease and nil observer
The strong owner initially keeps the student alive, while observer only watches.
Release and nil observer
How arc, weak references and ownership cycles works
Weak references are optional because the referenced object can disappear, at which point the weak reference becomes nil. Strong cycles occur when instances, or an instance and an escaping closure, keep one another alive. ARC counts those owners but cannot infer that an unreachable cycle is no longer useful.
Work through arc, weak references and ownership cycles
Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.
Swift · complete program
final class Student {
let name: String
init(_ name: String) { self.name = name }
deinit { print("Released:", name) }
}
var owner: Student? = Student("Diya")
weak var observer = owner
print(observer != nil)
owner = nil
print(observer == nil)Expected output
true
Released: Diya
trueFollow the execution
The strong owner initially keeps the student alive, while observer only watches. Releasing the owner lets ARC deinitialize the instance, printing its message, and observer becomes nil. A weak reference would not keep an instance alive by itself.
Try this program in the Swift compiler. Paste the complete source above.
Practice: ARC, weak references and ownership cycles
Your task: Would replacing observer with another strong variable allow this release when owner becomes nil?
Swift · complete program
final class Student {
let name: String
init(_ name: String) { self.name = name }
deinit { print("Released:", name) }
}
var owner: Student? = Student("Diya")
weak var observer = owner
print(observer != nil)
owner = nil
print(observer == nil)Worked answer
Not while that second strong variable still owns the instance. The instance remains alive until its strong ownership is no longer retained.
Review who keeps each object alive
Unowned references also do not own an instance but rely on stronger lifetime assumptions and can trap if accessed after it disappears. Escaping closures that capture self strongly need deliberate cycle analysis; weak capture is useful when disappearance is acceptable, not a universal replacement for ownership.
Check your decision
Review who keeps each object alive
Not while that second strong variable still owns the instance. The instance remains alive until its strong ownership is no longer retained. Unowned references also do not own an instance but rely on stronger lifetime assumptions and can trap if accessed after it disappears.
Further reading: official Swift language guide.
Reaching the recap and completing the quizzes are separate milestones.