ARC and Retain Cycles

ARC or Automatic Reference Counting is system by which objects store a count of strong references that have been created in memory.

Once the number of references reaches 0, the object can be deinitialized. 

A strong reference cycle or reference retain cycle is when two objects store strong references to each other. In this case, neither object can be removed from memory because the reference count will never reach 0.

Imagine a case where this type of object relationship was being created in a loop. The memory used by the app would grow indefinitely. This would be the cause of a memory leak.

It is important to be careful when creating object models that reference other objects. Closures can be particularly tricky because they store references to objects in a way that isn’t obvious to the developer.

A way to have an object relationship where two objects reference each other would be to make one of the references weak or unowned. 

This can be accomplished by marking a property @unowned or @weak in a class, struct, or enum.

In a closure, a capture list is used to mark references as strong, weak, or unowned.

Leave a Reply

Come See Me

San Francisco, CA
hello@ingax.com

See My Work