Kodeco Forums

ARC and Memory Management in Swift

Ever wondered how memory management works in Swift? Or what common pitfalls are? Take a dive into ARC and memory management in Swift with this tutorial.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/959-arc-and-memory-management-in-swift
1 Like

Wow! incredibly eye-opening material regarding memory management. This is going straight to my cheat sheet playgrounds folder. One question regarding the use of reference types for the Node class. In a recent tutorial on the site, the author created a binary tree in Swift.

Tutorial - https://www.raywenderlich.com/139821/swift-algorithm-club-swift-binary-search-tree-data-structure

The author replaced a node class similar to the one you defined by creating an enum with the indirect keyword specified to build the tree structure. To me this seemed to be forcing the use of value types when the use case seemed to call for a reference type. Does one approach or the other have a clear advantage when taking memory management into consideration?

Awesome tutorial on reference cycles! I would love to see more about this subject with real world examples and how to troubleshoot them. Thank you.

Hi faircoder! Thanks for the positive feedback!

The choice between a reference type or a value type depends on the situation. Generally, Swift favors structs over classes for multiple reasons:

  • Structs are allocated on the Stack instead of on the Heap, which makes them considerably faster.
  • They’re more ‘intuitive’: you never have to worry about them being changed in one place and in another one at the same time
  • And of course: no memory leaks… :]

In this tutorial, I added the ‘Node’ example to illustrate that reference cycles with structs are impossible because of their non-recursive nature.

But again, the choice between a reference or value type is dependent on each unique case. Reference types have features like the one illustrated in this tutorial; namely the fact that they disappear when they’re not referenced anymore, that can be useful in some situations.

Excellent article!

I am a 51 year-old programming beginner.
I was so impressed by your teaching skill.
I had been confused by choosing whether weak or unowned until I came across your article.

I translated this article Japanese and post it on my bolghttp://yataiblue.hatenablog.com/entry/2016/12/09/000000

Thank you!

1 Like

Hi yataiblue-san,

Let me update my comment because I was wrong. Ray does allow a limited number of translations with certain requirements which your blog meets! https://www.raywenderlich.com/faq

Sorry for the confusion! Thanks for your enthusiasm again.

@eastprogrammer surprised to know your level of knowledge on Memory Management. Expecting more tutorials from you on this topic. Cheers!

This is a totally off-topic comment, but may I suggest that you change your tutorial where you specify a telephone country code from “0032” to “+32”. That’s the correct way to specify country codes, and the “00” prefix is not universal - it’s different for use in many countries. So then you’ll help educate people about both ARC and the international telephone system. :slight_smile:

This tutorial is more than six months old so questions regarding it are no longer supported for the moment. We will update it as soon as possible. Thank you! :]