Ole Begemann

iOS Development

iOS 5 Tech Talk: Michael Jurewitz on Performance Measurement

Update January 27, 2012: Apple recently posted some new videos to its developer site. One of them, titled Optimizing App Performance with Instruments is presented by Michael and follows the contents of his Tech Talk presentation pretty closely. It’s worth watching, especially for all the Instruments demos he is doing. See my blog post about the new videos on where to find them.

The last Tech Talk session I am going to write about here is Michael Jurewitz’s talk titled Your iOS App Performance Hitlist:

The best iOS apps are not only beautiful and well designed, they also launch quickly, present a highly responsive interface, and use memory efficiently. Master the techniques to diagnose and fix common performance problems before your customers see them. Learn the performance hit-list you should apply to every one of your apps before it goes out the door.

In this session, Michael gave us a hands-on introduction on how to identify performance problems with Instruments based on three common problem areas. Overall, I liked this talk the best. I had seen sessions on Instruments before but sometimes you have to see things demo’ed two or three times until the learning kicks in. And I definitely feel that I learned a few new tricks here.

General Tips

Guesswork is almost never the right approach to performance optimization. It’s hard to optimize problems that you haven’t measured so the latter must always be your first priority. Apple provides Instruments to quantify different aspects of your app.

Another important factor is to test your app under realistic conditions. If you just test your app with a fresh developer build that contains little or no test data, it is very likely that you won’t catch many serious performance and memory problems that will later plague your users. You should always make sure to use realistically-sized data sets for your tests and make sure to also test the worst case. After all, users that manage large amounts of data with your app are often your best customers and you don’t want to offend them.

For the remainder of the talk, Michael focused on the following three performance characteristics:

  1. Launch Quickly/Avoid Blocking Work
  2. Minimize Memory Usage
  3. Draw Efficiently

In his words, if you optimize these, you’re probably more than half way there on the road to a well-performing app.

1. Launch Quickly

Many iOS apps are used for just a few seconds at a time and (even in times of multitasking) relaunched very often. Nothing is more frustrating to the user than having to wait for several seconds each time an app is launched until she can get started interacting with it.

Your goal should be for your app to be ready for user input as quickly as possible. If that means that some UI components (e.g., images or web content) have not yet loaded completely, no problem. It is much better to already display a content-less UI than forcing the user to wait until everything is 100% complete.

To minimize your launch time, try to identify the minimum possible stuff your app must do in all methods that can get invoked before your app’s UI is ready:

Your main focus should probably be on application:didFinishLaunchingWithOptions: and viewDidLoad.

Common problems you should look out for are synchronous network calls and the synchronous loading or parsing of large data sets such as huge data files or images. dispatch_async() should be your biggest friend here. You should never block the main thread with a synchronous operation that could potentially take a long time (more than a few tenths of a seconds or so) to complete, much less so during your app’s launch phase. Try to push these operations onto background queues from where you notify the main queue once the operation has finished. Until then, display a progress indicator in your UI.

Instruments: Time Profiler

The Time Profiler instrument in Instruments

To identify problems of this kind with Instruments, use the Time Profiler instrument. While your app runs, Instruments will continuously sample the process and record the code’s current position in the call stack, thereby identifying the methods your app spends the most time in.

When you have collected enough data, stop the recording. Your goal is now to find the most time-consuming methods in the Call Tree and try to optimize them one by one. Here are a few of Michael’s tips:

2. Minimize Memory Usage

Optimizing memory usage has been with iOS developers from the beginning. Because iOS devices have no swap file, there is a hard limit on the memory the system can use. If apps require more memory than is available, the OS has no other choice than to kill one or more apps, starting with (but not limited to) those that are currently in the background. There is another incentive to use as little memory as we can: the less memory your app uses, the longer the OS will keep it alive in the background, which means a better experience for your users.

To notify you of memory pressure, the OS can send a memory warning to the active app. Michael noted that he got the impression that, with the introduction of multitasking in iOS 4, many apps got a bit lazy when it comes to dealing with memory warnings. After all, if the frontmost app did not free any memory in reaction to a memory warning, the OS still wouldn’t kill it as there were usually several apps frozen in the background that could be killed first. And this is indeed the behavior in iOS 4. Because of these unintended consequences, Michael stressed that Apple has made changes in iOS 5: In iOS 5, if your app is the frontmost app and we send you a memory warning, it means that your head is already on the chopping board. So you better do something about it and react to those memory warnings!

Instruments: Allocations + Leaks + VM Tracker + Activity Monitor

Michael has constructed his own combination of instruments to measure an app’s memory usage by combining the Allocations, Leaks, VM Tracker and Activty Monitor instruments. It’s easy to do this yourself by creating a blank instrument and dragging the four mentioned instruments from the Library inspector pane into the main window. You can then save this instrument as a template for reuse.

Michael Jurewitz's collection of instruments for memory usage measurement

The Activity Monitor can be used to compare your app’s resource usage to other apps that are currently in the background. For example, sort the app list by Real Memory and select the Track inspection head option. When you now drag the inspection head across the timeline you just recorded, you will see your own app rise (or fall) among the other backgrounded apps depending on how much memory you use.

Activity Monitor options

The Allocations instruments can be misleading because it does not show you all of the memory your app is using (only the malloc-type allocations). Still, you should watch out for the following patterns:

The VM Tracker instruments is useful because it can show you the real amount of memory your app uses. The two columns, Resident Size and Dirty Size, show the amounts of memory that can be mapped out to disk or not be reclaimed, respectively. According to Michael, the amount shown in the *Dirty* row and Resident Size column is the most accurate view of the memory your app is using at the point that is currently selected in the timeline. Don’t be stunned if this is radically different from the value that is indicated by the Allocations instrument.

3. Draw Efficiently/Inefficient Drawing

The Core Animation instrument can help you identify performance issues in your drawing code. By color-coding the layers in your app’s UI, you get a quick at-a-glance overview how you are doing.

Core Animation instrument options

Even if your app’s UI just consists of plain UIKit controls and you don’t actually draw any content yourself, it is possible to make mistakes that can significantly affect performance, especially during scrolling. According to Michael, you should especially watch out for:

Also have a look at Cyril Godefroy’s blog post about the same talk at the iOS Tech Talk in London.

Update November 18, 2011: Benjamin Godard also blogged about this last part of the session. Look at the screenshots in Benjamin’s post to see what to watch out for.