What is ARC(Automatic Reference Counting) in iOS
Swift ?
- Memory management functions and its usage are handled in
Swift language through Automatic reference counting. - Reference Counting applies only to the instances of the
class because it is Reference Type. This means that
multiple objects can refer to the same object. Where as
Structure and Enumerations are Value Types.
Why we Used ARC?
- ARC is used to initialize and deinitialize the system resources
thereby releasing memory spaces used by the class
instances when the instances are no longer needed. - ARC keeps track of information about the relationships
between our code instances to manage the memory
resources effectively.
Example
class EmployeeDetails {
var ename: String!
var enumber: Int!
init(ename: String, enumber: Int) {
self.ename = ename
self.enumber = enumber
}
deinit {
print("Deinitialized of Employee name:- \(self.ename)")
print("Deinitialized of Employee number:- \(self.enumber)")
}
}
let ename = "James Bond"
let enumber = 007
print(ename)
print(enumber)
Output:-
James Bond
007
Explaination of Example
- ARC allocates a chunk of memory to store the information
each and every time when a new class instance is created
by init(). - Information about the instance type and its values are stored
in memory. - When the class instance is no longer needed it automatically
frees the memory space by deinit() for further class
instance storage and retrieval. - ARC keeps in track of currently referring class instances
properties, constants, and variables so that deinit() is
applied only to those unused instances. - ARC maintains a ‘strong reference’ to those class instance
property, constants and variables to restrict deallocation
when the class instance is currently in use.
How ARC Works ?
- Every time we create an instance of a Class, ARC allocates
a memory that holds information about the Type and Values it
holds to. Whenever we assign a class instance to a
constant or variable, that particular constant or variable
makes a strong reference to the instance and does not
allow it to be deallocated as long as that strong reference
exists. - ARC keeps track of all the constants and variables currently it
is referring to and will not deallocate until the last active
reference is released. - Once, all the active references are removed, ARC frees up the
memory by removing the instance. This ensures that class
instances do not take up unnecessary memory when not
required.
What are Reference Cycles?
There could be a scenario where two class instance holds a
strong Reference to each other and there is no way for the
system to deallocate them. This means that the Retain count of
both the class instance would never come down to 0. This is
known as strong Reference cycles
Swift provides two ways of resolving these Strong Reference
Cycles:-
1. Weak Reference
2. Unowned Reference
1.Weak Reference
- Apple Doc states that a weak reference is a reference that
does not keep a strong hold on the instance it refers to, and
so does not stop ARC from disposing of the referenced
instance. This behavior prevents the reference from
becoming part of a strong reference cycle. - You indicate a weak reference by placing the “weak” keyword
before a property or variable declaration.
Use Of Weak Reference
Weak Reference should be used in cases where other
instance can be deallocated first without any issue.
2.Unowned Reference
- Unowned reference too does not keep a strong hold on the
instance it refers to. However, there is one major difference
between Weak and
UnownedReference. - It should only be used when we are sure that both the
an instance has almost the same or more life span. - A Weak references must be defined using optional whereas
Unowned reference must be defined using non-optional
Type. Both do not increase the “Retain Count”
Sources: