MRC vs ARC — what's the difference?
Essence
Both models are reference counting: an object has a retain count, and when it reaches zero dealloc runs. The difference is who inserts the retain/release/autorelease calls.
- MRC (Manual Reference Counting, before 2011) — the programmer, following the ownership rules: methods named
alloc/new/copy/mutableCopyreturn a +1 object you must release; everything else is autoreleased, and if you want to keep it youretainit. - ARC (Automatic Reference Counting) — the clang compiler inserts the same calls at compile time by the same rules (including method-name analysis), and the optimizer removes paired retain/release. It is not a garbage collector: no pauses, no graph tracing, nothing happens "by itself" at runtime.
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
@end
#if !__has_feature(objc_arc)
// MRC: the setter is written by hand
@implementation Person
- (void)setName:(NSString *)name {
if (_name != name) {
[_name release];
_name = [name copy];
}
}
- (void)dealloc {
[_name release];
[super dealloc];
}
@end
#else
// ARC: the compiler generates the same thing from the copy attribute
@implementation Person
@end
#endif
The rest of this answer, with code and interview follow-ups, is in the DevPrep app.
Read the full answer in DevPrep