1# @ohos.hidebug (HiDebug) 2 3> **NOTE** 4> 5> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 6 7The **hidebug** module provides APIs for you to obtain the memory usage of an application, including the static heap memory (native heap) and proportional set size (PSS) occupied by the application process. You can also export VM memory slices and collect VM CPU profiling data. 8 9## Modules to Import 10 11```ts 12import { hidebug } from '@kit.PerformanceAnalysisKit'; 13``` 14 15## hidebug.getNativeHeapSize 16 17getNativeHeapSize(): bigint 18 19Obtains the size of heap memory (including the allocator metadata) held by a process, which is measured by the memory allocator. 20 21**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 22 23**Return value** 24 25| Type | Description | 26| ------ | --------------------------- | 27| bigint | Size of the heap memory (including the allocator metadata) held by the process, in bytes.| 28 29**Example** 30 31```ts 32import { hidebug } from '@kit.PerformanceAnalysisKit'; 33 34let nativeHeapSize: bigint = hidebug.getNativeHeapSize(); 35``` 36 37## hidebug.getNativeHeapAllocatedSize 38 39getNativeHeapAllocatedSize(): bigint 40 41Obtains the size of the heap memory allocated to a process service, which is measured by the memory allocator. 42 43**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 44 45**Return value** 46 47| Type | Description | 48| ------ | --------------------------------- | 49| bigint | Size of the heap memory allocated to a process service, in bytes.| 50 51 52**Example** 53```ts 54import { hidebug } from '@kit.PerformanceAnalysisKit'; 55 56let nativeHeapAllocatedSize: bigint = hidebug.getNativeHeapAllocatedSize(); 57``` 58 59## hidebug.getNativeHeapFreeSize 60 61getNativeHeapFreeSize(): bigint 62 63Obtains the size of the cache memory held by the memory allocator. 64 65**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 66 67**Return value** 68 69| Type | Description | 70| ------ | ------------------------------- | 71| bigint | Size of the cache memory held by the memory allocator, in bytes.| 72 73**Example** 74```ts 75import { hidebug } from '@kit.PerformanceAnalysisKit'; 76 77let nativeHeapFreeSize: bigint = hidebug.getNativeHeapFreeSize(); 78``` 79 80## hidebug.getPss 81 82getPss(): bigint 83 84Obtains the size of the physical memory actually used by the application process. 85 86**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 87 88**Return value** 89 90| Type | Description | 91| ------ | ------------------------- | 92| bigint | Size of the physical memory actually used by the application process, in KB.| 93 94**Example** 95```ts 96import { hidebug } from '@kit.PerformanceAnalysisKit'; 97 98let pss: bigint = hidebug.getPss(); 99``` 100 101## hidebug.getVss<sup>11+<sup> 102 103getVss(): bigint 104 105Obtains the virtual set size used by the application process. 106 107**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 108 109**Return value** 110 111| Type | Description | 112| ------ | ---------------------------------------- | 113| bigint | Virtual set size used by the application process, in KB.| 114 115**Example** 116 117```ts 118import { hidebug } from '@kit.PerformanceAnalysisKit'; 119 120let vss: bigint = hidebug.getVss(); 121``` 122 123## hidebug.getSharedDirty 124 125getSharedDirty(): bigint 126 127Obtains the size of the shared dirty memory of a process. 128 129**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 130 131**Return value** 132 133| Type | Description | 134| ------ | -------------------------- | 135| bigint | Size of the shared dirty memory of the process, in KB.| 136 137 138**Example** 139```ts 140import { hidebug } from '@kit.PerformanceAnalysisKit'; 141 142let sharedDirty: bigint = hidebug.getSharedDirty(); 143``` 144 145## hidebug.getPrivateDirty<sup>9+<sup> 146 147getPrivateDirty(): bigint 148 149Obtains the size of the private dirty memory of a process. 150 151**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 152 153**Return value** 154 155| Type | Description | 156| ------ | -------------------------- | 157| bigint | Size of the private dirty memory of the process, in KB.| 158 159**Example** 160```ts 161import { hidebug } from '@kit.PerformanceAnalysisKit'; 162 163let privateDirty: bigint = hidebug.getPrivateDirty(); 164``` 165 166## hidebug.getCpuUsage<sup>9+<sup> 167 168getCpuUsage(): number 169 170Obtains the CPU usage of a process. 171 172For example, if the CPU usage is **50%**, **0.5** is returned. 173 174**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 175 176**Return value** 177 178| Type | Description | 179| ------ | -------------------------- | 180| number | CPU usage of the process.| 181 182 183**Example** 184```ts 185import { hidebug } from '@kit.PerformanceAnalysisKit'; 186 187let cpuUsage: number = hidebug.getCpuUsage(); 188``` 189 190## hidebug.getServiceDump<sup>9+<sup> 191 192getServiceDump(serviceid : number, fd : number, args : Array\<string>) : void 193 194Obtains system service information. 195 196**Required permissions**: ohos.permission.DUMP (available only for system applications) 197 198**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 199 200**Parameters** 201 202| Name | Type | Mandatory| Description | 203| -------- | ------ | ---- | ------------------------------------------------------------ | 204| serviceid | number | Yes | Obtains the system service information based on the specified service ID.| 205| fd | number | Yes | File descriptor to which data is written by the API.| 206| args | Array\<string> | Yes | Parameter list corresponding to the **Dump** API of the system service.| 207 208**Error codes** 209 210For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md). 211 212| ID| Error Message| 213| ------- | ----------------------------------------------------------------- | 214| 401 | the parameter check failed,Possible causes:1.the parameter type error 2.the args parameter is not string array | 215| 11400101 | ServiceId invalid. The system ability does not exist. | 216 217**Example** 218 219```ts 220import { fileIo } from '@kit.CoreFileKit'; 221import { hidebug } from '@kit.PerformanceAnalysisKit'; 222import { common } from '@kit.AbilityKit'; 223import { BusinessError } from '@kit.BasicServicesKit'; 224 225let applicationContext: common.Context | null = null; 226try { 227 let context = getContext() as common.UIAbilityContext; 228 applicationContext = context.getApplicationContext(); 229} catch (error) { 230 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 231} 232 233let filesDir: string = applicationContext!.filesDir; 234let path: string = filesDir + "/serviceInfo.txt"; 235console.info("output path: " + path); 236let file = fileIo.openSync(path, fileIo.OpenMode.READ_WRITE | fileIo.OpenMode.CREATE); 237let serviceId: number = 10; 238let args: Array<string> = new Array("allInfo"); 239 240try { 241 hidebug.getServiceDump(serviceId, file.fd, args); 242} catch (error) { 243 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 244} 245fileIo.closeSync(file); 246``` 247 248## hidebug.startJsCpuProfiling<sup>9+</sup> 249 250startJsCpuProfiling(filename : string) : void 251 252Starts the profiling method. `startJsCpuProfiling()` and `stopJsCpuProfiling()` are called in pairs. `startJsCpuProfiling()` always occurs before `stopJsCpuProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`. 253 254**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 255 256**Parameters** 257 258| Name | Type | Mandatory| Description | 259| -------- | ------ | ---- | ------------------------------------------------------------ | 260| filename | string | Yes | User-defined profile name. The `filename.json` file is generated in the `files` directory of the application based on the specified `filename`.| 261 262**Error codes** 263 264For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 265 266| ID| Error Message| 267| ------- | ----------------------------------------------------------------- | 268| 401 | the parameter check failed,Parameter type error | 269 270**Example** 271 272```ts 273import { hidebug } from '@kit.PerformanceAnalysisKit'; 274import { BusinessError } from '@kit.BasicServicesKit'; 275 276try { 277 hidebug.startJsCpuProfiling("cpu_profiling"); 278 // ... 279 hidebug.stopJsCpuProfiling(); 280} catch (error) { 281 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 282} 283``` 284 285## hidebug.stopJsCpuProfiling<sup>9+</sup> 286 287stopJsCpuProfiling() : void 288 289Stops the profiling method. `startJsCpuProfiling()` and `stopJsCpuProfiling()` are called in pairs. `startJsCpuProfiling()` always occurs before `stopJsCpuProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`. 290 291**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 292 293**Example** 294 295```ts 296import { hidebug } from '@kit.PerformanceAnalysisKit'; 297import { BusinessError } from '@kit.BasicServicesKit'; 298 299try { 300 hidebug.startJsCpuProfiling("cpu_profiling"); 301 // ... 302 hidebug.stopJsCpuProfiling(); 303} catch (error) { 304 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 305} 306``` 307 308## hidebug.dumpJsHeapData<sup>9+</sup> 309 310dumpJsHeapData(filename : string) : void 311 312Exports the heap data. 313 314**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 315 316**Parameters** 317 318| Name | Type | Mandatory| Description | 319| -------- | ------ | ---- | ------------------------------------------------------------ | 320| filename | string | Yes | User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the application based on the specified `filename`.| 321 322**Error codes** 323 324For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 325 326| ID| Error Message| 327| ------- | ----------------------------------------------------------------- | 328| 401 | the parameter check failed, Parameter type error | 329 330**Example** 331 332```ts 333import { hidebug } from '@kit.PerformanceAnalysisKit'; 334import { BusinessError } from '@kit.BasicServicesKit'; 335 336try { 337 hidebug.dumpJsHeapData("heapData"); 338} catch (error) { 339 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 340} 341``` 342 343## hidebug.startProfiling<sup>(deprecated)</sup> 344 345startProfiling(filename : string) : void 346 347> **NOTE** 348> This API is deprecated since API version 9. You are advised to use [hidebug.startJsCpuProfiling](#hidebugstartjscpuprofiling9). 349 350Starts the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`. 351 352**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 353 354**Parameters** 355 356| Name | Type | Mandatory| Description | 357| -------- | ------ | ---- | ------------------------------------------------------------ | 358| filename | string | Yes | User-defined profile name. The `filename.json` file is generated in the `files` directory of the application based on the specified `filename`.| 359 360**Example** 361 362```ts 363import { hidebug } from '@kit.PerformanceAnalysisKit'; 364 365hidebug.startProfiling("cpuprofiler-20220216"); 366// code block 367// ... 368// code block 369hidebug.stopProfiling(); 370``` 371 372## hidebug.stopProfiling<sup>(deprecated)</sup> 373 374stopProfiling() : void 375 376> **NOTE** 377> This API is deprecated since API version 9. You are advised to use [hidebug.stopJsCpuProfiling](#hidebugstopjscpuprofiling9). 378 379Stops the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the sequence similar to the following is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`. 380 381**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 382 383**Example** 384 385```ts 386import { hidebug } from '@kit.PerformanceAnalysisKit'; 387 388hidebug.startProfiling("cpuprofiler-20220216"); 389// code block 390// ... 391// code block 392hidebug.stopProfiling(); 393``` 394 395## hidebug.dumpHeapData<sup>(deprecated)</sup> 396 397dumpHeapData(filename : string) : void 398 399> **NOTE** 400> This API is deprecated since API version 9. You are advised to use [hidebug.dumpJsHeapData](#hidebugdumpjsheapdata9). 401 402Exports the heap data. 403 404**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 405 406**Parameters** 407 408| Name | Type | Mandatory| Description | 409| -------- | ------ | ---- | ------------------------------------------------------------ | 410| filename | string | Yes | User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the application based on the specified `filename`.| 411 412**Example** 413 414```ts 415import { hidebug } from '@kit.PerformanceAnalysisKit'; 416 417hidebug.dumpHeapData("heap-20220216"); 418``` 419 420## hidebug.getAppVMMemoryInfo<sup>12+</sup> 421 422getAppVMMemoryInfo(): VMMemoryInfo 423 424Obtains VM memory information. 425 426**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 427 428**Return value** 429 430| Type | Description | 431| -------------| --------------------------------------- | 432| [VMMemoryInfo](#vmmemoryinfo12) | VM memory information. | 433 434**Example** 435 436```ts 437import { hidebug } from '@kit.PerformanceAnalysisKit'; 438 439let vmMemory: hidebug.VMMemoryInfo = hidebug.getAppVMMemoryInfo(); 440console.info(`totalHeap = ${vmMemory.totalHeap}, heapUsed = ${vmMemory.heapUsed},` + 441 `allArraySize = ${vmMemory.allArraySize}` ); 442``` 443 444## hidebug.getAppThreadCpuUsage<sup>12+</sup> 445 446getAppThreadCpuUsage(): ThreadCpuUsage[] 447 448Obtains the CPU usage of application threads. 449 450**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 451 452**Return value** 453 454| Type | Description | 455| -----------------| ------------------------------------------------------------| 456| [ThreadCpuUsage](#threadcpuusage12)[] | CPU usage of all threads of the current application process.| 457 458 459 460**Example** 461 462```ts 463import { hidebug } from '@kit.PerformanceAnalysisKit'; 464 465let appThreadCpuUsage: hidebug.ThreadCpuUsage[] = hidebug.getAppThreadCpuUsage(); 466for (let ii = 0; ii < appThreadCpuUsage.length; ii++) { 467 console.info(`threadId=${appThreadCpuUsage[ii].threadId}, cpuUsage=${appThreadCpuUsage[ii].cpuUsage}`); 468} 469``` 470 471## hidebug.startAppTraceCapture<sup>12+</sup> 472 473startAppTraceCapture(tags : number[], flag: TraceFlag, limitSize: number) : string 474 475Starts automatic trace collection for a specified scope. This API is a supplement to the [hitrace](../../dfx/hitrace.md) module. 476The performance consumption during trace collection increases with the collection scope. Therefore, before using this API, you are advised to run the **hitrace** command to capture trace logs and select the key scope of trace collection to improve the API performance. 477 478**startAppTraceCapture()** and [stopAppTraceCapture ()](#hidebugstopapptracecapture12) must be called in pairs. Repeat calling of **startAppTraceCapture()** will cause exceptions. Trace collection consumes a lot of performance resources. Therefore, call **stopAppTraceCapture()** immediately after trace collection is complete. 479 480When an application calls **startAppTraceCapture()** to collect trace data and the size of the data exceeds the value of **limitSize**, the system automatically calls **stopAppTraceCapture()** to stop trace collection. Therefore, if **limitSize** is set improperly, the collected trace data is insufficient for fault analysis. Therefore, you need to evaluate the value of **limitSize** as required. 481 482Evaluation method: limitSize = Expected trace collection duration x Unit trace traffic. 483 484Expected trace collection duration: You can determine the duration based on the fault scenario. The unit is second. 485 486Unit trace traffic: The size of a trace generated by an application per second. The recommended value is 300 KB/s. You are advised to use the actual value of your application. The unit is KB/s. 487 488To obtain the unit trace traffic, you can call **startAppTraceCapture()** with **limitSize** set to the maximum value 500 MB. After **N** seconds, call **stopAppTraceCapture()** to stop the collection and check the trace size (**S** KB). The unit trace traffic is S/N. 489 490**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 491 492**Parameters** 493 494| Name | Type | Mandatory| Description | 495| -------- | ------ | ---- |------------------------------------| 496| tags | number[] | Yes | Scope for trace collection. For details, see [tags](#hidebugtags12).| 497| flag | TraceFlag| Yes | For details, see [TraceFlag](#traceflag12). | 498| limitSize| number | Yes | Limit on the trace file size, in bytes. The maximum size of a single file is 500 MB.| 499 500**Return value** 501 502| Type | Description | 503| -----------------| -----------------------------------------------| 504| string | Path of the trace file. | 505 506**Error codes** 507 508For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md). 509 510| ID| Error Message| 511| ------- | ----------------------------------------------------------------- | 512| 401 | Invalid argument, Possible causes:1.The limit parameter is too small 2.The parameter is not within the enumeration type 3.The parameter type error or parameter order error| 513| 11400102 | Capture trace already enabled. | 514| 11400103 | No write permission on the file. | 515| 11400104 | Abnormal trace status. | 516 517**Example** 518 519```ts 520import { hidebug } from '@kit.PerformanceAnalysisKit'; 521 522let tags: number[] = [hidebug.tags.ABILITY_MANAGER, hidebug.tags.ARKUI]; 523let flag: hidebug.TraceFlag = hidebug.TraceFlag.MAIN_THREAD; 524let limitSize: number = 1024 * 1024; 525 526try { 527 let fileName: string = hidebug.startAppTraceCapture(tags, flag, limitSize); 528 // code block 529 // ... 530 // code block 531 hidebug.stopAppTraceCapture(); 532} catch (error) { 533 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 534} 535``` 536 537## hidebug.stopAppTraceCapture<sup>12+</sup> 538 539stopAppTraceCapture() : void 540 541Stops application trace collection. Use [startAppTraceCapture()](#hidebugstartapptracecapture12) to start collection before calling this API. If this API is called before trace collection or it is repeatedly called, an exception will occur. 542 543If **startAppTraceCapture ()** is called without a properly specified **limitSize**, the size of the generated trace may exceed the **limitSize** value, causing the system to automatically call **stopAppTraceCapture()**. In this case, if **stopAppTraceCapture()** is called again, an error code 11400105 will be displayed. 544 545**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 546 547**Error codes** 548 549For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md). 550 551| ID| Error Message| 552| ------- | ----------------------------------------------------------------- | 553| 11400104 | The status of the trace is abnormal | 554| 11400105 | No capture trace running | 555 556**Example** 557 558```ts 559import { hidebug } from '@kit.PerformanceAnalysisKit'; 560 561let tags: number[] = [hidebug.tags.ABILITY_MANAGER, hidebug.tags.ARKUI]; 562let flag: hidebug.TraceFlag = hidebug.TraceFlag.MAIN_THREAD; 563let limitSize: number = 1024 * 1024; 564try { 565 let fileName: string = hidebug.startAppTraceCapture(tags, flag, limitSize); 566 // code block 567 // ... 568 // code block 569 hidebug.stopAppTraceCapture(); 570} catch (error) { 571 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 572} 573``` 574 575## hidebug.getAppMemoryLimit<sup>12+</sup> 576 577getAppMemoryLimit() : MemoryLimit 578 579Obtains the memory limit of the application process. 580 581**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 582 583**Return value** 584 585| Type | Description | 586| ------ | -------------------------- | 587| [MemoryLimit](#memorylimit12) | Memory limit of the application process.| 588 589**Example** 590 591```ts 592import { hidebug } from '@kit.PerformanceAnalysisKit'; 593 594let appMemoryLimit:hidebug.MemoryLimit = hidebug.getAppMemoryLimit(); 595``` 596 597## hidebug.getSystemCpuUsage<sup>12+</sup> 598 599getSystemCpuUsage() : number 600 601Obtains the CPU usage of the system. 602 603For example, if the CPU usage of system resources is **50%**, **0.5** is returned. 604 605**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 606 607**Return value** 608 609| Type | Description | 610|--------|-------------| 611| number | CPU usage of the system.| 612 613**Error codes** 614 615For details about the error codes, see [HiDebug CPU Usage Error Codes](errorcode-hiviewdfx-hidebug-cpuusage.md). 616 617| ID| Error Message | 618| ------- |-------------------------------------------------| 619| 11400104 | The status of the system CPU usage is abnormal. | 620 621**Example** 622```ts 623import { hidebug } from '@kit.PerformanceAnalysisKit'; 624 625try { 626 console.info(`getSystemCpuUsage: ${hidebug.getSystemCpuUsage()}`) 627} catch (error) { 628 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 629} 630``` 631 632## hidebug.setAppResourceLimit<sup>12+</sup> 633 634setAppResourceLimit(type: string, value: number, enableDebugLog: boolean) : void 635 636Sets the number of FDs, number of threads, JS memory, or native memory limit of the application. 637**NOTE**: This feature is available only after **Developer options** is enabled and the device is restarted. 638 639**Atomic service API**: This API can be used in atomic services since API version 12. 640 641**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 642 643**Parameters** 644 645| Name | Type | Mandatory| Description | 646| -------- | ------ | ---- | ------------------------------------------------------------ | 647| type | string | Yes | Types of resource leakage: pss_memory (native memory) leak, js_heap (js heap memory) leak, fd (file descriptor) leak, and thread (thread) leak.| 648| value | number | Yes | The maximum value of a resource leakage type. Value range: pss_memory leak **[1024, 4 * 1024 * 1024] (in KB)**, js_heap memory leak **[85, 95]** (85% to 95% of the upper limit of the JS heap memory), fd leak [10, 10000], thread leak **[1, 1000]**| 649| enableDebugLog | boolean | Yes | Whether to enable debug log. The default value is **false**. Set this parameter to **true** only in the dark version because collecting debug logs consumes too much CPU or memory.| 650 651**Error codes** 652 653For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md). 654 655| ID| Error Message| 656| ------- | ----------------------------------------------------------------- | 657| 401 | Invalid argument, Possible causes:1.The limit parameter is too small 2.The parameter is not in the specified type 3.The parameter type error or parameter order error | 658| 11400104 | Set limit failed due to remote exception | 659 660**Example** 661 662```ts 663import { hidebug } from '@kit.PerformanceAnalysisKit'; 664 665let type: string = 'js_heap'; 666let value: number = 85; 667let enableDebugLog: boolean = false; 668try { 669 hidebug.setAppResourceLimit(type, value, enableDebugLog); 670} catch (error) { 671 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 672} 673``` 674 675## hidebug.getAppNativeMemInfo<sup>12+</sup> 676 677getAppNativeMemInfo(): NativeMemInfo 678 679Obtains the memory information of the application process. 680 681**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 682 683**Return value** 684 685| Type | Description | 686| ------ | -------------------------- | 687| [NativeMemInfo](#nativememinfo12) | Memory information of the application process.| 688 689**Example** 690 691```ts 692import { hidebug } from '@kit.PerformanceAnalysisKit'; 693 694let nativeMemInfo: hidebug.NativeMemInfo = hidebug.getAppNativeMemInfo(); 695console.info(`pss: ${nativeMemInfo.pss}, vss: ${nativeMemInfo.vss}, rss: ${nativeMemInfo.rss}, ` + 696 `sharedDirty: ${nativeMemInfo.sharedDirty}, privateDirty: ${nativeMemInfo.privateDirty}, ` + 697 `sharedClean: ${nativeMemInfo.sharedClean}, privateClean: ${nativeMemInfo.privateClean}`); 698``` 699 700## hidebug.getSystemMemInfo<sup>12+</sup> 701 702getSystemMemInfo(): SystemMemInfo 703 704Obtains system memory information. 705 706**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 707 708**Return value** 709 710| Type | Description | 711| ------ | -------------------------- | 712| [SystemMemInfo](#systemmeminfo12) | System memory information.| 713 714**Example** 715 716```ts 717import { hidebug } from '@kit.PerformanceAnalysisKit'; 718 719let systemMemInfo: hidebug.SystemMemInfo = hidebug.getSystemMemInfo(); 720 721console.info(`totalMem: ${systemMemInfo.totalMem}, freeMem: ${systemMemInfo.freeMem}, ` + 722 `availableMem: ${systemMemInfo.availableMem}`); 723``` 724 725## hidebug.getVMRuntimeStats<sup>12+</sup> 726 727getVMRuntimeStats(): GcStats 728 729Obtains all system GC statistics. 730 731**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 732 733**Return value** 734 735| Type | Description | 736|-----------------------|----------| 737| [GcStats](#gcstats12) | System GC statistics.| 738 739**Example** 740 741```ts 742import { hidebug } from '@kit.PerformanceAnalysisKit'; 743 744let vMRuntimeStats: hidebug.GcStats = hidebug.getVMRuntimeStats(); 745console.info(`gc-count: ${vMRuntimeStats['ark.gc.gc-count']}`); 746console.info(`gc-time: ${vMRuntimeStats['ark.gc.gc-time']}`); 747console.info(`gc-bytes-allocated: ${vMRuntimeStats['ark.gc.gc-bytes-allocated']}`); 748console.info(`gc-bytes-freed: ${vMRuntimeStats['ark.gc.gc-bytes-freed']}`); 749console.info(`fullgc-longtime-count: ${vMRuntimeStats['ark.gc.fullgc-longtime-count']}`); 750``` 751 752## hidebug.getVMRuntimeStat<sup>12+</sup> 753 754getVMRuntimeStat(item : string): number 755 756Obtains the specified system GC statistics based on parameters. 757 758**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 759 760**Parameters** 761 762| Name | Type | Mandatory| Description | 763| -------- | ------ | ---- |-------------| 764| item | string | Yes | Item of the GC statistics to be obtained.| 765 766| Input Parameter | Return Value Description | 767|------------------------------|----------------| 768| ark.gc.gc-count | Count of GC of the calling thread. | 769| ark.gc.gc-time | GC time triggered by the calling thread, in milliseconds.| 770| ark.gc.gc-bytes-allocated | Memory size allocated to the Ark VM of the calling thread, in bytes.| 771| ark.gc.gc-bytes-freed | Memory freed by the GC of the calling thread, in bytes.| 772| ark.gc.fullgc-longtime-count | Count of long fullGC of the calling thread.| 773 774**Error codes** 775 776| ID| Error Message | 777| ------- |------------------------------------------------------------------------------------------------------------| 778| 401 | Possible causes:1. Invalid parameter, a string parameter required. 2. Invalid parameter, unknown property. | 779 780**Example** 781 782```ts 783import { hidebug } from '@kit.PerformanceAnalysisKit'; 784try { 785 console.info(`gc-count: ${hidebug.getVMRuntimeStat('ark.gc.gc-count')}`); 786 console.info(`gc-time: ${hidebug.getVMRuntimeStat('ark.gc.gc-time')}`); 787 console.info(`gc-bytes-allocated: ${hidebug.getVMRuntimeStat('ark.gc.gc-bytes-allocated')}`); 788 console.info(`gc-bytes-freed: ${hidebug.getVMRuntimeStat('ark.gc.gc-bytes-freed')}`); 789 console.info(`fullgc-longtime-count: ${hidebug.getVMRuntimeStat('ark.gc.fullgc-longtime-count')}`); 790} catch (error) { 791 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 792} 793``` 794 795## MemoryLimit<sup>12+</sup> 796 797Defines the memory limit of the application process. 798 799**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 800 801| Name | Type | Mandatory| Description | 802| --------- | ------ | ---- | ------------ | 803| rssLimit | bigint | Yes | Limit on the resident set size, in KB. | 804| vssLimit | bigint | Yes | Limit on the virtual memory size, in KB. | 805| vmHeapLimit | bigint | Yes | Limit on the JS VM heap size of the calling thread, in KB. | 806| vmTotalHeapSize | bigint | Yes | Size limit of the JS heap memory of the process, in KB. | 807 808## VMMemoryInfo<sup>12+</sup> 809 810Describes the VM memory information. 811 812**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 813 814| Name | Type | Readable| Writable| Description | 815| -------------------| ------- | ---- | ---- | ---------------------------------- | 816| totalHeap | bigint | Yes | No | Total heap size of the current VM, in KB. | 817| heapUsed | bigint | Yes | No | Heap size used by the current VM, in KB. | 818| allArraySize | bigint | Yes | No | Size of all array objects of the current VM, in KB.| 819 820## ThreadCpuUsage<sup>12+</sup> 821 822Describes the CPU usage of a thread. 823 824**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 825 826| Name | Type | Readable| Writable| Description | 827| -------------------| ------- | ---- | ---- | ----------------------------------- | 828| threadId | number | Yes | No | Thread ID. | 829| cpuUsage | number | Yes | No | CPU usage of the thread. | 830 831## hidebug.tags<sup>12+</sup> 832 833Enumerates the tags used in trace collection. You can use the [hitrace](../../dfx/hitrace.md) commands to capture the trace data of a specified tag for preview. 834 835Note that the tag values are defined by the system and may change with the version upgrade. To avoid compatibility issues after the upgrade, use the tag names instead of the tag values in application development. 836 837**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 838 839| Name | Type | Read Only | Description | 840| -------------------------| ------- |-----|--------------------------------------------| 841| ABILITY_MANAGER | number | Yes| Capability management. The corresponding command is **tagName:ability**. | 842| ARKUI | number | Yes| ArkUI development framework. The corresponding command is **tagName:ace**. | 843| ARK | number | Yes| JSVM VM. The corresponding command is **tagName:ark**. | 844| BLUETOOTH | number | Yes| Bluetooth. The corresponding command is **tagName:bluetooth**. | 845| COMMON_LIBRARY | number | Yes| Common library subsystem. The corresponding command is **tagName:commonlibrary**. | 846| DISTRIBUTED_HARDWARE_DEVICE_MANAGER | number | Yes| Distributed hardware device management. The corresponding command is **tagName:devicemanager**. | 847| DISTRIBUTED_AUDIO | number | Yes| Distributed audio. The corresponding command is **tagName:daudio**. | 848| DISTRIBUTED_CAMERA | number | Yes| Distributed camera. The corresponding command is **tagName:dcamera**. | 849| DISTRIBUTED_DATA | number | Yes| Distributed data management. The corresponding command is **tagName:distributeddatamgr**.| 850| DISTRIBUTED_HARDWARE_FRAMEWORK | number | Yes| Distributed hardware framework. The corresponding command is **tagName:dhfwk**. | 851| DISTRIBUTED_INPUT | number | Yes| Distributed input. The corresponding command is **tagName:dinput**. | 852| DISTRIBUTED_SCREEN | number | Yes| Distributed screen. The corresponding command is **tagName:dscreen**. | 853| DISTRIBUTED_SCHEDULER | number | Yes| Distributed scheduler. The corresponding command is **tagName:dsched**. | 854| FFRT | number | Yes| FFRT task. The corresponding command is **tagName:ffrt**. | 855| FILE_MANAGEMENT | number | Yes| File management system. The corresponding command is **tagName:filemanagement**. | 856| GLOBAL_RESOURCE_MANAGER | number | Yes| Global resource management. The corresponding command is **tagName:gresource**. | 857| GRAPHICS | number | Yes| Graphics module. The corresponding command is **tagName:graphic**. | 858| HDF | number | Yes| HDF subsystem. The corresponding command is **tagName:hdf**. | 859| MISC | number | Yes| MISC module. The corresponding command is **tagName:misc**. | 860| MULTIMODAL_INPUT | number | Yes| Multi-modal input module. The corresponding command is **tagName:multimodalinput**. | 861| NET | number | Yes| Network. The corresponding command is **tagName:net**. | 862| NOTIFICATION | number | Yes| Notification module. The corresponding command is **tagName:notification**. | 863| NWEB | number | Yes| Nweb. The corresponding command is **tagName:nweb**. | 864| OHOS | number | Yes| OHOS. The corresponding command is **tagName:ohos**. | 865| POWER_MANAGER | number | Yes| Power management. The corresponding command is **tagName:power**. | 866| RPC | number | Yes| RPC. The corresponding command is **tagName:rpc**. | 867| SAMGR | number | Yes| System capability management. The corresponding command is **tagName:samgr**. | 868| WINDOW_MANAGER | number | Yes| Window management. The corresponding command is **tagName:window**. | 869| AUDIO | number | Yes| Audio module. The corresponding command is **tagName:zaudio**. | 870| CAMERA | number | Yes| Camera module. The corresponding command is **tagName:zcamera**. | 871| IMAGE | number | Yes| Image module. The corresponding command is **tagName:zimage**. | 872| MEDIA | number | Yes| Media module. The corresponding command is **tagName:zmedia**. | 873 874## NativeMemInfo<sup>12+</sup> 875 876Describes memory information of the application process. 877 878**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 879 880| Name | Type | Mandatory| Description | 881| --------- | ------ | ---- | ------------ | 882| pss | bigint | Yes | Size of the occupied physical memory (including the proportionally allocated memory occupied by the shared library), in KB. | 883| vss | bigint | Yes | Size of the occupied virtual memory (including the memory occupied by the shared library), in KB. | 884| rss | bigint | Yes | Size of the occupied physical memory (including the memory occupied by the shared library), in KB. | 885| sharedDirty | bigint | Yes | Size of the shared dirty memory, in KB. | 886| privateDirty | bigint | Yes | Size of the private dirty memory, in KB. | 887| sharedClean | bigint | Yes | Size of the shared clean memory, in KB. | 888| privateClean | bigint | Yes | Size of the private clean memory, in KB. | 889 890## SystemMemInfo<sup>12+</sup> 891 892Describes the system memory information. 893 894**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 895 896| Name | Type | Mandatory| Description | 897| --------- | ------ | ---- | ------------ | 898| totalMem | bigint | Yes | Total memory of the system, in KB. | 899| freeMem | bigint | Yes | Free memory of the system, in KB. | 900| availableMem | bigint | Yes | Available memory of the system, in KB. | 901 902## TraceFlag<sup>12+</sup> 903 904Defines the type of the trace collection thread. 905 906**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 907 908| Name | Value| Description | 909| --------------------------- |---| ----------------------- | 910| MAIN_THREAD | 1 | The main thread of the application.| 911| ALL_THREADS | 2 | All threads of the application.| 912 913## GcStats<sup>12+</sup> 914 915type GcStats = Record<string, number> 916 917Key-value pair format used to store GC statistics. This type is not multi-thread safe. If a **GcStats** instance is operated by multiple threads at the same time in an application, use the lock mechanism for the instance. 918 919System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug 920 921| Type | Description | 922| -----------| ---------------------------- | 923| Record<string, number> | Indicates the value is in **Record** key-value pair format. | 924 925**GcStats** contain the following key values: 926 927| Name | Type | Description | 928|-------------------------| ------ |------------------------- | 929| ark.gc.gc-count | number | Count of GC of the calling thread.| 930| ark.gc.gc-time | number | GC time triggered by the calling thread, in milliseconds.| 931| ark.gc.gc-bytes-allocated | number | Memory size allocated to the Ark VM of the calling thread, in bytes.| 932| ark.gc.gc-bytes-freed | number | Memory freed by the GC of the calling thread, in bytes.| 933| ark.gc.fullgc-longtime-count | number | Count of long fullGC of the calling thread.| 934 935## hidebug.isDebugState<sup>12+</sup> 936 937isDebugState(): boolean 938 939Obtains whether an application process is being debugged. If the ark or native layer of the application process is being debugged, **true** is returned. Otherwise, **false** is returned. 940 941**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 942 943**Return value** 944 945| Type | Description | 946| ------ | -------------------------- | 947| boolean | Whether an application process is being debugged.| 948 949**Example** 950 951```ts 952import { hidebug } from '@kit.PerformanceAnalysisKit'; 953 954console.info(`isDebugState = ${hidebug.isDebugState()}`) 955``` 956 957## hidebug.getGraphicsMemory<sup>14+</sup> 958 959getGraphicsMemory(): Promise<number> 960 961Obtains the size of the GPU memory. This API uses a promise to return the result. 962 963**Atomic service API**: This API can be used in atomic services since API version 14. 964 965**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 966 967**Return value** 968 969| Type | Description | 970|-----------------------|------------------------------| 971| Promise<number> | Size of the GPU memory, in KB.| 972 973**Error codes** 974 975| ID| Error Message| 976| ------- | ----------------------------------------------------------------- | 977| 11400104 | Failed to get the application memory due to a remote exception. | 978 979**Example** 980 981```ts 982import { hidebug, hilog } from '@kit.PerformanceAnalysisKit'; 983import { BusinessError } from '@kit.BasicServicesKit'; 984 985hidebug.getGraphicsMemory().then((ret: number) => { 986 console.info(`graphicsMemory: ${ret}`) 987}).catch((error: BusinessError) => { 988 console.error(`error code: ${error.code}, error msg: ${error.message}`); 989}) 990``` 991 992## hidebug.getGraphicsMemorySync<sup>14+</sup> 993 994getGraphicsMemorySync(): number 995 996Obtains the size of the GPU memory. This API uses a synchronous callback to return the result. 997 998**Note**: This API involves multiple cross-process communications and may have performance problems. The asynchronous API **getGraphicsMemory** is recommended. 999 1000**Atomic service API**: This API can be used in atomic services since API version 14. 1001 1002**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug 1003 1004**Return value** 1005 1006| Type | Description | 1007| ------ |------------| 1008| number | Size of the GPU memory, in KB.| 1009 1010**Error codes** 1011 1012| ID| Error Message| 1013| ------- | ----------------------------------------------------------------- | 1014| 11400104 | Failed to get the application memory due to a remote exception. | 1015 1016**Example** 1017 1018```ts 1019import { hidebug } from '@kit.PerformanceAnalysisKit'; 1020import { BusinessError } from '@kit.BasicServicesKit'; 1021 1022try { 1023 console.info(`graphicsMemory: ${hidebug.getGraphicsMemorySync()}`) 1024} catch (error) { 1025 console.error(`error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`); 1026} 1027``` 1028