目录

Analysis of Empty Running Issues of the App UI Process

Introduction to Empty Running of the App UI Process

Empty running of the app UI process refers to the situation where the app process continues to refresh at a fixed frame rate even when there are no actual changes on the app UI, no user interaction, and no visible animations.

The following figure shows an example of a trace for empty running of the UI. In the figure, the main thread of the app, powerdemon, is refreshed at 90 Hz. However, in the highlighted area, the frame corresponding to the render_service thread is not refreshed. This indicates that powerdemon did not deliver any valid drawing instructions to render_service for drawing, resulting in empty frames. These empty frames are usually caused by the app registering a frame callback but there is no dirty area on any node.

You can further search for "FlushMessages" in the empty frame, as shown in the following figure. If "UI skip" is displayed under "FlushMessages", it indicates that no drawing instruction is delivered for the frame, and the UI is empty-running.

In the following figure, which is a non-UI empty running scenario, if "H:MarshRSTransactionData cmdCount: 2, transactionFlag:[22766,879]" is displayed under "FlushMessages", it indicates that a drawing instruction is delivered for the frame, and the preparation for the next frame render_service's RS tree will be triggered. In the preceding information, 22766 indicates the ID of the thread that delivers the drawing instruction, 879 indicates the index of the frame data, and cmdCount:2 indicates that the number of drawing instructions is 2. Two ArkUI nodes in the frame are triggered to refresh the dirty area.

Analysis

[h2]Using Trace Analysis

You can use the Energy template in Profiler to record a trace for analysis to find the cause of empty UI frames. Key trace: H:DisplaySyncId[29] Type[0]... Type indicates the cause of frame refresh, including:

Type[-1]: Other (callback API applied by the third party)

Type[0]: Animator

Type[1]: Xcomponent

[h2]Analysis Using the Energy Tool of Profiler (Recommended)

In DevEco Studio 6.1 (the phone version must be HarmonyOS 6.1 or later), the automatic detection and analysis capability has been added to address UI empty running issues. You can perform the following steps to locate the fault:

  1. Capture trace information.

    Click the Profiler, select the app process to be analyzed, create an Energy session, and perform operations on the app.

  2. View anomaly information.

    Click the Energy Anomaly lane. The anomaly information is displayed in the Detail column. UI Empty Run indicates that a UI empty running anomaly exists.

    In the figure, the following items are displayed: ① AnomalyType: indicates the anomaly type. ② Anomaly Reason: indicates the anomaly reason. ③ Anomaly Count: indicates the number of abnormal frames. ④ More: indicates the abnormal frames.

  1. View details of a single frame.

    In the More column, click a frame. In the main thread lane of the app, view the trace of the keyword H:DisplaySyncId and determine the root cause type based on the value of Type.

Common Root Causes

[h2]Animator Animation

Animator is an animation mechanism that generates UI refreshes based on the DisplaySync mechanism. As shown in position 1 in the following figure, "jsAnimator onframe, duration: 5000, curve: ease, id:1" indicates that the animation duration is 5000 ms, the animation curve is ease, and the ID of the Animator is 1.

After the hdc shell param set persist.ace.debug.enabled 1 feature is enabled, if the Animator causes the actual component attribute to be updated, the following information is displayed:
  • H:ViewPU.viewPropertyHasChanged MyAnimatorTest wid 1 6 0 false
  • H:ViewPU.viewPropertyHasChanged MyAnimatorTest hei 1 6 1 false

Pay attention to the MyAnimatorTest, wid, and hei information, which indicates that the Animator is executed on the MyAnimatorTest component and the affected state variables are wid and hei. In addition, the information about the component dirty area refresh, FlushTask, and SendCommand is displayed in areas 2, 3, and 4 in the following figure. It is confirmed that the Animator effect is implemented by updating the component variables wid and hei when the drawing instruction is submitted.

The main root cause of the empty Animator running is that the Animator does not proactively stop or cancel when the component is destructed or becomes invisible. Animator is an independent callback object and is not associated with components in a one-to-one manner. Therefore, the system cannot reclaim these objects that continuously run empty.

When using Animator, you can follow these two key points:

  1. Ensure that Animator calls finish and is left empty during component destruction to effectively avoid empty running and memory leaks.
  2. Add a visibility callback to the component. By default, Animator does not play. It plays only when the component is visible.
let expectedFrameRate: ExpectedFrameRateRange = {
  min: 0,
  max: 120,
  expected: 30
}

@Component
export struct MyAnimatorTest {
  private TAG: string = '[AnimatorTest]'
  private backAnimator: AnimatorResult | undefined = undefined
  private flag: boolean = false
  @State wid: number = 100
  @State hei: number = 100

  create() {
    this.backAnimator = this.getUIContext()?.createAnimator({
      // You are advised to use this.getUIContext().createAnimator().
      duration: 5000,
      easing: "ease",
      delay: 0,
      fill: "forwards",
      direction: "normal",
      iterations: 1,
      begin: 100, // Start point of the animation interpolation.
      end: 200 // End point of the animation interpolation.
    })
    this.backAnimator.setExpectedFrameRateRange(expectedFrameRate)
    this.backAnimator.onFinish = () => {
      this.flag = true
      console.info(this.TAG, 'backAnimator onFinish')
    }
    this.backAnimator.onRepeat = () => {
      console.info(this.TAG, 'backAnimator repeat')
    }
    this.backAnimator.onCancel = () => {
      console.info(this.TAG, 'backAnimator cancel')
    }
    this.backAnimator.onFrame = (value: number) => {
      this.wid = value
      this.hei = value
    }
  }

  aboutToDisappear() {
    // Since backAnimator references this in onFrame, and this holds backAnimator,
    // set backAnimator stored in the custom component to undefined when the component disappears to avoid memory leak.
    this.backAnimator?.finish();
    this.backAnimator = undefined;
  }

  build() {
    Column() {
      Column() {
        Column()
          .width(this.wid)
          .height(this.hei)
          .backgroundColor(Color.Red)
          .onVisibleAreaChange([0.0, 1.0], (isExpanding: boolean, currentRatio: number) => {
            if (!isExpanding && currentRatio <= 0.0) {
              console.info('Component is completely invisible.')
              this.backAnimator?.pause()
            }
          })
      }
      .width('100%')
      .height(300)

      Column() {
        Row() {
          Button('create')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.create()
            })
        }
        .padding(10)

        Row() {
          Button('play')
            .fontSize(30)
            .fontColor(Color.Black)
            .onClick(() => {
              this.flag = false
              if (this.backAnimator) {
                this.backAnimator.play()
              }
            })
        }
        .padding(10)
      }
    }
  }
}

[h2]DisplaySync

DisplaySync allows you to run UI services at a specified frame rate. It is mainly used for fine-grained control of the drawing frame rate in scenarios such as dynamic frame rate animation and setting a drawing frame rate independent of the system refresh rate for specific UI components.

Recommended optimization measures:

1. If you want to register a listener for window frame rate changes to analyze scenarios such as UI freezing, frame loss, and FPS monitoring, you are advised to use the frameMetricsMeasured API provided by ohos.window. This API triggers the registered callback only when the client UI content is redrawn (for example, during page switching, interaction with responsive components, or setting the background color and transparency).

2. When using DisplaySync to implement custom animations and independent UI drawing control, ensure proper lifecycle management. For details about the optimization solution, see Optimization Example of displaySync.