Tag Archives: ARC

iOS Automatic Reference Counting (ARC)

copied from


With iOS 5 Apple has released the most appealing language feature, automatic reference counting or ARC. It makes memory management a lot simpler because you no longer have to manually keep a count of object references – the compiler takes care of it.

Manual Reference Counting

If you have been developing on the earlier versions of iOS 4 then you are probably familiar with reference counting. In a manual reference counting environment you have to keep track of object ownership. If you own an object you must make sure to release it, if you don’t then you will have a memory leak, which eventually leads to app crashes. Aside from crashes or leaks keeping count of the retain, release and autorelease is cumbersome.

Here’s a typical example, you allocate a new object called obj1, then you point obj2 to obj1. At this point you have to make sure that you retain obj2, which increases the reference count. In addition, you have to make sure you send a release to each object to free memory once the retain count goes down to zero. This is a simple example but in a normal sized app this can get very complicated, very quickly.

Manual reference counting

Automatic Reference Counting (ARC)

The above example is simplified when using ARC.

MyClass *obj1 = [[MyClass alloc] init];
MyClass *obj2 = obj1;

As you can see, you still allocate obj1 and then point obj2 to obj1. The retains and releases are still occurring, although now it all happens behind the scenes. It is the compiler’s job to maintain the life of an object and to ensure that it is appropriately deallocated when no longer in use. Since this is not garbage collection, you do not sacrifice speed. Your app is just as fast and now with less maintenance. You get to focus on the features of your app and not get distracted by memory management.

Strong vs Weak

With ARC the only thing you have to consider are the qualifiers Strong and Weak.

The strong qualifier is set by default. When using this qualifier you are instructing compiler that you would like to retain an abject for the current event cycle.

You can still use retain, however it is recommended that you use strong instead.

The weak qualifier also known as a zeroing weak reference, instructs the compiler that you do not need to retain the object. And if all the references to this object go down to zero then the object is released and set to nil. This is important because a message send to a nil object does not cause a crash, it simply doesn’t do anything.

You can still use assign, however it is recommended that you use weak instead because it will set a deallocated object to nil.

A weak qualifier is especially used in a parent child object relationship, where the parent has a strong reference to a child object. And the child object a weakreference back to parent otherwise you will end up creating a circular reference.

Weak references

Upgrading to ARC

Upgrading an existing project to use ARC is very simple. Open up your project in Xcode 4.2 and from the menu select Edit > Refactor > Convert to Objective-C ARC. The conversion process first performs a pre-flight check and then ensures that you are ready to upgrade to ARC. It then gives you a preview of all the changes it will make to your code before finally committing the changes.

Upgrading to ARC

Further Reading

This is just a preview of the changes that come with ARC. There are plenty of little nuances that you can read about in the articles below:
Technical specification of ARC
Transition to ARC release notes