Objective-C Memory Management with Automatic Reference Counting (ARC)

Posted: November 27, 2011 in iOS Development, Quick Tips
Tags: , , ,

iOS 5 introduced a new feature called Automatic Reference Counting (ARC) few months back. My initial assumption was – either it brings in garbage collection for iOS Development or memory is released by compiler on a certain period of time like how it works for autorelease pools. But when I dived more into ARC I found it it’s neither of the above and it’s something much more powerful which makes the life of programmer easier.

In XCode 4.2 the new LLVM compiler reduces the effort of developers by automating the process of memory management. So developers don’t need to manually retain, release or autorelease objects. However, to repeat again – this is not garbage collection, ARC just automates the memory management process similar to how syntax checking is done as you type. You can create a new XCode project by selecting to use Automatic Reference Counting or you can also refactor your existing codes to Objective-C Automatic Reference Counting format.

But few things to remember once you do so:

1 – retain, release, autorelease, retainCount methods won’t work

In ARC mode retain, release, autorelease, retainCount and similar manual memory management methods are obsoleted. Usage of these methods will give compiler error as above. Memory of an object is retained, released and managed automatically by compiler based on two primary things.

  • Scope of the Object
  • Its property attributes

2 – dealloc method will be missing from classes

By default dealloc method will be missing from Objective-C classes. dealloc() method shouldn’t be called explicitly rather it gets called when an object no longer has any owner or in other words the retainCount of an object becomes 0. If you need to reset any of the state information or release any resources which are not instances of the class you can create a custom dealloc() method which gets called when the object no longer has any owner. However, you shouldn’t call [super dealloc] as that will be enforced by the compiler.

3 – retain, assign, copy “@property” attributes are no-longer used

In stead of retain, assign, copy “@property” attributes, ARC uses two new attributes to declare the property of the variables.

  • weak : a reference to an object that does not stop it from being deallocated. (Used in place of copy or assign)
  • strong : a reference to an object that stops it from being deallocated (Usually used to retain the variable)

4 – Use @autoreleasepool{} blocks in stead of NSAutoReleasePool

You must not use NSAutoReleasePool objects in ARC compliant code, instead use the @autoreleasepool{}blocks. Refer to main.m file in your XCode project to see how it uses @autoreleasepool{} block in stead of NSAutoReleasePool that was being used earlier.

You can find more about Automatic Reference Counting from the official site.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s