IRON MAN does not have to do anything with ARC or SWIFT or APPLE.

Automatic reference counting (ARC) in Action

Siddharth Paneri
3 min readFeb 6, 2021

Memory management in Swift? ARC?

ARC? uhh.. yeah sure, I heard about that. — Swifties

I am not criticising Swift, in-fact it’s a great language, but the point is that new developers who were pushed by our industry to learn just Swift, rarely know anything about memory management, the only thing they would talk about is[weak self].

Is this the only thing that Swift exposes to us?.

Almost everybody who migrated from Objective-C knows about memory management a few of these developers know more and have struggled through migrating their apps from MRC to ARC.

Well, I am not going to talk about MRC and bore you. Either you won’t get it or you won’t be interested in it. But, trust me if all of us were exposed to MRC memory management as computer engineers would have been one of our core skills.

Every day we have to make decision save memory? or boost execution time? and it’s neither an easy decision to make nor an easy pill to eat. With a growing community of developers and size of apps it’s very important to understand why memory is important in the apps we build and here is a small contribution to this community where memory management discussions have become limited to huge development houses only, why? because ARC in Swift takes care of most bits.

Before we see it in action we need to understand,

How ARC works?

Every time we create an instance to a class (Reference types) ARC allocates some memory to store the information about this instance. What does it hold?
Well it holds information like the type of instance and values of stored properties associated with this instance.

Now consider this, when our program no longer needs the instance, ARC would free up its memory. and so Automatic. We have not seen how it works yet.

If for some reason we try to access any property or method from the instance that that has been deallocated we might end up getting a crash (Well only if you don’t use optional ?).

Crashes are important! But how does ARC help us?

Well, it holds on to the instance until there is no one claiming for it.
Meaning? To make sure the instance does not disappear ARC tracks the information about how many properties, variables, constants are still referring to that instance. and this brings in the concept of strong reference

Whenever we create a constant (let), variable (var) with the instance of a class, it holds the strong reference to that instance and ARC makes sure until and unless there is at least one strong reference to that instance it will not deallocate the memory.

Let’s see it in action

Here we have a SuperHero class, can be initialise using a name.
Try copying the content in your Swift playground.

now let’s create a reference to our Super Hero.

If you now press the play button on your playground you shall see the console with below output

Tony Stark is our new superhero. Woohoo, IRON MAN

Let’s get more references to Tony!

What do you see in the playground now?

Tony Stark is our new super hero. Woohoo, IRON MAN

It’s the same output. Well, references you see.

Let’s get rid of the references now. What if you removed Tony?

Umm no change in the playground?

Why don’t you get rid of all the refs to tony

That’s sad that IRON MAN is no more but we learned something, look at the output console.

Tony Stark is our new super hero. Wohooo, IRON MAN

We don’t have Tony Stark among us now, sorry to announce but IRON MAN is DEAD!

That’s when we remove all the references to tonyStark or SuperHero object ARC decides to deallocate the instance and deinit is called, fun isn’t it.

I won’t mark this blog complete but I think it’s enough for one read and see ARC in action.

Did you know? ARC does not always work by itself, it is automatic but not smart like you.

I will write the next blog on this. but for now
I must say goodbye IRON MAN.

We ❤️ you 3000

--

--

Siddharth Paneri

iOS Applications expert with strong skills in design, architecture, UX standards, and DevOps for Mobile. Product ideology!