Slide 1

Slide 1 text

10 iPhone Memory Management Tips Adrian Kosmaczewski January 28th, 2009 1

Slide 2

Slide 2 text

http://kosmaczewski.net/ 2009/01/28/ 10-iphone-memory-management-tips/ 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

Facts 4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

128 MB RAM 6

Slide 7

Slide 7 text

128 MB RAM 6

Slide 8

Slide 8 text

128 MB RAM ~ 40 MB for you! 6

Slide 9

Slide 9 text

Garbage Collection 7

Slide 10

Slide 10 text

Garbage Collection 7

Slide 11

Slide 11 text

[ alloc | copy | retain] [ release ] 8

Slide 12

Slide 12 text

[ alloc | copy | retain] [ release ] 8

Slide 13

Slide 13 text

9

Slide 14

Slide 14 text

http://cocoadevcentral.com/d/learn_objectivec/ 9

Slide 15

Slide 15 text

Objects on the heap 10

Slide 16

Slide 16 text

// C++ automatic object on the stack // Memory freed when out of scope std::string name(“Adrian”); // C++ object on the heap std::string *name = NULL; name = new std::string(“Adrian”); delete name; 11

Slide 17

Slide 17 text

... autorelease]; 12

Slide 18

Slide 18 text

Virtual Memory 13

Slide 19

Slide 19 text

Virtual Memory 13

Slide 20

Slide 20 text

14

Slide 21

Slide 21 text

Tips 15

Slide 22

Slide 22 text

16

Slide 23

Slide 23 text

1) Respond to Memory Warnings 17

Slide 24

Slide 24 text

- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } 18

Slide 25

Slide 25 text

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)app { [[ImageCache sharedImageCache] removeAllImagesInMemory]; } 19

Slide 26

Slide 26 text

NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; [center addObserver:self selector:@selector(whatever:) name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; 20

Slide 27

Slide 27 text

21

Slide 28

Slide 28 text

2) Avoid Autoreleased Objects 22

Slide 29

Slide 29 text

(when possible) 23

Slide 30

Slide 30 text

// Instead of NSString *string = [NSString stringWithFormat:@"value = %d", var]; // use NSString *string = [[NSString alloc] initWithFormat:@"value = %d", var]; ... [string release]; 24

Slide 31

Slide 31 text

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; for (id item in array) { id anotherItem = [item create]; [anotherItem doSomethingWithIt]; } [pool release]; 25

Slide 32

Slide 32 text

26

Slide 33

Slide 33 text

3) Load Lazily and Reuse 27

Slide 34

Slide 34 text

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { Item *item = [items objectAtIndex:indexPath.row]; if (detailController == nil) { detailController = [[DetailController alloc] init]; } detailController.item = item; [self.navigationController pushViewController:detailController animated:YES]; } 28

Slide 35

Slide 35 text

viewWillAppear: viewDidDisappear: 29

Slide 36

Slide 36 text

tabBarController:didSelectViewController: 30

Slide 37

Slide 37 text

31

Slide 38

Slide 38 text

4) Avoid UIImage’s imageNamed: 32

Slide 39

Slide 39 text

http://www.alexcurylo.com/blog/2009/01/13/ imagenamed-is-evil/ 33

Slide 40

Slide 40 text

@implementation UIImage (AKLoadingExtension) + (UIImage *)newImageFromResource:(NSString *)filename { NSString *imageFile = [[NSString alloc] initWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], filename]; UIImage *image = nil; image = [[UIImage alloc] initWithContentsOfFile:imageFile]; [imageFile release]; return image; } @end 34

Slide 41

Slide 41 text

