Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 1

Optimizing Xcode Builds for Profiling

Recommended resources

Hello! Welcome to the first lesson in our course on mastering Xcode Instruments. Given your extensive experience in app development, we'll dive straight into the practical configurations that form the bedrock of any serious performance analysis.

This lesson focuses on setting up your Xcode project and build schemes correctly. Think of this as calibrating your measurement tools before starting an experiment. An improper setup can lead to misleading data, causing you to chase non-existent issues or miss real ones. By the end of this session, you'll be able to configure optimization levels, debug symbols, and diagnostic tools to ensure the profiling data you gather is both accurate and actionable, setting a solid foundation for the entire course.

Build Schemes and Configurations: The Foundation

First, let's clarify the distinction between build configurations and build schemes, as they are central to this process.

  • A Build Configuration (e.g., Debug, Release) is a set of build settings for a target. The Debug configuration typically includes no code optimization and full debug symbols to make debugging easy. The Release configuration enables optimizations to create a faster, smaller binary suitable for the App Store.
  • A Build Scheme defines which build configuration to use for a specific Xcode action (Build, Run, Test, Profile, Analyze, Archive). A single scheme bundles together targets, a build configuration, and settings for each of these actions.

You can get a formal overview of how schemes work from Apple's documentation.

Customizing the build schemes for a project | Apple Developer Documentation

This document provides the official overview of build schemes.

Please read the Overview section to understand the role of schemes. Then, scroll down to the table under the heading "Configure the runtime environment for built products". Pay close attention to the Build Configuration attribute, noting how it can be set independently for the Run, Test, and Profile actions.

The key takeaway is that you can, and should, have different settings for when you Run your app for day-to-day debugging versus when you Profile it for performance analysis.


Profiling: Use the Release Configuration

A common point of confusion is whether to profile a Debug or Release build. While a Debug build offers easier step-through debugging, its performance characteristics are fundamentally different from the app your users run. It contains extra assertions, logging, and lacks compiler optimizations. Profiling a Debug build can lead you to optimize code paths that aren't bottlenecks in the final product.

Therefore, the professional standard is to profile using the Release build configuration. This ensures you are measuring the performance of the same optimized code that will be shipped to your users.

The following video segment from Essential Developer explains this point perfectly.

How senior iOS devs profile and solve performance issues with Instruments.app | Live Dev Mentoring

Watch this clip for a senior developer's perspective on why using the Release configuration is critical for obtaining meaningful performance data.

Pay close attention to the explanation from why to use Release. The speaker highlights how debug symbols and other features in the Debug configuration can skew the results.


Essential Build Settings for Meaningful Profiling

Now that we've established the "what" (profile the Release configuration), let's look at the "how." You need to ensure your Release configuration has the correct build settings to produce useful profiling data. These are found in your project's Build Settings tab.

1. Optimization Level

This setting controls how aggressively the compiler optimizes your code.

  • For Debug builds: Set to No Optimization [-Onone]. This ensures the compiled code maps directly to your source code, making debugging predictable. The debugger can reliably inspect variables and step through lines of code.
  • For Release builds: Set to Fastest, Smallest [-Os]. This tells the compiler to optimize for both speed and binary size, which is what you want for a shipping app. This is the code you should be measuring.
This image shows the `Optimization Level` setting in Xcode's Build Settings. For profiling, you'll typically use the `Release` setting, which is `Fastest, Smallest [-Os]` by default.

2. Debug Information Format

To make sense of a profiling trace, Instruments needs to map the low-level machine instructions back to your Swift or Objective-C functions and line numbers. This process is called symbolication. It relies on debug information generated during the build.

  • For Debug builds: DWARF is sufficient. The debug symbols are placed directly inside the compiled binary.
  • For Release builds: Set this to DWARF with dSYM File. This option creates a separate debug symbols file (a .dSYM bundle). This is crucial. It allows you to fully symbolicate traces from your Release build without bloating the binary that you ship to users. Without the dSYM, your Instruments trace will show memory addresses instead of function names, rendering it nearly useless.
This image shows the `Debug Information Format` setting. For your `Release` configuration, ensure `DWARF with dSYM File` is selected to enable proper symbolication in Instruments.

For a quick recap of these and other related settings, the following video offers a concise overview.

7 Xcode Settings You MUST Change for iOS Development

