What is ARC in iOS, and how does it work?
Instruction: Describe Automatic Reference Counting (ARC) and its role in memory management.
Updated
Official answer
Automatic Reference Counting (ARC) manages the lifetime of class instances through strong ownership. The compiler generates and optimizes ownership operations; reference counts and deallocation operate at runtime. A class instance can be destroyed when its last strong owner releases it.
ARC is not a tracing garbage collector and does not collect strong reference cycles. For example, an owner that stores a closure which strongly captures that owner can remain alive after all outside references disappear. Review the ownership graph and use a non-owning reference where the relationship permits it.
In Swift, weak references become nil when their target is deallocated. An ordinary unowned reference requires a valid lifetime relationship; accessing it after the target is gone traps. Neither is a substitute for having a real owner.
This standalone Swift example demonstrates two strong references and a non-owning observer:
final class Resource {}
var first: Resource? = Resource()
var second = first
weak var observed = first
first = nil
assert(observed != nil)
second = nil
assert(observed == nil)
ARC reduces manual ownership errors, but leaks, excessive caches and resources that need explicit cancellation or closing still require attention. Use memory graphs and Instruments to investigate an actual retained object or allocation trend.