cs193p - lecture 10...•use performance tools to figure out where to invest monday, february 8,...

117
CS193P - Lecture 10 iPhone Application Development Performance 1 Monday, February 8, 2010

Upload: others

Post on 13-Oct-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

CS193P - Lecture 10iPhone Application Development

Performance

1Monday, February 8, 2010

Page 2: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Announcements

2Monday, February 8, 2010

Page 3: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Announcements• Paparazzi 2 is due next Wednesday at 11:59pm

2Monday, February 8, 2010

Page 4: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Announcements• Paparazzi 2 is due next Wednesday at 11:59pm• Friday section tomorrow at 4 PM, Building 260 Room 113

■ Yelp

2Monday, February 8, 2010

Page 5: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

A little more Core Data

3Monday, February 8, 2010

Page 6: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

A little more Core Data• NSFetchedResultsController

■ Interacts with the Core Data database on your behalf■ [fetchedResultsController objectAtIndexPath:] gets at row data■ [fetchedResultsController sections] gets at section data

3Monday, February 8, 2010

Page 7: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

A little more Core Data• NSFetchedResultsController

■ Interacts with the Core Data database on your behalf■ [fetchedResultsController objectAtIndexPath:] gets at row data■ [fetchedResultsController sections] gets at section data

• NSFetchedResultsSectionInfo■ Protocol defining methods that you can call from your

UITableViewDataSource methods■ numberOfSectionsInTableView:■ tableView:numberOfRowsInSection:■ tableView:cellForRowAtIndexPath:

3Monday, February 8, 2010

Page 8: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Today’s Topics• Memory Usage

■ Leaks■ Autorelease■ System warnings

• Concurrency■ Threads■ Operations and queues

• Additional Tips & Tricks

4Monday, February 8, 2010

Page 9: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

iPhone Performance Overview

5Monday, February 8, 2010

Page 10: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

iPhone Performance Overview• iPhone applications must work with...

■ Limited memory■ Slow or unavailable network resources■ Less powerful hardware

5Monday, February 8, 2010

Page 11: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

iPhone Performance Overview• iPhone applications must work with...

■ Limited memory■ Slow or unavailable network resources■ Less powerful hardware

• Write your code with these constraints in mind

5Monday, February 8, 2010

Page 12: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

iPhone Performance Overview• iPhone applications must work with...

■ Limited memory■ Slow or unavailable network resources■ Less powerful hardware

• Write your code with these constraints in mind• Use performance tools to figure out where to invest

5Monday, February 8, 2010

Page 13: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Usage

6Monday, February 8, 2010

Page 14: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory on the iPhone

7Monday, February 8, 2010

Page 15: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory on the iPhone• Starting points for performance

■ Load lazily■ Don’t leak■ Watch your autorelease footprint■ Reuse memory

7Monday, February 8, 2010

Page 16: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory on the iPhone• Starting points for performance

■ Load lazily■ Don’t leak■ Watch your autorelease footprint■ Reuse memory

• System memory warnings are a last resort■ Respond to warnings or be terminated

7Monday, February 8, 2010

Page 17: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading Lazily• Pervasive in Cocoa frameworks• Do only as much work as is required

■ Application launch time!

• Think about where your code really belongs

• Use multiple NIBs for your user interface

8Monday, February 8, 2010

Page 18: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Too Early

9Monday, February 8, 2010

Page 19: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Too Early• What if it’s not needed until much later? Or not at all?- (id)init{

self = [super init];if (self) {

// Too early...myImage = [self readSomeHugeImageFromDisk];

}return self;

}

9Monday, February 8, 2010

Page 20: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Lazily

10Monday, February 8, 2010

Page 21: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Lazily• Wait until someone actually requests it, then create it- (UIImage *)myImage{

if (myImage == nil) {myImage = [self readSomeHugeImageFromDisk];

}}

10Monday, February 8, 2010

Page 22: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Lazily• Wait until someone actually requests it, then create it

• This pattern benefits both memory and launch time

- (UIImage *)myImage{

if (myImage == nil) {myImage = [self readSomeHugeImageFromDisk];

}}

10Monday, February 8, 2010

Page 23: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Lazily• Wait until someone actually requests it, then create it

• This pattern benefits both memory and launch time

• Not always the right move, consider your specific situation

- (UIImage *)myImage{

if (myImage == nil) {myImage = [self readSomeHugeImageFromDisk];

}}

10Monday, February 8, 2010

Page 24: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Loading a Resource Lazily• Wait until someone actually requests it, then create it

• This pattern benefits both memory and launch time

