Share
Embed
Transcript
25 slides
-
iOS Memory Analysis and Management Cyril 'notorca' Lashkevich, iOS solutions
architect, Viber. piątek, 31 maja 13 -
• Three magic words • alloc* • new* • *copy
MRC (pre ARC) piątek, 31 maja 13 -
• Three magic words • alloc* • new* • *copy
• retain/release/autorelease MRC (pre ARC) piątek, 31 maja 13 -
• Three magic words • alloc* • new* • *copy
• retain/release/autorelease • 2 questions MRC (pre ARC) piątek, 31 maja 13 -
• Three magic words • alloc* • new* • *copy
• retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? piątek, 31 maja 13 -
• Three magic words • alloc* • new* • *copy
• retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? • Not in NSObject. piątek, 31 maja 13 -
• Three magic words • alloc* • new* • *copy
• retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? • Not in NSObject. • How does autorelease pool work? piątek, 31 maja 13 -
• @autoreleasepool guarantee balance of pool create/drain. • Interface to
a per-thread stack piątek, 31 maja 13 -
ARC • It just works. • It is not a
kind of GC • It is smarter then you • It is faster piątek, 31 maja 13 -
It just works - (id)init { if (self = [super
init];) { self.wallet = [[[Wallet alloc] init] autorelease]; } return self; } - (void)dealloc { self.wallet = nil; [super dealloc]; } piątek, 31 maja 13 -
It just works - (id)init { if (self = [super
init];) { self.wallet = [[Wallet alloc] init]; } return self; } piątek, 31 maja 13 -
Not a kind of GC •weak, strong(retain), copy, unsafe_unretained(assign) •__weak,
__strong, __unsafe_unretained • IBOutlets should generally be weak except File’s Owner • Weak objects should not override release • __autoreleased for pass-by-writeback (id *) and for objects thats are autoreleased on return. piątek, 31 maja 13 -
Toll-free bridging • id obj = (__bridge id)CFDictionaryGetValue(cfDict, key); •
NSString *value = (__bridge_transfer NSString *)CFPreferencesCopyAppValue(CFSTR("someKey"), CFSTR("com.company.someapp")); [self useValue: value]; • CFStringRef value = (__bridge_retained CFStringRef) [self someString]; UseCFStringValue(value); CFRelease(value); piątek, 31 maja 13 -
Return values and toll- free bridging •- (CGColorRef)getFooColor { UIColor*
__autoreleasing color = [UIColor redColor]; return [color CGColor]; } •- (CGColorRef)fooColorCopy { UIColor* color = [UIColor redColor]; CGColorRef c = CFRetain([color CGColor]); return c; } piątek, 31 maja 13 -
ARC is safer Foo *foo = [self foo]; [foo bar];
[self baz]; [foo quux]; piątek, 31 maja 13 -
ARC is safer Foo *foo = [self foo]; [foo bar];
[self baz]; [foo quux]; Foo *foo = [self foo]; [foo bar]; [self setFoo: newFoo]; [foo quux]; // crash piątek, 31 maja 13 -
Foo *foo = objc_retainAutoreleasedReturnValue([self foo]); [foo bar]; [self baz]; [self
setFoo: newFoo]; [foo quux]; // fine objc_release(foo); - (Foo *)foo { return objc_retainAutoreleaseReturnValue(_foo); } piątek, 31 maja 13 -
• All objects passed as arguments are retained at the
beginning of the function and released at the end • All values from getters are retained and released at the end of variable scope piątek, 31 maja 13 -
Why it is faster •[obj release] -> objc_release(obj) • objc_retainAutoreleasedReturnValue(id)
objc_retainAutoreleaseReturnValue(id) piątek, 31 maja 13 -
Memory management and blocks. • Use __weak, not __block to
capture self • Copy block when passing 'down' to stack. Passing 'up' compiler handle automaticly. piątek, 31 maja 13