DevPrep

All topics › Modern Swift

How does ARC work and when do retain cycles occur?

Middle PRO

The idea

ARC (Automatic Reference Counting) — the compiler inserts retain/release where strong references to a class instance appear and disappear. The count lives in the object header; when it drops to zero, deinit runs and the memory is freed — deterministically, with no garbage collector. ARC applies only to classes (and to closures, whose context is also a heap object); structs and enums are simply copied.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store