skip to content

How does Swift's ARC differ from a tracing garbage collector, and what does that buy and cost a Swift app?

level: middleimportance: must knowfreq 60%

answer

  1. decided when the code is compiled
  2. no collector, no pass over the heap
  3. cleanup you can predict
  4. work on every reference copy

basics

~20 s

Swift's ARC compiles retains and releases into the code and frees an instance the moment its last strong reference goes. That buys deterministic deinit and no collector pauses; it costs counting work on every reference copy and leaves reference cycles uncollected.

solid answer

~50 s

Swift has no collector: the compiler inserts runtime calls such as `swift_retain` and `swift_release` where references are copied and dropped, and the optimizer removes pairs it proves redundant. Because the release that drops the count to zero runs `deinit` and frees memory on the spot, cleanup is deterministic — a class can close a file or a socket in `deinit` and you know when that happens — and there are no tracing pauses, no collector thread and no extra heap headroom to size. The costs mirror that. Every copy and drop of a class reference does counting work, atomic by default because any thread may share the reference. Releasing the root of a large graph frees the whole graph synchronously on that thread. And objects that reference each other strongly are never reclaimed, so Swift code breaks such cycles with `weak` or `unowned` references.

go deeper

for a junior

Recall that Swift frees objects when their last strong reference goes, with no garbage collector, and that cycles must be broken by hand.

for a middle

Explain that the compiler inserts retains and releases, that deinit is deterministic as a result, and what the counting and the missing cycle collection cost.

for a senior

Discuss release cascades on the main thread, atomic counting on shared objects and when reference traffic is worth measuring in a hot path.

for a principal

Weigh ARC's determinism and small memory footprint against its per-operation cost when choosing between class-heavy and value-type designs for a codebase.

## Two ways to decide an object is dead A **tracing garbage collector** periodically starts from a set of roots, follows references, and frees whatever it cannot reach, at a time the collector chooses. **Automatic Reference Counting (ARC)**, which Swift uses for class instances, keeps a count of strong references on each object and frees it the instant that count reaches zero. Swift has no tracing collector at all; the Swift book describes ARC as working much like ARC for Objective-C, so Swift and Objective-C objects on Apple platforms share one lifetime model. ## What ARC looks like in a Swift binary ARC is not a background service. The **compiler** places the counting operations in your code: - a store such as `slot = newBox`, built with `-O`, becomes a call to `swift_retain` for the new object and `swift_release` for the old one; - the optimizer removes retain/release pairs it can prove redundant — the Swift project's optimization notes describe inlining and ARC optimization removing a retain and release entirely; - value types that contain no references, recursively, need no counting at all, which is one reason the same notes recommend structs of plain values in hot arrays. By default these operations are **atomic**, because a reference may be shared across threads. A frontend flag, `-assume-single-threaded` (passed with `-Xfrontend`), switches the emitted calls to `swift_nonatomic_retain` and `swift_nonatomic_release`, which shows that the default is the thread-safe variant. ## What deterministic release buys 1. **Predictable cleanup.** The release that reaches zero runs `deinit` synchronously, so a class can close a file handle, remove an observer or delete a temporary file in `deinit` and rely on it happening when the last owner lets go, rather than whenever a collector next runs. 2. **No collection pauses.** Nothing stops the program to trace the heap, and there is no collector thread competing for CPU. 3. **Lower peak memory.** Memory comes back as soon as it is unused, so a Swift process does not need spare heap headroom for a collector to work efficiently — useful on memory-constrained devices. 4. **Seamless Objective-C interop.** Objects crossing between Swift and Objective-C follow the same counting rules, with no second memory manager to reconcile. ## What it costs - **Counting traffic.** Every copy and drop of a class reference may touch the object's count. In hot loops this shows up as time in `swift_retain` and `swift_release`; measuring and reducing it is a performance-tuning topic of its own. - **Release cascades.** Freeing is not free. Dropping the last reference to the root of a large graph runs every deinit and frees every node synchronously on the releasing thread — possibly the main thread. - **Cycles are not collected.** Two objects that hold strong references to each other keep each other's counts above zero, and ARC has no tracing pass to notice they are unreachable. The Swift answer is to make one edge `weak` or `unowned`, which is the subject of the material on reference cycles. - **Lifetimes follow use, not scope.** The optimizer may release a local after its last use, so code that relies on an object surviving until a closing brace must say so explicitly. ## Side by side | | Swift ARC | Tracing garbage collector | |---|---|---| | When an object is freed | at the release that drops the strong count to zero | when a collection finds it unreachable | | Cleanup hook | `deinit`, synchronous and immediate | finalizer or cleanup hook, timing up to the collector | | Pauses | none for collection; release cascades run inline | collection pauses, short or long by design | | Per-reference cost | count update on copy and drop | write barriers in some collectors; most cost moves to collection | | Cycles | leak unless broken with `weak` or `unowned` | reclaimed once unreachable | | Memory headroom | not needed | needed for efficient collections | ## Saying it well in an interview The strong answer is not "ARC is faster" or "ARC is free". It is that ARC trades a small, spread-out cost on every reference operation for **determinism** — you know when `deinit` runs and memory comes back — and hands you one responsibility a tracing collector would take: breaking reference cycles. A candidate who adds that the counting happens at run time, even though its placement is decided at compile time, has understood the mechanism.

  • Does ARC mean a Swift app never pauses to free memory?
    There are no collection pauses, but freeing still takes time. Dropping the last reference to a large graph — a cache holding thousands of class instances — runs every deinit and frees every node synchronously on the releasing thread, which may be the main thread. Releasing in smaller pieces, or letting a background owner drop the final reference, keeps that work off a latency-sensitive path.
  • Does every struct copy cost retains and releases too?
    Only when the struct contains references. A struct made of plain values such as `Int` and `Double`, recursively, needs no counting at all. One holding a class reference, an `Array` or a large `String` carries reference-counted storage, so copying it retains that storage, unless the optimizer can prove the copy unnecessary.

  • Swift runs a garbage collector, just a faster one
  • ARC costs nothing at run time because it happens at compile time
  • Swift's deinit timing is as unpredictable as a finalizer
  • ARC reclaims two objects that only reference each other
  • Every struct copy pays a retain and a release