openharmony 鸿蒙 hidebug-guidelines-arkts

2025-06-12 浏览 (1)

Using HiDebug (ArkTS)

HiDebug provides multiple methods for debugging and profiling applications. With these methods, you can obtain memory, CPU, GPU, and GC data, collect process trace and profiler data, and dump VM heap snapshots. Since most APIs of this module are both performance-consuming and time-consuming, and are defined based on the HiDebug module, you are advised to use these APIs only during the application debugging and profiling phases. If the APIs are required in other scenarios, evaluate the impact of the APIs on application performance.

Available APIs

APIDescription
hidebug.getNativeHeapSizeObtains the size of heap memory (including the allocator metadata) held by a process, which is measured by the memory allocator.
hidebug.getNativeHeapAllocatedSizeObtains the size of the heap memory allocated to a process service, which is measured by the memory allocator.
hidebug.getNativeHeapFreeSizeObtains the size of the cache memory held by the memory allocator.
hidebug.getPssObtains the size of the physical memory actually used by the application process.
hidebug.getVssObtains the virtual set size used by the application process.
hidebug.getSharedDirtyObtains the size of the shared dirty memory of a process.
hidebug.getPrivateDirtyObtains the size of the private dirty memory of a process.
hidebug.getCpuUsageObtains the CPU usage of a process.
hidebug.getServiceDumpObtains system service information.
hidebug.dumpJsHeapDataExports the heap data.
hidebug.startJsCpuProfilingStarts the VM profiling method.
hidebug.stopJsCpuProfilingStops the VM profiling method.
hidebug.getAppVMMemoryInfoObtains VM memory information.
hidebug.getAppThreadCpuUsageObtains the CPU usage of application threads.
hidebug.startAppTraceCaptureStarts application trace collection.
hidebug.stopAppTraceCaptureStops application trace collection.
hidebug.getAppMemoryLimitObtains the memory limit of the application process.
hidebug.getSystemCpuUsageObtains the CPU usage of the system.
hidebug.setAppResourceLimitSets the number of FDs, number of threads, JS memory, or native memory limit of the application.
hidebug.getAppNativeMemInfoObtains the memory information of the application process.
hidebug.getSystemMemInfoObtains system memory information.
hidebug.getVMRuntimeStatsObtains all system GC statistics.
hidebug.getVMRuntimeStatObtains the specified system GC statistics based on parameters.
hidebug.isDebugStateObtains the debugging state of an application process.
hidebug.getGraphicsMemoryObtains the size of the GPU memory asynchronously.
hidebug.getGraphicsMemorySyncObtains the size of the GPU memory synchronously.
hidebug.dumpJsRawHeapDataDumps the original heap snapshot of the VM for the calling thread.

For details about how to use HiDebug, see the API Reference.

How to Develop

The following describes how to add a button in the application and click the button to call the hidebug APIs.

  1. Create a project, with Empty Ability as the template.

  2. On the project configuration page, set Model to Stage.

  3. In the Project window, click entry > src > main > ets > pages to open the Index.ets file.

    The following shows how to add hidebug.getSystemCpuUsage() to call the hidebug APIs. For details about how to use other APIs, see API Reference.

    import { hidebug } from '@kit.PerformanceAnalysisKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    function testHiDebug(event?: ClickEvent) {
      try {
        console.info(`getSystemCpuUsage: ${hidebug.getSystemCpuUsage()}`);
      } catch (error) {
        console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
      }
    }
    

    Add a click event to the Text component. The sample code is as follows:

    @Entry
    @Component
    struct Index {
      @State message: string = 'Hello World';
    
      build() {
        Row() {
          Column() {
            Text(this.message)
              .fontSize(50)
              .fontWeight(FontWeight.Bold)
              .onClick(testHiDebug);// Add a click event.
          }
          .width('100%')
        }
        .height('100%')
      }
    }
    
  4. Run the project on a real device and click "Hello World" on the app/service.

  5. At the bottom of DevEco Studio, switch to the Log tab and set the filter criteria to testTag.

    Then, the CPU usage logs obtained using hidebug.getSystemCpuUsage() are displayed in the window.

     08-20 11:06:01.891   1948-1948     A03d00/JSAPP                    com.examp...lication  I     getSystemCpuUsage: 0.4722222222222222
    

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Performance Analysis Kit

harmony 鸿蒙Analyzing Application Freeze

harmony 鸿蒙Development of Application Recovery

harmony 鸿蒙Analyzing C++ Crash

harmony 鸿蒙Development of Error Manager

harmony 鸿蒙hdc

harmony 鸿蒙Event Reporting

harmony 鸿蒙Introduction to HiAppEvent

harmony 鸿蒙Subscribing to Address Sanitizer Events (ArkTS)

harmony 鸿蒙Subscribing to Address Sanitizer Events (C/C++)

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/pjDOqH