Automatic Reference Counting
May
11
2019

What is ARC(Automatic Reference Counting) in iOS
Swift ?

  1. Memory management functions and its usage are handled in
    Swift language through Automatic reference counting.
  2. 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?

  1. 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.
  2. 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

  1. ARC allocates a chunk of memory to store the information
    each and every time when a new class instance is created
    by init().
  2. Information about the instance type and its values are stored
    in memory.
  3. When the class instance is no longer needed it automatically
    frees the memory space by deinit() for further class
    instance storage and retrieval.
  4. ARC keeps in track of currently referring class instances
    properties, constants, and variables so that deinit() is
    applied only to those unused instances.
  5. 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 ?

  1. 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.
  2. 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.
  3. 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

  1. 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.
  2. 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

  1. 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.
  2. It should only be used when we are sure that both the
    an instance has almost the same or more life span.
  3. 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”