Instruments Quick Start

Instruments is a powerful tool you can use to collect data about the performance and behavior of a process on the system, and track that data over time. Unlike most other performance and debugging tools, Instruments lets you gather widely disparate types of data and view them side by side. In this way, you can identify trends that might otherwise be hard to spot with other tools. For example, your app may exhibit large memory growth that is caused by multiple network connections being opened. Using the Allocations and Connections instruments together, you can identify the connections that are not closing and resulting in the rapid memory growth.

The Instruments tool uses individual data collection modules known as instruments to collect data about a process over time. Each instrument collects and displays a different type of information, such as file access, memory use, and so forth. Instruments includes a library of standard instruments, which you can configure and use to examine various aspects of your code. You can also build your own custom instruments that use DTrace to gather other kinds of data.

All work in Instruments is done in a trace document, which contains a set of instruments and the data they collected. You can save a trace document to preserve the trace data you have gathered and open it again later to view that data.

Launching Instruments

Instruments is contained within the Xcode toolset, which can be installed through the App Store. After you have installed Xcode, Instruments can be launched in one of three ways.

bullet
To run Instruments from Xcode
  1. Open Xcode.

  2. Choose Xcode > Open Developer Tool > Instruments.

    ../Art/xcode_instruments_menu_2x.png

Incorporating Instruments into your workflow from the beginning of the app development process can save you time later by helping you find issues early in the development cycle. The ability to initiate Instruments directly from within Xcode allows you to gather information about your app regularly, such as every time you build it.

bullet
To run Instruments while building your code
  1. Select Edit Scheme in the scheme menu in the Xcode toolbar.

    ../Art/xcode_edit_scheme_menu_2x.png

     

  2. Click Profile in the scheme editor sidebar.

    ../Art/xcode_scheme_editor_profile_area_2x.png
  3. Choose an instrument template from the Instrument pop-up menu.

    The default choice is “Ask on Launch,” which causes Instruments to display its template chooser dialog when it starts up. You can, of course, select a specific instrument profiling template here instead, if you prefer.

  4. Click Close.

  5. Hold down the Run button in the Xcode toolbar and choose Profile or choose Profile from the Product menu whenever you want to build your app and run it in Instruments.

The third way to open Instruments is from the Dock. To do this, you need to first open Instruments using one of the methods listed above and configure it to stay in the Dock.

bullet
To run Instruments from the Dock
  1. Launch Instruments from Xcode using one of the two methods above.

  2. Control-click the Instruments icon in the Dock.

  3. Click Options > Keep in Dock.

    ../Art/instruments_dock_menu_2x.png

Gathering Your First Data

Even though each instrument is different, there is one general workflow when collecting information from your app. You:

  1. Open Instruments.

  2. Choose a target device and app.

  3. Choose a trace template.

  4. Collect information from your app.

  5. Examine the collected information.

Choose a Trace Template

When Instruments first starts up, it provides you with a list of trace templates. These templates contain collections of instruments that are commonly used together to provide you with a specific set of information. Optionally, you may specify a target device and app at the top of the template selection window. In Figure 1-1, the Time Profiler trace template is selected and an app named Sketch is selected as the target app.

Figure 1-1  The Instruments trace template selection window

Choose Your Target

If you didn’t launch Instruments from the Xcode Product menu or Run button, or select a target from the template selection window, then you need to specify a target for the instruments in the trace document to profile. A selected target may be an installed app, such as Chess in Figure 1-2, or a process that is already running.

Figure 1-2  Selecting the Chess app as the target

Collect and Examine the Data

Once you have created a trace template and chosen a target for your app, it’s time to collect some data. You can adjust the information that’s collected by an instrument via the Record Settings area in the Inspector sidebar. Click Record to start collecting data. When you have enough, click Stop. Figure 1-3 shows data collected from the Sketch app using the Time Profiler template.

After collecting your data, you can examine it in the detail pane, as well as the Extended Detail area in the Inspector sidebar. Select a row in the detail pane to bring up extra information about the row in the Extended Detail area.

Figure 1-3  Collecting information with the Time Profiler trace template