Junior — Middle
Does iOS use automatic memory management for resource control?
sobes.tech AI
Answer from AI
iOS does not use automatic memory cleanup in the traditional sense, like garbage collection in JavaScript or Java. Instead, iOS employs a memory management system called ARC (Automatic Reference Counting).
ARC automatically tracks the number of references to objects and frees memory when an object is no longer used. Developers do not write code to manually free memory but must manage references correctly to avoid cyclic dependencies that can lead to memory leaks.
Example of using ARC:
class Person {
var name: String
init(name: String) {
self.name = name
}
}
var person1: Person? = Person(name: "Ivan")
var person2 = person1 // reference count increases
person1 = nil // reference count decreases, object still exists
person2 = nil // reference count becomes 0, object is freed
Thus, iOS manages memory automatically based on reference counting, not garbage collection.