#import @interface ImageCache : NSObject { @private NSMutableArray *keyArray; NSMutableDictionary *memoryCache; NSFileManager *fileManager; } + (ImageCache *)sharedImageCache; - (UIImage *)imageForKey:(NSString *)key; - (BOOL)hasImageWithKey:(NSString *)key; - (void)storeImage:(UIImage *)image withKey:(NSString *)key; - (BOOL)imageExistsInMemory:(NSString *)key; - (BOOL)imageExistsInDisk:(NSString *)key; - (NSUInteger)countImagesInMemory; - (NSUInteger)countImagesInDisk; - (void)removeImageWithKey:(NSString *)key; - (void)removeAllImages; - (void)removeAllImagesInMemory; - (void)removeOldImages; @end 35

Slide 42

Slide 42 text

- (void)applicationDidReceiveMemoryWarning:(UIApplication *)app { [[ImageCache sharedImageCache] removeAllImagesInMemory]; } 36

Slide 43

Slide 43 text

http://kosmaczewski.net/projects/iphone-image-cache/ 37

Slide 44

Slide 44 text

38

Slide 45

Slide 45 text

5) Build Custom Table Cells and Reuse Them Properly 39

Slide 46

Slide 46 text

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { Item *item = [items objectAtIndex:indexPath.row]; static NSString *identifier = @"ItemCell"; ItemCell *cell = (ItemCell *)[tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[[ItemCell alloc] initWithIdentifier:identifier] autorelease]; } cell.item = item; return cell; } 40

Slide 47

Slide 47 text

41

Slide 48

Slide 48 text

6) Override Setters Properly 42

Slide 49

Slide 49 text

@interface SomeClass { @private NSArray *items; NSString *name; id delegate; } @property (nonatomic, retain) NSArray *items; @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) id delegate; @end 43

Slide 50

Slide 50 text

- (void)setItems:(NSArray *)obj { if (obj == items) { return; // (merci Marco! :) } [items release]; items = nil; items = [obj retain]; if (items != nil) { // create the internal // structure of the cell // if not present, // and change the // widget values } } 44

Slide 51

Slide 51 text

- (void)setName:(NSString *)obj { [name release]; name = nil; // I always copy NSStrings! name = [obj copy]; if (name != nil) { // create the internal // structure of the cell // if not present, // and change the // widget values } } 45

Slide 52

Slide 52 text

- (void)setDelegate:(id)obj { // do not retain! // This is an "assign" property delegate = obj; if (delegate != nil) { // create the internal // structure of the cell // if not present, // and change the // widget values } } 46

Slide 53

Slide 53 text

47

Slide 54

Slide 54 text

7) Beware of Delegation 48

Slide 55

Slide 55 text

@interface SomeClass { @private Widget *widget; } @end 49

Slide 56

Slide 56 text

@implementation SomeClass - (id)init { if (id = [super init]) { widget = [[Widget alloc] init]; widget.delegate = self; } return self; } 50

Slide 57

Slide 57 text

- (void)dealloc { // widget might be retained by someone else! widget.delegate = nil; [widget release]; widget = nil; } #pragma mark - #pragma mark WidgetDelegate methods - (void)widget:(Widget *)obj method:(BOOL)value { // and here something happens... } @end 51

Slide 58

Slide 58 text

52

Slide 59

Slide 59 text

8) Use Instruments 53

Slide 60

Slide 60 text

54

Slide 61

Slide 61 text

55

Slide 62

Slide 62 text

9) Use a Static Analysis Tool 56

Slide 63

Slide 63 text

http://clang.llvm.org/StaticAnalysis.html 57

Slide 64

Slide 64 text

scan-build -k -V xcodebuild -configuration Debug 58

Slide 65

Slide 65 text

59

Slide 66

Slide 66 text

60

Slide 67

Slide 67 text

61

Slide 68

Slide 68 text

62

Slide 69

Slide 69 text

10) Use NSZombieEnabled 63

Slide 70

Slide 70 text

64

Slide 71

Slide 71 text

65

Slide 72

Slide 72 text

66

Slide 73

Slide 73 text

Thanks! 67

Slide 74

Slide 74 text

Questions? 68