logo
On this page
2023-12-21

In iOS application development, when stuttering, device heating, excessive memory usage and other problems occur, how should they be analyzed?

Products / Plugins:Video Call / Audio Call / Live streaming
Platform / Framework:iOS

During iOS application development, performance often fails to meet requirements, leading to problems such as stuttering, heating, excessive memory usage, and crashes. These can be analyzed and optimized through the following methods:

  1. Use the Instruments tool in Xcode to perform performance detection and analysis;
  2. Through analysis, find areas with large performance overhead such as resource leaks, memory leaks, and energy consumption, and then optimize them.
Note

In audio and video scenarios, performance analysis and optimization are particularly important. Since audio and video rendering, encoding and other operations have large overhead, especially in certain overseas business scenarios (for example, users have general hardware conditions), if the application's performance is poor, the impact is more obvious, and the user experience will be greatly reduced.

Introduction to Instruments

Instruments is part of the Xcode toolset and is a powerful and flexible performance analysis and testing tool that can help developers analyze iOS, watchOS, tvOS, and macOS applications, processes, and devices to better understand and optimize their behavior and performance. By integrating tools into the workflow from the early stages of application development, developers can discover problems early in the development cycle, saving time later.

In Instruments, you can use specialized "tools" to track different aspects of applications, processes, and devices over time. By analyzing the collected data, detailed results are presented.

Instruments can broadly collect different types of data and repeatedly troubleshoot, making it easier to identify problems that might be overlooked. For example, an application might experience significant memory growth due to multiple open network connections. Through "Allocations" and "Network", developers can identify unclosed connections and free up memory usage.

Effective use of tools can:

  • Check the behavior of one or more applications or processes.
  • Check specific features of the device, such as Wi-Fi and Bluetooth.
  • Perform analysis on a simulator or physical device.
  • Track issues in source code.
  • Perform performance analysis on applications.
  • Find memory issues in applications, such as leaks, abandoned memory, and zombie objects.
  • Determine ways to optimize applications for greater efficiency.
  • Perform general system-level troubleshooting.
  • Save tool configuration as a template for next use.

Although Instruments is embedded in Xcode's toolset and used together with Xcode, Instruments is also a standalone application that can be used separately as needed.

Using Instruments

Launch Instruments

In Xcode, select "Xcode > Open Developer Tool > Instruments".

Create a Trace File

After launching Instruments, in the "Choose a profilling template for:" dialog box that appears, select a target device and target process (App), then select the tool you need to use (the figure below uses Allocations as an example), and finally click "Choose" to complete the creation.

  • Toolbar

    • Start/Stop: Start or stop monitoring and analyzing the App. When you click Start, the App will launch, and Instruments will monitor the App's operation according to the configuration. The timeline panel and details panel will update in real-time. When you click Stop, the App will exit, and Instruments will stop working.
    • Pause: Pause monitoring and analyzing the App. When you click Pause, the App will pause and freeze, and Instruments will pause monitoring the App's operation.
    • Target Device: A list of target devices, including iPhone, iPad, Mac, Apple Watch, etc. iOS devices can be connected via wired or wireless connection.
    • Target Process: Analyzable processes on the target device, such as Apps on iPhone.
    • Activity Observer: Analysis activities that already exist in the current Trace file. You can switch between them by clicking the left and right buttons, and the time used for each analysis activity is displayed.
    • Add Analysis Tool: Show or hide all Instruments tool lists. You can select a tool from the list and add it to the current analysis activity.
  • Timeline Panel

    The timeline panel updates in real-time as it monitors the App over time, collecting data and displaying it in chart form. The chart can be scaled, scrolled left and right, click on a specific time point to observe nearby data, insert marker icons at a certain time point, mark as highlight, define a specific time area (highlight), and select one or more tools in the Instruments list to pin them, fixing the tool display at the bottom of the timeline panel.

  • Data Display Area

    The data display area updates in real-time as it monitors the App over time, collecting data and displaying it in table form. The displayed data content corresponds to the "Instruments tool" selected in the timeline panel (highlighted). The data content details are related to the time selection in the timeline panel, such as a specific time point or time area specified in the timeline panel, and change by default as time progresses.

    • Navigation Bar: The upper left corner can change the data organization type of the data display area, as shown in the figure, the current is Allocations List, which displays all data in a flat list form
    • Data Collection Display Area: Displays data content details.
    • Filter and Configuration Toolbar: You can filter the data you want to display in the data display area by entering keywords in the input box; you can adjust the way data is displayed in the data display area by selecting configuration button options.

