Automatic Reference Counting (ARC)
ARC (Automatic Reference Counting) is a memory management system used in Swift and Objective-C to automatically manage the allocation and deallocation of objects in memory. Here’s how ARC works in Swift:
- Every object in Swift has a reference count, which represents the number of strong references to the object that exist in the program.
- When an object is first created, its reference count is set to 1, because there is one strong reference to the object (usually the variable that holds a reference to the object).
- Whenever a new strong reference to the object is created (e.g. by assigning the object to a new variable), the reference count is incremented by 1.
- Whenever a strong reference to the object is destroyed (e.g. by setting the variable to nil), the reference count is decremented by 1.
- When an object’s reference count reaches 0, it is deallocated by the memory management system.
- ARC works automatically in the background, so you don’t have to manually allocate or deallocate memory for objects.
- In addition to strong references, Swift also supports weak references and unowned references. Weak references are used to break strong reference cycles and prevent retain cycles, while unowned references are used when the reference is guaranteed to always be non-nil.
- ARC can sometimes lead to memory leaks if there are circular references (retain cycles) between objects, where each object holds a strong reference to the other. To avoid memory leaks, you can use weak references or unowned references to break the strong reference cycle.
- ARC works seamlessly with Swift’s optionals, so you don’t have to manually check for nil before using an object.
Overall, ARC is a powerful and convenient memory management system that makes it easy to manage object memory in Swift. By automatically allocating and deallocating objects as needed, ARC helps to prevent memory leaks and improve performance in our Swift code.
NOTE:
- ARC is a compile-time feature, meaning that it is built into the Swift compiler and does not require any special runtime support.
- Reference counting applies only to instances of classes. Structures and enumerations are value types, not reference types, and are not stored and passed by reference.
Internal working of ARC:
ARC (Automatic Reference Counting) is a memory management system used by Swift to automatically manage the allocation and deallocation of memory for objects. When a new instance of a class is created, ARC allocates a chunk of memory to store information about that instance, including the type of the instance and the values of any stored properties associated with the instance.
However, when an instance is no longer needed, ARC frees up the memory used by that instance so that the memory can be used for other purposes. To ensure that instances don’t disappear while they’re still needed, ARC tracks how many properties, constants, and variables are currently referring to each class instance.
Whenever a property, constant, or variable is assigned a reference to a class instance, that property, constant, or variable makes a strong reference to the instance. This means that the reference keeps a firm hold on that instance, and doesn’t allow it to be deallocated for as long as that strong reference remains.
In summary, strong references in Swift keep a firm hold on class instances, ensuring that the instances don’t disappear while they’re still needed. ARC tracks how many strong references exist for each instance and deallocates the instance’s memory when there are no more strong references remaining.
More info. about the same: