How does the new automatic reference counting mechanism work?

Viewed 60451

Can someone briefly explain to me how ARC works? I know it's different from Garbage Collection, but I was just wondering exactly how it worked.

Also, if ARC does what GC does without hindering performance, then why does Java use GC? Why doesn't it use ARC as well?

6 Answers

ARC is a compiler feature that provides automatic memory management of objects.

Instead of you having to remember when to use retain, release, and autorelease, ARC evaluates the lifetime requirements of your objects and automatically inserts appropriate memory management calls for you at compile time. The compiler also generates appropriate dealloc methods for you.

The compiler inserts the necessary retain/release calls at compile time, but those calls are executed at runtime, just like any other code.

The following diagram would give you the better understanding of how ARC works.

enter image description here

Those who're new in iOS development and not having work experience on Objective C. Please refer the Apple's documentation for Advanced Memory Management Programming Guide for better understanding of memory management.

Related