6

In Objective-C or Swift when ARC (Automatic Reference Counting) is working? On compilation or on run time stage? And why is it matter?

Aleksei Danilov
  • 615
  • 1
  • 10
  • 16

2 Answers2

14

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

Alexander
  • 59,041
  • 12
  • 98
  • 151
12

From Apple's Transitioning to ARC Release Notes

ARC is a compiler feature that provides automatic memory management of Objective-C 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.

Example ARC enabled class:

@interface Person : NSObject
@property NSString *firstName;
@property NSString *lastName;
@end

@implementation Person

// ARC insert dealloc method & associated memory 
// management calls at compile time.
- (void)dealloc
{
    [super dealloc];
    [_firstName release];
    [_lastName release];
}

@end
jscs
  • 63,694
  • 13
  • 151
  • 195
Shamim Hossain
  • 1,690
  • 12
  • 21
  • 4
    The real answer is both at compile time and at runtime. There is code emitted such that a frame can know if another frame of execution is ARC or not and avoid the autoreleasepool, when possible. – bbum Oct 23 '17 at 21:16