|
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 AppIn 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 ResultsThe 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 HangIf 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:
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 ArchiveYou can read previous issues of the newsletter using the following button:
|
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.
TextEditingReference collection I am the new maintainer of TextEditingReference, a collection of open-source libraries for working with text in iOS and Mac apps. The libraries in the collection include the following: Markdown editing views Rich text editing views Text views with syntax highlighting Syntax highlighting libraries TextKit utility packages If you develop apps that involve editing more than one line of text, you may find a library or two in the TextEditingReference collection to...
Swift Concurrency Learning Resources One of the biggest pain points experienced Swift developers have is dealing with Swift concurrency, especially the changes in Swift 6. Many developers are sticking with Swift 5 to avoid the pain of migrating their code to Swift 6. If you are having trouble learning about Swift concurrency, I put together a list of resources that should help. F**king Approachable Swift Concurrency F**king Approachable Swift Concurrency is a good starting point to learn...
Find the Code Causing Long SwiftUI Updates with Instruments Instruments comes with a SwiftUI instrument to find performance problems in your SwiftUI apps. This article shows how to use Instruments to find the code that causes long SwiftUI view updates. If you have never used Instruments, read the following article to learn how to profile your app with the SwiftUI instrument: Find the SwiftUI Views that Update the Most Using Instruments Finding Long Updates When you finish profiling your app,...