• Not always the right move, consider your specific situation• Notice that above implementation is not thread-safe!

- (UIImage *)myImage{

if (myImage == nil) {myImage = [self readSomeHugeImageFromDisk];

}}

10Monday, February 8, 2010

Page 25: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Plugging Leaks

11Monday, February 8, 2010

Page 26: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Plugging Leaks• Memory leaks are very bad

■ Especially in code that runs often

11Monday, February 8, 2010

Page 27: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Plugging Leaks• Memory leaks are very bad

■ Especially in code that runs often

• Luckily, leaks are easy to find with the right tools

11Monday, February 8, 2010

Page 28: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Method Naming and Object Ownership

12Monday, February 8, 2010

Page 29: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Method Naming and Object Ownership• If a method’s name contains alloc, copy or new,

then it returns a retained object

12Monday, February 8, 2010

Page 30: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Method Naming and Object Ownership• If a method’s name contains alloc, copy or new,

then it returns a retained object• Balance calls to alloc, copy, new or retain with calls to release or

autorelease

12Monday, February 8, 2010

Page 31: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Method Naming and Object Ownership• If a method’s name contains alloc, copy or new,

then it returns a retained object• Balance calls to alloc, copy, new or retain with calls to release or

autorelease■ Early returns can make this very difficult to do!

12Monday, February 8, 2010

Page 32: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Finding Leaks• Use Instruments with the Leaks recorder

13Monday, February 8, 2010

Page 33: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Identifying Leaks in Instruments• Each leak comes with a backtrace• Leaks in system code do exist, but they’re rare

■ If you find one, tell us at http://bugreport.apple.com

• Consider your own application code first

14Monday, February 8, 2010

Page 34: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Caught in the Act

15Monday, February 8, 2010

Page 35: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Demo:Finding Leaks with Instruments

16Monday, February 8, 2010

Page 36: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Autorelease and You

17Monday, February 8, 2010

Page 37: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Autorelease and You• Autorelease simplifies your code

■ Worry less about the scope and lifetime of objects

17Monday, February 8, 2010

Page 38: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Autorelease and You• Autorelease simplifies your code

■ Worry less about the scope and lifetime of objects

• When an autorelease pool is drained, it calls -release on each object

17Monday, February 8, 2010

Page 39: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Autorelease and You• Autorelease simplifies your code

■ Worry less about the scope and lifetime of objects

• When an autorelease pool is drained, it calls -release on each object

• An autorelease pool is created automatically for each iteration of your application’s run loop

17Monday, February 8, 2010

Page 40: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

So What’s the Catch?• What if many objects are autoreleased before the pool pops?• Consider the maximum memory footprint of your application

18Monday, February 8, 2010

Page 41: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

A Crowded Pool...

19Monday, February 8, 2010

Page 42: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reducing Your High-Water Mark

20Monday, February 8, 2010

Page 43: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reducing Your High-Water Mark• When many objects will be autoreleased, create and release

your own pool

20Monday, February 8, 2010

Page 44: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reducing Your High-Water Mark• When many objects will be autoreleased, create and release

your own pool■ Usually not necessary, don’t do this without thinking!

20Monday, February 8, 2010

Page 45: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reducing Your High-Water Mark• When many objects will be autoreleased, create and release

your own pool■ Usually not necessary, don’t do this without thinking!■ Tools can help identify cases where it’s needed

20Monday, February 8, 2010

Page 46: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reducing Your High-Water Mark• When many objects will be autoreleased, create and release

your own pool■ Usually not necessary, don’t do this without thinking!■ Tools can help identify cases where it’s needed■ Loops are the classic case

20Monday, February 8, 2010

Page 47: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Autorelease in a Loop• Remember that many methods return autoreleased objects

21Monday, February 8, 2010

Page 48: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Autorelease in a Loop• Remember that many methods return autoreleased objectsfor (int i = 0; i < someLargeNumber; i++) {

NSString *string = ...;string = [string lowercaseString];string = [string stringByAppendingString:...];NSLog(@“%@”, string);

}

21Monday, February 8, 2010

Page 49: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Creating an Autorelease Pool• One option is to create and release for each iteration

22Monday, February 8, 2010

Page 50: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Creating an Autorelease Pool• One option is to create and release for each iterationfor (int i = 0; i < someLargeNumber; i++) {

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString *string = ...;string = [string lowercaseString];string = [string stringByAppendingString:...];NSLog(@“%@”, string);

[pool release];}

22Monday, February 8, 2010

Page 51: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Outliving the Autorelease Pool• What if some object is needed outside the scope of the pool?

