A: caton
What is Hang?
Imagine a situation where a user interacts with your APP, but the APP doesn’t respond to the user’s actions in a timely manner.
This experience can be described as delayed, slow, and stuck. In Apple development, we call this unresponsive performance Hang.
(Reminder: This is from WWDC21: Understand and eliminate files from your app) and Diagnose Power and Performance regressions in your app
If you want to know more about WWDC2021, you can read my article below. Welcome to communicate and correct
This article takes you through WWDC21 core (new) technology points
The ultimate APP performance survival guide
Detect and diagnose memory problems
Understand the main runloop
User interaction takes place in the Runloop of the main thread. When the user interacts with the APP, the main thread receives this event, processes it, and updates the UI.
If processing events takes a long time, there is a delay between the user interaction and the UI update.
What usually causes Hang?
-
The main thread is Busy
For example, overloading resources. The current page only needs to show the first four images, but all of them are loaded at once.
For example, a task unrelated to the main thread was performed.
Another example is using suboptimal apis. There are two ways to draw rounded corners like a picture:
-
Method one:
-
Method 2:
Comparing these two methods, method one is CPU intensive and consumes a lot of memory. Method two uses GPU to draw, faster and more timely.
-
-
Main thread Blocked
Operations that may cause the main thread to block, such as:
-
Synchronize the request network and wait for the data to return
-
The behavior of accessing system resources such as file IO
-
Database operation
-
The lock
-
How to diagnose Hang
-
Use Instruments to troubleshoot offline problems
-
Time Profile
-
-
With MetricKit, check the condition on the line
If governance Hang?
Core goal: Reduce work on the main thread
-
Optimize tasks that must be performed in the main thread
-
Use the cache
For example, while other threads save and update the cache, the main thread only reads
-
The use of the Notification
Notifications are sent in the main thread, and other threads receive the notifications and process the data asynchronously
-
-
Remove unnecessary tasks from the main thread
In general, tasks that provide critical information to the UI should be performed on the main thread. In addition, all views and View controllers must be created, modified, and destroyed on the main thread.
Computations can be performed in other threads and then in the main UI step. Some non-critical maintenance, non-time sensitive tasks should be executed asynchronously in other threads.
For example, network requests
-
Process tasks asynchronously using GCD
-
The Instruments tool contains a wealth of data, allowing you to view specific performance data for each model and category, but also allowing you to be confused about how to optimize.
The Regressions module was added to Instruments in Xcode 13, which highlights performance issues that need to be addressed first to simplify workflows.
2: Regressions
Understand Regression in Instrument
When an APP deteriorates in performance or power relative to a recent version, this is called Regression.
For example, after launching a new version, the APP startup time increases.
The startup time of the latest version is compared to the average startup time of recent versions, and if the latest version takes longer, it is marked as regression.
The left-hand column of Regression summarizes which metrics have been regressed and how much they have deteriorated compared to previous versions.
-
Disk Writes
Disk is a restricted resource like memory and CPU, and failing to check for disk writes can wear out and damage underlying devices. It can also cause Hang (a user action exceeding 250ms is not responding), UI delays, and even shortened battery life.
- Insights
Organzier has added a new area called the Insights area to provide some performance tuning suggestions
-
File activities
File Activities in Instruments can also be used to debug stored problems.
The relevant data
System Trace in depth
What’s new in MetricKit
Diagnose power and performance regressions in your app
Improving battery life and performance
Modernizing Grand Central Dispatch
Why is my app getting killed?
Triage TestFlight crashes with Xcode Organizer
Identify trends with the Power and Performace API