Swift’s Automatic Reference Counting (ARC) Explained with Examples

Deepak Carpenter
Appgrid
Published in
2 min readMay 31, 2023

In Swift, Automatic Reference Counting (ARC) handles memory management by automatically managing the creation and destruction of objects. This blog post aims to provide a comprehensive understanding of how ARC works in Swift, including an explanation of object reference counts. Alongside detailed examples, we will explore the intricacies of memory management in Swift.

Swift Automatic Reference Counting

Understanding Automatic Reference Counting (ARC): Automatic Reference Counting (ARC) is a mechanism in Swift that automatically tracks and manages the memory used by class instances. It keeps track of references to objects and deallocates them when they are no longer in use, freeing up memory and preventing memory leaks. By determining object lifetimes and managing reference counts, ARC ensures memory is released appropriately.

Object Reference Count Explanation:
Each class instance has a reference count associated with it. The reference count represents the number of strong references pointing to an object. When a new reference is created, such as assigning an instance to a variable or passing it as an argument, the reference count increases by one. Conversely, when a reference is removed, such as when a variable goes out of scope or is set to nil, the reference count decreases. When the reference count reaches zero, the object is deallocated, and its memory is freed.

Example: Basic Usage of ARC and Object Reference Count Let’s explore a simple example that demonstrates how reference counts change during the lifecycle of an object:

class Person {
let name: String

init(name: String) {
self.name = name
print("\(name) is being initialized.")
}

deinit {
print("\(name) is being deallocated.")
}
}

func createPersons() {
var person1: Person? = Person(name: "John")
print("Reference Count: \(CFGetRetainCount(person1))") // Output: Reference Count: 1

var person2: Person? = person1
print("Reference Count: \(CFGetRetainCount(person1))") // Output: Reference Count: 2

person1 = nil
print("Reference Count: \(CFGetRetainCount(person2))") // Output: Reference Count: 1

person2 = nil
print("Reference Count: \(CFGetRetainCount(person2))") // Output: Reference Count: 0
}

createPersons()

In this example, we create two optional variables person1 and person2 that hold references to the Person object. We observe the reference count at different stages using the CFGetRetainCount() function. As references are created and removed, the reference count changes accordingly. When both references are set to nil, the reference count reaches zero, triggering deallocation.

Final Thoughts:
Automatic Reference Counting (ARC) is a powerful feature in Swift that simplifies memory management by automatically handling object creation and deallocation. Understanding the concept of object reference counts is essential for writing memory-safe code. By automating memory management, Swift allows developers to focus on building robust applications without the burden of manual memory management. Harness the power of ARC in Swift and leverage its automatic memory cleanup capabilities for efficient and reliable code.

Cheers!
Happy Coding!

--

--