23Monday, February 8, 2010

Page 52: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Outliving the Autorelease Pool• What if some object is needed outside the scope of the pool?NSString *stringToReturn = nil;

for (int i = 0; i < someLargeNumber; i++) {NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

NSString *string = ...;string = [string stringByAppendingString:...];

if ([string someCondition]) {stringToReturn = [string retain];

}

[pool release];if (stringToReturn) break;

}

return [stringToReturn autorelease];

23Monday, February 8, 2010

Page 53: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reducing Use of Autorelease• Another option is to cut down on use of autoreleased objects

■ Not always possible if you’re callling into someone else’s code

• When it makes sense, switch to alloc/init/release• In previous example, perhaps use a single NSMutableString?

24Monday, February 8, 2010

Page 54: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Demo:Measuring Your High-Water Mark

25Monday, February 8, 2010

Page 55: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Object Creation Overhead• Most of the time, creating and deallocating objects is not a

insignificant hit to application performance• In a tight loop, though, it can become a problem...

26Monday, February 8, 2010

Page 56: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Object Creation Overhead• Most of the time, creating and deallocating objects is not a

insignificant hit to application performance• In a tight loop, though, it can become a problem...

for (int i = 0; i < someLargeNumber; i++) {MyObject *object = [[MyObject alloc] initWithValue:...];[object doSomething];[object release];

}

26Monday, February 8, 2010

Page 57: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reusing Objects

27Monday, February 8, 2010

Page 58: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reusing Objects• Update existing objects rather than creating new ones

27Monday, February 8, 2010

Page 59: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reusing Objects• Update existing objects rather than creating new ones• Combine intuition and evidence to decide if it’s necessaryMyObject *myObject = [[MyObject alloc] init];

for (int i = 0; i < someLargeNumber; i++) {myObject.value = ...;[myObject doSomething];

}

[myObject release];

27Monday, February 8, 2010

Page 60: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reusing Objects• Update existing objects rather than creating new ones• Combine intuition and evidence to decide if it’s necessary

• Remember -[UITableView dequeueReusableCellWithIdentifier]

MyObject *myObject = [[MyObject alloc] init];

for (int i = 0; i < someLargeNumber; i++) {myObject.value = ...;[myObject doSomething];

}

[myObject release];

27Monday, February 8, 2010

Page 61: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Warnings

28Monday, February 8, 2010

Page 62: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Warnings• Coexist with system applications

28Monday, February 8, 2010

Page 63: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Warnings• Coexist with system applications• Memory warnings issued when memory runs out

28Monday, February 8, 2010

Page 64: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Warnings• Coexist with system applications• Memory warnings issued when memory runs out

• Respond to memory warnings or face dire consequences!

28Monday, February 8, 2010

Page 65: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Warnings• Coexist with system applications• Memory warnings issued when memory runs out

• Respond to memory warnings or face dire consequences!

28Monday, February 8, 2010

Page 66: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Memory Warnings• Coexist with system applications• Memory warnings issued when memory runs out

• Respond to memory warnings or face dire consequences!

28Monday, February 8, 2010

Page 67: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Responding to Memory Warnings• Every view controller gets -didReceiveMemoryWarning

■ By default, releases the view if it’s not visible■ Release other expensive resources in your subclass

29Monday, February 8, 2010

Page 68: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Responding to Memory Warnings• Every view controller gets -didReceiveMemoryWarning

■ By default, releases the view if it’s not visible■ Release other expensive resources in your subclass

- (void)didReceiveMemoryWarning{

// Always call super[super didReceiveMemoryWarning];

// Release expensive resources[expensiveResource release];expensiveResource = nil;

}

29Monday, February 8, 2010

Page 69: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Responding to Memory Warnings• Every view controller gets -didReceiveMemoryWarning

■ By default, releases the view if it’s not visible■ Release other expensive resources in your subclass

- (void)didReceiveMemoryWarning{

// Always call super[super didReceiveMemoryWarning];

// Release expensive resources[expensiveResource release];expensiveResource = nil;

}

• App Delegate gets -applicationDidReceiveMemoryWarning:

29Monday, February 8, 2010

Page 70: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

What Other Resources Do I Release?• Images• Sounds• Cached data

30Monday, February 8, 2010

Page 71: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

What Other Resources Do I Release?• Images• Sounds• Cached data

30Monday, February 8, 2010

Page 72: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Use SQLite/Core Data for Large Data Sets• Many data formats keep everything in memory• SQLite can work with your data in chunks

31Monday, February 8, 2010

Page 73: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

More on Memory Performance• “Memory Usage Performance Guidelines”