Translate Trace Stack

After Instruments analysis is complete, the stack information analyzed in the Trace file needs to be translated to help confirm problematic functions or issues to be optimized.

Click "File > Symbols".

Generally speaking, for common applications, symbol tables (Symbols) can be divided into two types:

  • Can be obtained directly. For example, symbol tables generated by the application itself during the compilation process.

    How to generate symbol tables for your App:

    1. Select "Build Settings > Build Options > Debug Information Format" and confirm whether it is DWARF with dSYM File.
    2. After compiling the project, a corresponding ".app" file will be generated in the Products folder. Right-click on the file, select "show in Finder", and in the same directory, find the corresponding dSYM file, which is the symbol table file.
  • For third-party libraries, such as ZegoExpressEngine, the symbol table is maintained by ZEGO, so developers cannot directly obtain it.

During the analysis process, if you find that the anomaly is related to ZEGO, you can save the Trace file and contact ZEGOCLOUD Technical Support for analysis.

Save Trace File

Save the analyzed Trace file locally and contact ZEGOCLOUD Technical Support to locate the problem.

Click "File > Save/Save As...".

CPU Consumption

When analyzing CPU consumption during App runtime, generally use Time Profiler.

CPU consumption directly leads to device heating, stuttering, power consumption and other problems. Analyzing CPU consumption can confirm whether each thread in the App is working as expected.

For example, using the ZegoExpressEngine official website sample Demo, run the Demo, click Publishing, click the Start Publishing button, click the Stop Publishing button, and the CPU consumption is as shown below:

Click the Threads filter option above to view the CPU usage of all threads in the sample Demo.

From the figure below, you can see that during the selected time period, the main thread has a higher CPU consumption; expand the call stack details below to see that it is a callback in the Demo that causes the CPU consumption to increase.

Then click on the call stack interface details in the lower right corner to jump to the corresponding code location in the Demo.

By viewing the code details, you can find that it is because the SDK called back the stream publishing quality at this time, and the Demo updated the content of the UILabel. The UI refresh caused the CPU consumption to increase.

When you discover that your App has abnormal CPU consumption, phone heating, serious stuttering and other problems, you can analyze them through the above methods. If you find that the ZegoExpressEngine SDK causes increased CPU consumption, you can save the Trace file and contact ZEGOCLOUD Technical Support for further analysis.

Memory Growth

When analyzing memory growth during App runtime, generally use Allocations.

For example, using the ZegoExpressEngine official website sample demo, run the demo, click Publishing, click the Start Publishing button, click the Stop Publishing button, return to the home list page, and the memory changes are as shown below:

Since the Demo's symbol table has already been imported earlier, you can see the detailed call stack of the Demo; at the same time, since the ZegoExpressEngine SDK's symbol table has not been imported, the corresponding stacks are only simple addresses.

Select one of the memory change areas, click the stack on the right and the method calls related to the Demo, and you can directly jump to the corresponding code location, and it displays in detail the memory impact of each line of code.

If you find that the ZegoExpressEngine SDK's memory changes are abnormal, such as continuous memory growth or memory not being released as expected, you can save the Trace file and contact ZEGOCLOUD Technical Support for further analysis.

Previous

How to customize video capture rotation method?

Next

Why is it unable to get device ID on Chrome 81 and above browsers?

On this page

Back to top