Optimizing your application’s performance is an important phase of the development process, more so in iPhone OS–based devices, which, although powerful computing devices, do not have the memory or CPU power that desktop or portable computers possess. You also have to pay attention to your application’s battery use, as it directly impacts your customer’s battery-life experience.
This chapter describes Instruments and Shark, the tools you use to measure and tune your application’s performance.
The Instruments application lets you gather a variety of application performance metrics, such as memory and network use. You can gather data from iPhone applications running in iPhone Simulator or on your development devices.
It is important that your iPhone applications use the resources of iPhone OS–based devices as efficiently as possible to provide a satisfactory experience for you customers. For example, your application should not use resources in a way that makes the application feel sluggish to users or drains their batteries too quickly. Applications that use too much memory run slowly. Applications that rely on the network for their operation must use it as sparingly as possible because powering up the radios for network communications is a significant drag on the battery.
The Instruments application provides an advanced data gathering interface that lets you know exactly how your application uses resources, such as the CPU, memory, file system, and so on.
Instruments uses software-based data-gathering tools, known as instruments, to collect performance data. An instrument collects a specific type of data, such as network activity or memory usage. You find which instruments are available for iPhone OS in the Instruments Library.
Although most iPhone applications run in iPhone Simulator and you can test most design decisions there, the simulator does not emulate a device, in particular it doesn’t attempt to replicate a device’s performance characteristics such as CPU speed or memory throughput. To effectively measure your application’s performance as users may use it on their devices, you must use an iPhone or iPod touch. That’s because only on a device can you get an accurate representation of the runtime environment (in terms of processor speed, memory limitations, specialized hardware, and the like).
These are some limitations of iPhone Simulator:
Maximum of two fingers. If your application’s user interface can respond to touch events involving more than two fingers, you can test that capability only on devices.
Accelerometer. Although you can access your computer’s accelerometer (if it has one) through the UIKit framework, its readings differ from the accelerometer readings on a device. This discrepancy stems largely from the different positioning of the screen in relation to the rest of the hardware between computers and iPhone OS–based devices.
OpenGL ES. OpenGL ES uses renderers on devices that are slightly different from those it uses in iPhone Simulator. For this reason, a scene on the simulator and the same scene on a device may not be identical at the pixel level. See “Drawing with OpenGL ES” in iPhone Application Programming Guide for details.
To measure your application’s performance on a device:
Build and run your application on the device as described in “Running Applications.”
Launch Instruments.
The Instruments application is located at <Xcode>/Applications. (<Xcode> refers to the installation location of the development tools.)
Choose a template, such as Activity Monitor, to create the trace document.
A trace document contains one or more instruments that collect data about a process.
From the Default Target pop-up menu in the toolbar, select the iPhone OS–based device containing the application from which you want to collect performance data.
Add or remove instruments from the trace document to collect the desired data.
Use the Default Target pop-up menu, to launch or attach to the target application.
Click Record to start collecting data and use your application, exercising the areas you want to examine.
To learn more about measuring and analyzing application performance, see Instruments User Guide. This document provides general information about using Instruments.
To complement the performance data Instruments collects, the Shark application lets you view system-level events, such as system calls, thread-scheduling decisions, interrupts, and virtual memory faults. You can see how your code’s threads interact with each other and how your application interacts with iPhone OS.
When performance problems in your code are more related to the interaction between your code, iPhone OS, and the hardware architecture of the device, you can use Shark to get information about those interactions and find performance bottlenecks.
For information about using Shark with your iPhone applications, see Shark User Guide.
Last updated: 2009-08-06