https://developer.apple.com/iphone/library/documentation/Performance/Conceptual/ManagingMemory/

32Monday, February 8, 2010

Page 74: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Concurrency

33Monday, February 8, 2010

Page 75: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Why Concurrency?• With a single thread, long-running operations may interfere

with user interaction• Multiple threads allow you to load resources or perform

computations without locking up your entire application

34Monday, February 8, 2010

Page 76: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Threads on the iPhone• Based on the POSIX threading API

■ /usr/include/pthread.h

• Higher-level wrappers in the Foundation framework

35Monday, February 8, 2010

Page 77: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

NSThread Basics• Run loop automatically instantiated for each thread • Each NSThread needs to create its own autorelease pool• Convenience methods for messaging between threads

36Monday, February 8, 2010

Page 78: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Typical NSThread Use Case

37Monday, February 8, 2010

Page 79: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Typical NSThread Use Case- (void)someAction:(id)sender{

// Fire up a new thread[NSThread detachNewThreadSelector:@selector(doWork:) withTarget:self object:someData];

}

37Monday, February 8, 2010

Page 80: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Typical NSThread Use Case- (void)someAction:(id)sender{

// Fire up a new thread[NSThread detachNewThreadSelector:@selector(doWork:) withTarget:self object:someData];

}

- (void)doWork:(id)someData{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

[someData doLotsOfWork];

// Message back to the main thread[self performSelectorOnMainThread:@selector(allDone:) withObject:[someData result] waitUntilDone:NO];

[pool release];}

37Monday, February 8, 2010

Page 81: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

UIKit and Threads• Unless otherwise noted, UIKit classes are not threadsafe

■ Objects must be created and messaged from the main thread

• You can create a UIImage on a background thread■ But you can’t set it on a UIImageView

38Monday, February 8, 2010

Page 82: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Demo:Threads and Xcode

39Monday, February 8, 2010

Page 83: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Locks• Protect critical sections of code, mediate access to shared data• NSLock and subclasses

40Monday, February 8, 2010

Page 84: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Locks• Protect critical sections of code, mediate access to shared data• NSLock and subclasses

- (void)init{

myLock = [[NSLock alloc] init];}

- (void)someMethod{

[myLock lock];// We only want one thread executing this code at once[myLock unlock]

}

40Monday, February 8, 2010

Page 85: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Conditions

41Monday, February 8, 2010

Page 86: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Conditions• NSCondition is useful for producer/consumer model

41Monday, February 8, 2010

Page 87: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Conditions• NSCondition is useful for producer/consumer model

// On the producer thread- (void)produceData{

[condition lock];

// Produce new datanewDataExists = YES;

[condition signal];[condition unlock];

}

41Monday, February 8, 2010

Page 88: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Conditions• NSCondition is useful for producer/consumer model

// On the producer thread- (void)produceData{

[condition lock];

// Produce new datanewDataExists = YES;

[condition signal];[condition unlock];

}

// On the consumer thread- (void)consumeData{

[condition lock];while(!newDataExists) {

[condition wait];}

// Consume the new datanewDataExists = NO;

[condition unlock];}

41Monday, February 8, 2010

Page 89: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Conditions• NSCondition is useful for producer/consumer model

• Wait is equivalent to: unlock, sleep until signalled, lock

// On the producer thread- (void)produceData{

[condition lock];

// Produce new datanewDataExists = YES;

[condition signal];[condition unlock];

}

// On the consumer thread- (void)consumeData{

[condition lock];while(!newDataExists) {

[condition wait];}

// Consume the new datanewDataExists = NO;

[condition unlock];}

41Monday, February 8, 2010

Page 90: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

The Danger of Locks• Very difficult to get locking right!• All it takes is one poorly behaved client

■ Accessing shared data outside of a lock■ Deadlocks■ Priority inversion

42Monday, February 8, 2010

Page 91: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Threading Pitfalls

43Monday, February 8, 2010

Page 92: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Threading Pitfalls• Subtle, nondeterministic bugs may be introduced

43Monday, February 8, 2010

Page 93: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Threading Pitfalls• Subtle, nondeterministic bugs may be introduced• Code may become more difficult to maintain

43Monday, February 8, 2010

Page 94: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Threading Pitfalls• Subtle, nondeterministic bugs may be introduced• Code may become more difficult to maintain

• In the worst case, more threads can mean slower code

43Monday, February 8, 2010

Page 95: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Alternatives to Threading

44Monday, February 8, 2010

Page 96: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Alternatives to Threading• Asynchronous (nonblocking) functions

