Swift Course: How ARC works

Maksim Vialykh
3 min readApr 24, 2018

--

We will take a short overview in swift memory management.

Official Apple documentation you can read on Apple Developer.

What we’ll know from that article:

  • What is ARC. How it works?
  • strong, unowned, weak references
  • Strong reference cycle between class instances
  • Strong reference cycle for closures
  • Capture list
  • Autorelease Pools

What is ARC. How it works?

ARC — automatic reference counting. ARC automatically frees up memory used by class when instance haven’t strong references.

Reference counting aplied only for instances of classes.

Structures and enumerations are value types and not stored and passed by reference. There are stored on stack.

swift ARC in action

strong, unowned, weak references

strong — that is default reference type. When you assign class instance to a variable (var), constant (let) that makes a strong reference to the instance.

The “strong” reference doesn't allow instance to be deallocated for as long as that reference remains.

strong reference

weak — That reference doesn’t keep a strong hold on the instance if refers to.
The weak reference prevents strong reference cycles between class instances.

The ARC automatically sets weak reference to nil when the instance that it refers to is deallocated.

Weak reference always declared as variables because that needs possibility to be changed to nil at runtime.

Note from Apple:

Property observers aren’t called when ARC sets a weak reference to nil.

You can check existense of a value in weak reference, just like any other optional value using “guard let as?”/”if let as?” constructions. That reference will never be setup to invalid instance.

weak reference

unowned — That does not keep a strong hold on the instance it refers to. Similar like a weak reference.

Unowned reference always have a value. ARC never sets value of unowned reference to nil. Unowned reference defined using nonoptional types.

IMPORTANT from Apple:

Use an unowned reference only when you are sure that the reference always refers to an instance that has not been deallocated.

If you try to access the value of an unowned reference after that instance has been deallocated, you’ll get a runtime error.

unowned reference

Strong reference cycle between class instances

Situation when two class instances hold a strong reference to each other, such that each instance keeps the other alive names “strong reference cycle”.

When it’s happens an instance of a class never gets to a point where it has zero strong references because it has one more has cycled reference.

strong reference cycle

Swift provides two ways to resolve strong reference cycles: weak references and unowned references.

Weak and unowned references enable one instance in a reference cycle to refer to the other instance without keeping a strong hold on it.

See previous article part: reference types.

Strong reference cycle for closures

Strong reference cycle can also occur when closure is an class instance property and the body of closure captures the instance.

strong reference cycle for closure

Capture list

That construction allows us using weak/unowned references in closure.

capture list

Autorelease Pools

Info from Apple Developer

Autorelease pool blocks allow objects to relinquish ownership without being deallocated immediately. Typically, you don’t need to create your own autorelease pool blocks, but there are some situations in which either you must — such as when spawning a secondary thread — or it is beneficial to do so — such as when writing a loop that creates many temporary objects.

In Objective-C, autorelease pool blocks are marked using @autoreleasepool. In Swift, you can use the autoreleasepool(_:) function to execute a closure within an autorelease pool block.

Reduce memory usage

You’ve done it!

Remember, the receipt to success is continuous learning and communication with colleagues including, self-education.

This was a small guide about Swift ARC memory management. Read more on Apple Developer site. Don’t stop self-education.

Subscribe to my account to get notifications about new stories.

If you still have questions — write about it in the comments. I’ll answer them in future articles.

Next Lesson->

--

--