Memory Management in Swift

becomingiOSbecomingiOS
5 min read

Memory management is a critical aspect of software development, particularly in mobile environments where constraints are bordering on severe. Proper memory management ensures that an application uses system resources efficiently, avoids memory leaks, and maintains optimal performance. Understanding memory management is essential for developers to prevent common issues such as crashes and slow performance, which can arise from improper handling of memory.

ARC

Swift employs Automatic Reference Counting (ARC) to manage the memory of objects. ARC automatically keeps track of the number of active references to each object, and when there are no longer any active references, the object is deallocated from memory. This system relieves developers from the manual memory management tasks that are necessary in languages without garbage collection, such as C.

ARC works by incrementing a reference count when an object is referenced and decrementing it when the reference is released. When the count drops to zero, the object is deallocated. Swift handles most of this process automatically, but developers need to be aware of how their code affects reference counts, especially in complex scenarios involving closures and class hierarchies.

Here is a code examples to illustrate how ARC works in Swift:

class Person {
    let name: String

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

    deinit {
        print("\(name) is being deinitialized")
    }
}

var reference1: Person?
var reference2: Person?
var reference3: Person?

reference1 = Person(name: "Tim Cook") 
// Prints "Tim Cook is being initialized"
reference2 = reference1
reference3 = reference1

reference1 = nil
reference2 = nil
// The Person instance is not yet deallocated because reference3 is still holding a strong reference to it.

reference3 = nil 
// Prints "Tim Cook is being deinitialized"
// The Person instance is now deallocated because there are no more strong references to it.

In this example, a Person instance is created and assigned to reference1. Then, reference2 and reference3 are also assigned to the same instance, increasing the reference count. As each reference is set to nil, the reference count is decremented. When the last reference (reference3) is set to nil, the reference count drops to zero, and ARC deallocates the instance, calling its deinitializer.

Reference Cycles

A reference cycle, also referred to as a retain cycle, occurs when two or more objects hold strong references to each other, preventing ARC from deallocating them even when they are no longer needed. This can lead to memory leaks, as the objects are retained and remain in memory indefinitely.

A person and a robot holding hands that can never let go of one another, even if they tried.

For example, a Person object might have a strong reference to an Apartment object, which in turn has a strong reference back to the Person. If both objects are no longer needed but still reference each other, they will not be deallocated, resulting in a memory leak.

class Apartment {
    let unit: String
    var tenant: Person?

    init(unit: String) {
        self.unit = unit
    }

    deinit {
        print("Apartment \(unit) is being deinitialized")
    }
}

class Person {
    let name: String
    var apartment: Apartment?

    init(name: String) {
        self.name = name
    }

    deinit {
        print("\(name) is being deinitialized")
    }
}

var tim: Person?
var unit4A: Apartment?

tim = Person(name: "Tim Cook")
unit4A = Apartment(unit: "4A")

tim?.apartment = unit4A
unit4A?.tenant = tim

tim = nil
unit4A = nil
// Neither the Person nor the Apartment instance is deallocated because of a strong reference cycle.

In this example, a strong reference cycle is created because the Person instance has a strong reference to the Apartment instance through its apartment property, and the Apartment instance has a strong reference back to the Person instance through its tenant property. Even after setting tim and unit4A to nil, the instances are not deallocated because of the strong reference cycle.

Well, you're probably thinking - how do we get out of this mess?!

Weak Self

To break reference cycles, Swift provides weak references. A weak reference does not increase the reference count of an object and can be used to prevent cycles. For instance, in the Person and Apartment example, making the Apartment's reference to Person weak ensures that the Person object can be deallocated even if the Apartment still exists.

Using weak self in closures is another common practice to prevent retain cycles. Closures can capture and hold strong references to self, which can lead to cycles if self also holds a strong reference to the closure. By capturing self weakly, the closure does not prevent the object from being deallocated.

class Apartment {
    let unit: String
    weak var tenant: Person? // Use weak to avoid a strong reference cycle

    init(unit: String) {
        self.unit = unit
    }

    deinit {
        print("Apartment \(unit) is being deinitialized")
    }
}

// Person class definition remains the same as in Example 2

var tim: Person?
var unit4A: Apartment?

tim = Person(name: "Tim Cook")
unit4A = Apartment(unit: "4A")

tim?.apartment = unit4A
unit4A!.tenant = tim

tim = nil // Prints "Tim Cook is being deinitialized"
unit4A = nil // Prints "Apartment 4A is being deinitialized"

// Both instances are deallocated because the weak reference does not prevent ARC from deallocating the Person instance.

In this modified example, the tenant property of the Apartment class is declared as a weak reference. This prevents the strong reference cycle, allowing ARC to deallocate the instances when their references are set to nil.

Wrap-Up

In conclusion, memory management in Swift, primarily through ARC, is designed to be mostly automatic, but it requires a good understanding of reference counts, reference cycles, and weak references to avoid common pitfalls. By using tools like weak references and being mindful of how objects are related, you can write cleaner, safer, and more performant Swift code which translates to better apps.

Stay Swifty!

10
Subscribe to my newsletter

Read articles from becomingiOS directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

becomingiOS
becomingiOS

New York City-based iOS Indie Software Developer. Learning Addict.