■ Specify target/action or delegate for callback■ NSURLConnection has synchronous and asynchronous variants

44Monday, February 8, 2010

Page 97: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Alternatives to Threading• Asynchronous (nonblocking) functions

■ Specify target/action or delegate for callback■ NSURLConnection has synchronous and asynchronous variants

• Timers■ One-shot or recurring■ Specify a callback method■ Managed by the run loop

44Monday, February 8, 2010

Page 98: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Alternatives to Threading• Asynchronous (nonblocking) functions

■ Specify target/action or delegate for callback■ NSURLConnection has synchronous and asynchronous variants

• Timers■ One-shot or recurring■ Specify a callback method■ Managed by the run loop

• Higher level constructs like operations

44Monday, February 8, 2010

Page 99: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

NSOperation• Abstract superclass• Manages thread creation and lifecycle

• Encapsulate a unit of work in an object• Specify priorities and dependencies

45Monday, February 8, 2010

Page 100: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Creating an NSOperation Subclass

46Monday, February 8, 2010

Page 101: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Creating an NSOperation Subclass• Define a custom init method

- (id)initWithSomeObject:(id)someObject{

self = [super init];if (self) {

self.someObject = someObject;}return self;

}

46Monday, February 8, 2010

Page 102: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Creating an NSOperation Subclass• Define a custom init method

• Override -main method to do work

- (id)initWithSomeObject:(id)someObject{

self = [super init];if (self) {

self.someObject = someObject;}return self;

}

- (void)main{

[someObject doLotsOfTimeConsumingWork];}

46Monday, February 8, 2010

Page 103: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

NSOperationQueue• Operations are typically scheduled by adding to a queue• Choose a maximum number of concurrent operations• Queue runs operations based on priority and dependencies

47Monday, February 8, 2010

Page 104: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Using an NSInvocationOperation• Concrete subclass of NSOperation• For lightweight tasks where creating a subclass is overkill

48Monday, February 8, 2010

Page 105: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Using an NSInvocationOperation• Concrete subclass of NSOperation• For lightweight tasks where creating a subclass is overkill

- (void)someAction:(id)sender{

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doWork:) object:someObject];

[queue addObject:operation];

[operation release];}

48Monday, February 8, 2010

Page 106: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Demo:Threaded Flickr Loading

49Monday, February 8, 2010

Page 107: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

More on Concurrent Programming• “Threading Programming Guide”

https://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/Multithreading

50Monday, February 8, 2010

Page 108: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Additional Tips & Tricks

51Monday, February 8, 2010

Page 109: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Drawing Performance• Avoid transparency when possible

■ Opaque views are much faster to draw than transparent views■ Especially important when scrolling

• Don’t call -drawRect: yourself• Use -setNeedsDisplayInRect: instead of -setNeedsDisplay• Use CoreAnimation Instrument

52Monday, February 8, 2010

Page 110: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reuse Table View Cells• UITableView provides mechanism for reusing table view cells

53Monday, February 8, 2010

Page 111: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reuse Table View Cells• UITableView provides mechanism for reusing table view cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

return cell;}

53Monday, February 8, 2010

Page 112: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reuse Table View Cells• UITableView provides mechanism for reusing table view cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

return cell;}

// Ask the table view if it has a cell we can reuseUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

53Monday, February 8, 2010

Page 113: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Reuse Table View Cells• UITableView provides mechanism for reusing table view cells

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

return cell;}

// Ask the table view if it has a cell we can reuseUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

if (!cell) { // If not, create one with our identifiercell = [[UITableViewCell alloc] initWithFrame:CGRectZero identifier:MyIdentifier];[cell autorelease];

}

53Monday, February 8, 2010

Page 114: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Get notified • Don’t continuously poll!

■ Unless you must, which is rare

• Hurts both responsiveness and battery life• Look in the documentation for a notification, delegate callback

or other asynchronous API

54Monday, February 8, 2010

Page 115: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Take Samples• Instrument that lets you monitor CPU usage• Backtrace taken every fraction of a second• Higher samples = better candidates for optimization

55Monday, February 8, 2010

Page 116: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Recap• Performance is an art and a science

■ Combine tools & concrete data with intuition & best practices

• Don’t waste memory• Concurrency is tricky, abstract it if possible• Drawing is expensive, avoid unnecessary work

56Monday, February 8, 2010

Page 117: CS193P - Lecture 10...•Use performance tools to figure out where to invest Monday, February 8, 2010 5 Memory Usage Monday, February 8, 2010 6 Memory on the iPhone Monday, February

Questions?

57Monday, February 8, 2010