10-Performance

10-Performance - CS193P - Lecture 10 iPhone Application...

Info iconThis preview shows pages 1–18. Sign up to view the full content.

View Full Document Right Arrow Icon
CS193P - Lecture 10 iPhone Application Development Performance
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Announcements Presence 2 is due tomorrow (May 5) at 11:59pm Presence 3 assignment will be released tomorrow Final project proposals due on Monday (May 11) See class website for more details
Background image of page 2
Today’s Topics Memory Usage Leaks Autorelease System warnings Concurrency Threads Operations and queues Additional Tips & Tricks
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 4
Memory Usage
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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; }
Background image of page 8
Loading a Resource Lazily Wait until someone actually requests it, then create it Ends up benefiting 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]; } }
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Plugging Leaks Memory leaks are very bad Especially in code that runs often Luckily, leaks are easy to find with the right tools
Background image of page 10
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!
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Finding Leaks Use Instruments with the Leaks recorder
Background image of page 12
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
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Caught in the Act
Background image of page 14
Demo: Finding Leaks with Instruments
Background image of page 15

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Autorelease and You Autorelease simplifies your code Worry less about the scope and lifetime of objects When an autorelease pool pops, it calls -release on each object An autorelease pool is created automatically for each iteration of your application’s run loop
Background image of page 16
What if many objects are autoreleased before the pool pops?
Background image of page 17

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 18
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 55

10-Performance - CS193P - Lecture 10 iPhone Application...

This preview shows document pages 1 - 18. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online