Using Instruments to Find Hangs in Your App


If you find your app becomes unresponsive at times, it can be frustrating to find the cause. Instruments includes a Hangs instrument that reports hangs to help you find and fix hangs. This article shows you how to use the Hangs and Time Profiler instruments to find hangs and find the code causing the hangs.

Profiling Your App

In Xcode press Cmd-I to build and profile your app with Instruments. When Instruments launches, it will ask you to choose a template for profiling. Select the Time Profiler template and click the Choose button. A configuration window will open.

You can choose the severity level of hangs that Instruments reports. Selecting the Include All Potential Interaction Delays will show the most hangs.

Click the Record button at the top of the window to start profiling your app. Run your app. Click the Stop button when you are finished.

Reading the Results

The Hangs instrument initially shows a summary table of the hangs it found.

There is a row for each hang type. Hangs are the most severe, followed by microhangs, brief unresponsiveness, and potential interaction delays.

For each hang type Instruments tells you the number of hangs along with the minimum, maximum, and average durations.

Pressing Cmd-2 will show a table listing each hang.

Finding the Code Causing the Hang

If you find hangs in your app, you want to find the code causing the hang. The Hangs instrument won't help, but the Time Profiler instrument will.

Start by creating an inspection range to focus on a hang. Above the statistics is a list of instruments. Select the Hangs instrument. There will be a graph that looks similar to the following image:

There is a bar in the graph for each hang. Click inside the graph at the start of a hang. Drag to the end of the hang to set the inspection range.

After setting the inspection range, select the Time Profiler instrument from the list of instruments. To see your code in the call tree view, take the following steps:

  1. Click the Call Tree button at the bottom of the window.
  2. Select the Hide System Libraries checkbox.
  3. Select the Invert Call Tree checkbox.

Look for any functions that have a high Self Weight value. Double-click a function to show the source code for that function and see the lines of code where your app spends the most time. Read the following article to learn more about the Time Profiler instrument:


Newsletter Archive

You can read previous issues of the newsletter using the following button:

Swift Dev Journal

Subscribe and get exclusive articles on Swift development, a free guide on moving from tutorials to making your first app, notices of sales on books, and anything I decide to add in the future.

Read more from Swift Dev Journal

Saving App Data in Property List Files Your app needs to save data. There's too much data to use User Defaults. Using SwiftData or Core Data would be overkill. One solution is to save the data in property list files. This article shows you how to save your app's data in property list files. Use the PropertyListEncoder Class if you can If the data you want to save conforms to the Codable protocol, use the PropertyListEncoder class to save the data. Encoding data with PropertyListEncoder...

Working with Lists in Multiplatform SwiftUI Apps One of SwiftUI's best features is you can use it to make apps that run on both iOS and Mac. Almost every SwiftUI article you find online is about iOS development, but most of the material also applies to Mac as well. Lists are one area of SwiftUI where there are large differences between iOS and Mac. If you read an article about lists and try to use the code in a Mac app, you'll run into problems. This article provides guidance on writing list...

Getting Started with Document-based SwiftUI Apps Document-based apps let people create documents they can share with others. Examples of document-based apps are text editors, spreadsheets and video editors. Learn the basics of making document-based SwiftUI apps in this article. Creating a Project in Xcode The iOS, Mac, and Multiplatform project categories have a Document App project template. Select that template and click the Next button to create a document app. Multiplatform document apps...