0
0
Swiftprogramming~10 mins

ARC overview for memory management in Swift - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ARC overview for memory management
Create Object
Increase Reference Count
Use Object
Decrease Reference Count
Reference Count == 0?
NoKeep Object Alive
Yes
Deallocate Object
This flow shows how ARC tracks how many references point to an object. When no references remain, ARC frees the object.
Execution Sample
Swift
class Person {
  var name: String
  init(name: String) { self.name = name }
  deinit { print("Person deallocated") }
}

var p1: Person? = Person(name: "Anna")
p1 = nil
This code creates a Person object, assigns it to p1, then sets p1 to nil, triggering ARC to deallocate the object.
Execution Table
StepActionReference CountObject StateOutput
1Create Person("Anna") and assign to p11Object alive
2Set p1 = nil (remove reference)0Object deallocatedPerson deallocated
3End--No references remain, object freed
💡 Reference count reached zero after p1 set to nil, so ARC deallocates the object.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
p1nilPerson instancenilnil
Reference Count0100
Key Moments - 3 Insights
Why does setting p1 to nil cause the object to be deallocated?
Because p1 was the only reference to the object, setting it to nil reduces the reference count to zero, triggering ARC to free the object (see execution_table step 2).
What happens if multiple variables reference the same object?
ARC increases the reference count for each variable. The object is only deallocated when all references are removed, meaning the count reaches zero.
Why do we see 'Person deallocated' printed?
The deinit method runs when ARC frees the object, showing that the object is cleaned up properly (see execution_table step 2 output).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the reference count after creating the Person object?
A0
B1
C2
Dnil
💡 Hint
Check execution_table row 1 under 'Reference Count'
At which step does ARC deallocate the object?
AStep 1
BStep 3
CStep 2
DNever
💡 Hint
See execution_table row 2 where reference count becomes zero and object is deallocated
If another variable p2 also referenced the Person object, what would happen when p1 is set to nil?
AReference count decreases but object stays alive
BReference count increases
CObject is deallocated immediately
DProgram crashes
💡 Hint
Recall that ARC deallocates only when reference count reaches zero; see key_moments about multiple references
Concept Snapshot
ARC (Automatic Reference Counting) tracks how many references point to an object.
When references increase, count goes up; when references are removed, count goes down.
If count reaches zero, ARC frees the object automatically.
Use optional variables to hold references; setting them to nil removes references.
Deinit runs when object is deallocated, useful for cleanup.
Full Transcript
This visual execution shows how ARC manages memory in Swift. First, creating a Person object sets its reference count to 1 because p1 points to it. When p1 is set to nil, the reference count drops to zero. ARC then deallocates the object, running the deinit method which prints 'Person deallocated'. Variables hold references; ARC counts these references to decide when to free memory. If multiple variables reference the same object, ARC waits until all are nil before deallocating. This automatic process helps prevent memory leaks and crashes by cleaning up unused objects safely.