All Articles

What is Automatic Reference Counting (ARC)?

What is Automatic Reference Counting (ARC)?

When you create an object, that object is given a ‘retain count’ or ‘reference count’. What this count is keeping track of is ‘who or what variables care about that object right now?’.

When that object is dropped out of scope, and returns from the function, the reference count goes to 0. Objective-C releases it from memory and makes it available for other objects to use. We don’t have to release it ourselves — as in, write it explicitly — as it’s been taken care for us.

Before, it looked something like this:

CatClass *kitten = [[CatClass alloc] init];
[kitten eatFood];
[kitten release];

There are several issues with this system, such as:

  • releasing the object too soon
  • not releasing the object at all, which will lead to a memory leak!
  • having a pointer pointing to an object in memory that doesn’t actually exist

Now, because of ARC, we don’t need to write release as it is handled for us.

This is just one tidbit of ARC and memory management - more to come!