Swift & Concurrency (50 Qs)•Part 1: Core Language, Memory & Value Types (1 - 10)
How does Automatic Reference Counting (ARC) work in Swift?
junior LevelswiftCore Language
In Simple Words: ARC automatically manages memory for class instances on the heap:
- Every time a new reference to an instance is created, ARC increments its reference count by 1.
- When a reference goes out of scope or is set to
nil, ARC decrements the reference count by 1. - When the reference count drops to 0, the instance is immediately deallocated from memory, and its
deinitblock is invoked. - ARC operates at compile-time by injecting
retainandreleasecalls into your code; it is not a garbage collector.