Slide 1

Slide 1 text

iOS Memory Analysis and Management Cyril 'notorca' Lashkevich, iOS solutions architect, Viber. piątek, 31 maja 13

Slide 2

Slide 2 text

MRC (pre ARC) piątek, 31 maja 13

Slide 3

Slide 3 text

• Three magic words • alloc* • new* • *copy MRC (pre ARC) piątek, 31 maja 13

Slide 4

Slide 4 text

• Three magic words • alloc* • new* • *copy • retain/release/autorelease MRC (pre ARC) piątek, 31 maja 13

Slide 5

Slide 5 text

• Three magic words • alloc* • new* • *copy • retain/release/autorelease • 2 questions MRC (pre ARC) piątek, 31 maja 13

Slide 6

Slide 6 text

• Three magic words • alloc* • new* • *copy • retain/release/autorelease • 2 questions MRC (pre ARC) • Where is reference count stored? piątek, 31 maja 13

Slide 7

Slide 7 text

• 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

Slide 8

Slide 8 text

• 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

Slide 9

Slide 9 text

piątek, 31 maja 13

Slide 10

Slide 10 text

• @autoreleasepool guarantee balance of pool create/drain. • Interface to a per-thread stack piątek, 31 maja 13

Slide 11

Slide 11 text

ARC piątek, 31 maja 13

Slide 12

Slide 12 text

ARC • It just works. • It is not a kind of GC • It is smarter then you • It is faster piątek, 31 maja 13

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

It just works - (id)init { if (self = [super init];) { self.wallet = [[Wallet alloc] init]; } return self; } piątek, 31 maja 13

Slide 15

Slide 15 text

It just works piątek, 31 maja 13

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

ARC is safer Foo *foo = [self foo]; [foo bar]; [self baz]; [foo quux]; piątek, 31 maja 13

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

• 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

Slide 23

Slide 23 text

Why it is faster •[obj release] -> objc_release(obj) • objc_retainAutoreleasedReturnValue(id) objc_retainAutoreleaseReturnValue(id) piątek, 31 maja 13

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

• http://clang.llvm.org/docs/ AutomaticReferenceCounting.html • http://www.mikeash.com/pyblog/friday- qa-2011-09-30-automatic-reference- counting.html • http://www.idryman.org/blog/2012/11/22/arc- best-practices-and-pitfalls/ Useful links: @notorca The End piątek, 31 maja 13