Find the Code Causing Long SwiftUI Updates with Instruments


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, the SwiftUI instrument shows a graph of long updates. The graph looks similar to the following screenshot:

Long View Body Updates Graph

Long updates appear in the graph as red and orange bars. If you find a long update, right-click on the bar in the graph and choose Set Inspection Range to focus on a single long update. Focusing on a single update makes it easier to see the code that runs during a long update.

Seeing the Code that Runs During the Update

After focusing on a long update, switch to the Time Profiler instrument. The Time Profiler instrument shows the functions that ran during the update. To focus on your app's code in the call tree, click the Call Tree button at the bottom of the window and select the Hide System Libraries checkbox.

If the call tree shows only a function named main, option-click the disclosure triangle next to main to expand the call tree and show your functions.

Double-click a function in the call tree to show the source code for the function. The source viewer shows the lines of code where your app spent the most time during the long update.

Avoid double-clicking any functions with an INLINED label next to them. The source viewer doesn't show code for inlined functions.


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

When you profile your app with the Allocations instrument, you may want to find the largest memory allocations your app makes. Take the following steps to find the largest memory allocations: Press Cmd-3 to open the allocations list. Click the Size column heading to sort the allocations by size. Choose All Heap Allocations from the Allocation Type menu in the bottom bar to hide virtual memory allocations. Your code doesn’t directly make virtual memory allocations. The Allocation Type menu is...

A common problem people run into when they start profiling their apps with Instruments is finding the code that is causing problems. Many instruments initially show general statistics instead of statistics about the code you wrote. For example the Allocations instrument initially shows the number of memory allocations and amount of allocated memory for hundreds of memory categories. If your app allocates a lot of memory, you want to find the code that allocates high amounts of memory. How do...

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...