This video by Antoine van der Lee quickly runs through several important build settings.

Watch the segment from General build settings. It reinforces the correct settings for Compilation Mode, Optimization Level, and Debug Information Format for both Debug and Release configurations.


Leveraging Scheme Diagnostics

Beyond standard build settings, Xcode schemes offer a powerful Diagnostics tab. This allows you to enable runtime tools that help detect specific, hard-to-find bugs like memory corruption, data races, and threading violations. While these tools add overhead and shouldn't be active during pure performance measurement, they are invaluable for dedicated diagnostic sessions.

You can enable these in the Diagnostics tab of your scheme editor (Product > Scheme > Edit Scheme... > Run > Diagnostics).

Key diagnostics include:

  • Main Thread Checker: Detects AppKit/UIKit/SwiftUI API calls made from a background thread. This is a common source of hangs and visual glitches. It's enabled by default for Debug schemes and is highly recommended.
  • Address Sanitizer (ASan): Detects memory corruption issues like buffer overflows and use-after-free bugs.
  • Thread Sanitizer (TSan): Detects data races in concurrent code. Note that it's incompatible with the Address Sanitizer.
  • Zombie Objects: Helps debug crashes from messaging a deallocated object by keeping deallocated objects alive as "zombies."

For a deeper, practical look at configuring schemes and using these powerful diagnostic tools, the following article is an excellent resource.

Xcode build schemes, diagnosing memory corruption and crashes ...

This blog post provides a detailed, step-by-step guide to setting up distinct Debug and Release schemes and explains what the most important diagnostic tools do.

Skim through the section "How to Configure Build Schemes" to see a practical workflow for creating separate, shared schemes for Debug and Release. Focus on the steps showing how to enable diagnostics. The author walks through setting options in the Diagnostics pane for a Debug scheme and then disabling them for the Release scheme. Finally, read the section "More On Sanitizers in Xcode" for a clear explanation of what the Address Sanitizer, Thread Sanitizer, and Undefined Behavior Sanitizer do. The C++ examples illustrate universal concepts of memory safety and concurrency that are just as relevant in Swift.


Your Turn: Configure Your Project

Now, let's apply this to your project. I recommend creating a dedicated, shared scheme for profiling.

  1. In Xcode, open your project.
  2. Go to Product > Scheme > Manage Schemes....
  3. Select your main app scheme and click the settings cog, then choose Duplicate.
  4. Rename the new scheme to something descriptive, like "[Your App Name] - Profiling". Make sure the Shared checkbox is ticked so it's saved in your project file and available to your team. Close the manager.
  5. Select your new profiling scheme in the scheme selector dropdown.
  6. Go to Product > Scheme > Edit Scheme....
  7. In the left pane, select the Profile action.
  8. On the Info tab, ensure the Build Configuration is set to Release.
  9. Now, go to your project's settings by clicking the project file in the navigator, and select your app target.
  10. Click the Build Settings tab and make sure All and Levels are selected.
  11. Search for Optimization Level. Expand it and confirm that for the Release configuration, it is set to Fastest, Smallest [-Os].
  12. Search for Debug Information Format. Expand it and confirm that Release is set to DWARF with dSYM File.

You now have a dedicated scheme for capturing performance data that accurately reflects your app's behavior in the wild.


Conclusion

In this lesson, we established the critical groundwork for effective profiling. You now understand not just what settings to change, but why they are essential for gathering meaningful data.

Key Takeaways:

  • Profile the Release configuration: This is paramount for measuring performance that reflects the user experience.
  • Use schemes to manage environments: A dedicated, shared scheme for profiling keeps your configurations clean and repeatable.
  • Enable DWARF with dSYM File for Release builds: This ensures your profiling traces can be fully symbolicated, turning cryptic addresses into readable function names.
  • Use Optimization Level [-Os] for Release builds: This ensures you are measuring the code that your users will run.
  • Leverage diagnostics for targeted bug hunts: Tools like the Address Sanitizer and Thread Sanitizer are incredibly powerful for finding specific classes of bugs but should be used in dedicated diagnostic runs, not general performance profiling.

With your Xcode environment now properly configured, you're ready to start digging into your app's performance. In the next lesson, we will explore the os_signpost API. This will allow you to insert custom markers into your code, enabling Instruments to measure the precise duration of specific tasks you care about—a fundamental technique for zeroing in on performance hotspots.