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.info(`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.info(`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.info(`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.info(`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.info(`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, hilog } from '@kit.PerformanceAnalysisKit';
438
439let vmMemory: hidebug.VMMemoryInfo = hidebug.getAppVMMemoryInfo();
440hilog.info(0x0000, "example", "totalHeap = %{public}d", vmMemory.totalHeap);
441hilog.info(0x0000, "example", "heapUsed = %{public}d", vmMemory.heapUsed);
442hilog.info(0x0000, "example", "allArraySize = %{public}d", vmMemory.allArraySize);
443```
444
445## hidebug.getAppThreadCpuUsage<sup>12+</sup>
446
447getAppThreadCpuUsage(): ThreadCpuUsage[]
448
449Obtains the CPU usage of application threads.
450
451**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
452
453**Return value**
454
455| Type            | Description                                                       |
456| -----------------| ------------------------------------------------------------|
457| [ThreadCpuUsage](#threadcpuusage12)[] | CPU usage of all threads of the current application process.|
458
459
460
461**Example**
462
463```ts
464import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
465
466let appThreadCpuUsage: hidebug.ThreadCpuUsage[] = hidebug.getAppThreadCpuUsage();
467for (let ii = 0; ii < appThreadCpuUsage.length; ii++) {
468    hilog.info(0x0000, "example", "threadId=%{public}d, cpuUsage=%{public}f", appThreadCpuUsage[ii].threadId,
469    appThreadCpuUsage[ii].cpuUsage);
470}
471```
472
473## hidebug.startAppTraceCapture<sup>12+</sup>
474
475startAppTraceCapture(tags : number[], flag: TraceFlag, limitSize: number) : string
476
477Starts application trace collection. **startAppTraceCapture()** and **[stopAppTraceCapture()](#hidebugstopapptracecapture12)** must be called in pairs.
478
479**startAppTraceCapture()** always occurs before **stopAppTraceCapture()**; that is, calling the APIs in the sequence similar to the following is prohibited: start -> start -> stop, start -> stop -> stop, and start -> start -> stop -> stop.
480
481When 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 collecting trace data. 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.
482
483Evaluation method: limitSize = Expected trace collection duration x Unit trace traffic.
484
485Expected trace collection duration: You can determine the duration based on the fault scenario. The unit is second.
486
487Unit 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.
488
489To 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.
490
491**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
492
493**Parameters**
494
495| Name  | Type    | Mandatory| Description                                  |
496| -------- | ------   | ---- |--------------------------------------|
497| tags     | number[] | Yes  | For details, see [tags](#hidebugtags12).          |
498| flag     | TraceFlag| Yes  | For details, see [TraceFlag](#traceflag12).       |
499| limitSize| number   | Yes  | Limit on the trace file size, in bytes. The maximum size of a single file is 500 MB.|
500
501**Return value**
502
503| Type            | Description                                          |
504| -----------------| -----------------------------------------------|
505| string           | Path of the trace file.                           |
506
507**Error codes**
508
509For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md).
510
511| ID| Error Message|
512| ------- | ----------------------------------------------------------------- |
513| 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|
514| 11400102 | Capture trace already enabled.                                         |
515| 11400103 | No write permission on the file.                                |
516| 11400104 | Abnormal trace status.                                 |
517
518**Example**
519
520```ts
521import { hidebug } from '@kit.PerformanceAnalysisKit';
522
523let tags: number[] = [hidebug.tags.ABILITY_MANAGER, hidebug.tags.ARKUI];
524let flag: hidebug.TraceFlag = hidebug.TraceFlag.MAIN_THREAD;
525let limitSize: number = 1024 * 1024;
526let fileName: string = hidebug.startAppTraceCapture(tags, flag, limitSize);
527// code block
528// ...
529// code block
530hidebug.stopAppTraceCapture();
531```
532
533## hidebug.stopAppTraceCapture<sup>12+</sup>
534
535stopAppTraceCapture() : void
536
537Stops application trace collection. [startAppTraceCapture()](#hidebugstartapptracecapture12) and **stopAppTraceCapture()** must be called in pairs.
538
539**startAppTraceCapture()** always occurs before **stopAppTraceCapture()**; that is, calling the APIs in the sequence similar to the following is prohibited: start -> start -> stop, start -> stop -> stop, and start -> start -> stop -> stop.
540
541If **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.
542
543**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
544
545**Error codes**
546
547For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md).
548
549| ID| Error Message|
550| ------- | ----------------------------------------------------------------- |
551| 11400104 | The status of the trace is abnormal                                |
552| 11400105 |   No capture trace running                                       |
553
554**Example**
555
556```ts
557import { hidebug } from '@kit.PerformanceAnalysisKit';
558
559let tags: number[] = [hidebug.tags.ABILITY_MANAGER, hidebug.tags.ARKUI];
560let flag: hidebug.TraceFlag = hidebug.TraceFlag.MAIN_THREAD;
561let limitSize: number = 1024 * 1024;
562let fileName: string = hidebug.startAppTraceCapture(tags, flag, limitSize);
563// code block
564// ...
565// code block
566hidebug.stopAppTraceCapture();
567```
568
569## hidebug.getAppMemoryLimit<sup>12+</sup>
570
571getAppMemoryLimit() : MemoryLimit
572
573Obtains the memory limit of the application process.
574
575**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
576
577**Return value**
578
579| Type | Description                     |
580| ------ | -------------------------- |
581| [MemoryLimit](#memorylimit12) | Memory limit of the application process.|
582
583**Example**
584
585```ts
586import { hidebug } from '@kit.PerformanceAnalysisKit';
587
588let appMemoryLimit:hidebug.MemoryLimit = hidebug.getAppMemoryLimit();
589```
590
591## hidebug.getSystemCpuUsage<sup>12+</sup>
592
593getSystemCpuUsage() : number
594
595Obtains the CPU usage of the system.
596
597For example, if the CPU usage of system resources is **50%**, **0.5** is returned.
598
599**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
600
601**Return value**
602
603| Type    | Description         |
604|--------|-------------|
605| number | CPU usage of the system.|
606
607**Error codes**
608
609For details about the error codes, see [HiDebug CPU Usage Error Codes](errorcode-hiviewdfx-hidebug-cpuusage.md).
610
611| ID| Error Message                                           |
612| ------- |-------------------------------------------------|
613| 11400104 | The status of the system CPU usage is abnormal. |
614
615**Example**
616```ts
617import { hidebug } from '@kit.PerformanceAnalysisKit';
618
619let cpuUsage: number = hidebug.getSystemCpuUsage();
620```
621
622## hidebug.setAppResourceLimit<sup>12+</sup>
623
624setAppResourceLimit(type: string, value: number, enableDebugLog: boolean) : void
625
626Sets the number of FDs, number of threads, JS memory, or native memory limit of the application.
627**NOTE**: This function is valid only when  **Developer options** is enabled and the device is restarted.
628
629**Atomic service API**: This API can be used in atomic services since API version 12.
630
631**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
632
633**Parameters**
634
635| Name  | Type  | Mandatory| Description                                                        |
636| -------- | ------ | ---- | ------------------------------------------------------------ |
637| 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.|
638| 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]**|
639| 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.|
640
641**Error codes**
642
643For details about the error codes, see [HiDebug Error Codes](errorcode-hiviewdfx-hidebug.md).
644
645| ID| Error Message|
646| ------- | ----------------------------------------------------------------- |
647| 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  |
648| 11400104 | Set limit failed due to remote exception |
649
650**Example**
651
652```ts
653import { hidebug } from '@kit.PerformanceAnalysisKit';
654
655let type: string = 'js_heap';
656let value: number = 85;
657let enableDebugLog: boolean = false;
658hidebug.setAppResourceLimit(type, value, enableDebugLog);
659```
660
661## hidebug.getAppNativeMemInfo<sup>12+</sup>
662
663getAppNativeMemInfo(): NativeMemInfo
664
665Obtains the memory information of the application process.
666
667**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
668
669**Return value**
670
671| Type | Description                     |
672| ------ | -------------------------- |
673| [NativeMemInfo](#nativememinfo12) | Memory information of the application process.|
674
675**Example**
676
677```ts
678import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
679
680let nativeMemInfo: hidebug.NativeMemInfo = hidebug.getAppNativeMemInfo();
681
682hilog.info(0x0000, 'testTag', "pss = %{public}d", nativeMemInfo.pss);
683
684hilog.info(0x0000, 'testTag', "vss = %{public}d", nativeMemInfo.vss);
685
686hilog.info(0x0000, 'testTag', "rss = %{public}d", nativeMemInfo.rss);
687
688hilog.info(0x0000, 'testTag', "sharedDirty = %{public}d", nativeMemInfo.sharedDirty);
689
690hilog.info(0x0000, 'testTag', "privateDirty = %{public}d", nativeMemInfo.privateDirty);
691
692hilog.info(0x0000, 'testTag', "sharedClean = %{public}d", nativeMemInfo.sharedClean);
693
694hilog.info(0x0000, 'testTag', "privateClean = %{public}d", nativeMemInfo.privateClean);
695```
696
697## hidebug.getSystemMemInfo<sup>12+</sup>
698
699getSystemMemInfo(): SystemMemInfo
700
701Obtains system memory information.
702
703**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
704
705**Return value**
706
707| Type | Description                     |
708| ------ | -------------------------- |
709| [SystemMemInfo](#systemmeminfo12) | System memory information.|
710
711**Example**
712
713```ts
714import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
715
716let systemMemInfo: hidebug.SystemMemInfo = hidebug.getSystemMemInfo();
717
718hilog.info(0x0000, 'testTag', "totalMem = %{public}d", systemMemInfo.totalMem);
719
720hilog.info(0x0000, 'testTag', "freeMem = %{public}d", systemMemInfo.freeMem);
721
722hilog.info(0x0000, 'testTag', "availableMem = %{public}d", 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, hilog } from '@kit.PerformanceAnalysisKit';
743
744let vMRuntimeStats: hidebug.GcStats = hidebug.getVMRuntimeStats();
745hilog.info(0x0000, "testTag", `gc-count: ${vMRuntimeStats['ark.gc.gc-count']}`);
746hilog.info(0x0000, "testTag", `gc-time: ${vMRuntimeStats['ark.gc.gc-time']}`);
747hilog.info(0x0000, "testTag", `gc-bytes-allocated: ${vMRuntimeStats['ark.gc.gc-bytes-allocated']}`);
748hilog.info(0x0000, "testTag", `gc-bytes-freed: ${vMRuntimeStats['ark.gc.gc-bytes-freed']}`);
749hilog.info(0x0000, "testTag", `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, hilog } from '@kit.PerformanceAnalysisKit';
784
785hilog.info(0x0000, "testTag", `gc-count: ${hidebug.getVMRuntimeStat('ark.gc.gc-count')}`);
786hilog.info(0x0000, "testTag", `gc-time: ${hidebug.getVMRuntimeStat('ark.gc.gc-time')}`);
787hilog.info(0x0000, "testTag", `gc-bytes-allocated: ${hidebug.getVMRuntimeStat('ark.gc.gc-bytes-allocated')}`);
788hilog.info(0x0000, "testTag", `gc-bytes-freed: ${hidebug.getVMRuntimeStat('ark.gc.gc-bytes-freed')}`);
789hilog.info(0x0000, "testTag", `fullgc-longtime-count: ${hidebug.getVMRuntimeStat('ark.gc.fullgc-longtime-count')}`);
790```
791
792## MemoryLimit<sup>12+</sup>
793
794Defines the memory limit of the application process.
795
796**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
797
798| Name     | Type  | Mandatory| Description        |
799| --------- | ------ | ---- | ------------ |
800| rssLimit    | bigint |  Yes | Limit on the resident set size, in KB.    |
801| vssLimit  | bigint |  Yes | Limit on the virtual memory size, in KB.      |
802| vmHeapLimit | bigint |  Yes | Limit on the JS VM heap size of the calling thread, in KB.     |
803| vmTotalHeapSize | bigint |  Yes | Size limit of the JS heap memory of the process, in KB.     |
804
805## VMMemoryInfo<sup>12+</sup>
806
807Describes the VM memory information.
808
809**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
810
811| Name              | Type   | Readable| Writable| Description                               |
812| -------------------| ------- | ---- | ---- | ---------------------------------- |
813| totalHeap          | bigint  | Yes  | No  | Total heap size of the current VM, in KB.   |
814| heapUsed           | bigint  | Yes  | No  | Heap size used by the current VM, in KB. |
815| allArraySize       | bigint  | Yes  | No  | Size of all array objects of the current VM, in KB.|
816
817## ThreadCpuUsage<sup>12+</sup>
818
819Describes the CPU usage of a thread.
820
821**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
822
823| Name              | Type   | Readable| Writable| Description                               |
824| -------------------| ------- | ---- | ---- | ----------------------------------- |
825| threadId           | number  | Yes  | No  | Thread ID.                          |
826| cpuUsage           | number  | Yes  | No  | CPU usage of the thread.                      |
827
828## hidebug.tags<sup>12+</sup>
829
830Enumerates scenario tags.
831
832**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
833
834| Name                    | Type   | Read Only |Description                               |
835| -------------------------| ------- |-----|----------------------------------- |
836| ABILITY_MANAGER          | number  | Yes|  Capability management.                        |
837| ARKUI                    | number  | Yes|  ArkUI development framework.                   |
838| ARK                      | number  | Yes|  JSVM VM.                      |
839| BLUETOOTH                | number  | Yes|  Bluetooth.                           |
840| COMMON_LIBRARY           | number  | Yes|  Common library subsystem.                    |
841| DISTRIBUTED_HARDWARE_DEVICE_MANAGER | number  | Yes|  Distributed hardware device management.    |
842| DISTRIBUTED_AUDIO        | number  | Yes|        Distributed audio.                |
843| DISTRIBUTED_CAMERA       | number  | Yes|  Distributed camera.                      |
844| DISTRIBUTED_DATA         | number  | Yes|  Distributed data management.               |
845| DISTRIBUTED_HARDWARE_FRAMEWORK | number  | Yes|  Distributed hardware framework.             |
846| DISTRIBUTED_INPUT        | number  | Yes|  Distributed input.                      |
847| DISTRIBUTED_SCREEN       | number  | Yes|  Distributed screen.                      |
848| DISTRIBUTED_SCHEDULER    | number  | Yes|  Distributed scheduler.                    |
849| FFRT                     | number  | Yes|  FFRT task.                       |
850| FILE_MANAGEMENT          | number  | Yes|  File management system.                    |
851| GLOBAL_RESOURCE_MANAGER  | number  | Yes|  Global resource management.                    |
852| GRAPHICS                 | number  | Yes|  Graphics module.                       |
853| HDF                      | number  | Yes|  HDF subsystem.                      |
854| MISC                     | number  | Yes|  MISC module.                       |
855| MULTIMODAL_INPUT         | number  | Yes|  Multimodal input module.                  |
856| NET                      | number  | Yes|  Network.                            |
857| NOTIFICATION             | number  | Yes|  Notification module.                        |
858| NWEB                     | number  | Yes|  Nweb.                           |
859| OHOS                     | number  | Yes|  OHOS.                        |
860| POWER_MANAGER            | number  | Yes|  Power management.                        |
861| RPC                      | number  | Yes|  RPC.                            |
862| SAMGR                    | number  | Yes|  System capability management.                    |
863| WINDOW_MANAGER           | number  | Yes|  Window management.                        |
864| AUDIO                    | number  | Yes|  Audio module.                       |
865| CAMERA                   | number  | Yes|  Camera module.                       |
866| IMAGE                    | number  | Yes|  Image module.                       |
867| MEDIA                    | number  | Yes|  Media module.                       |
868
869## NativeMemInfo<sup>12+</sup>
870
871Describes memory information of the application process.
872
873**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
874
875| Name     | Type  | Mandatory| Description        |
876| --------- | ------ | ---- | ------------ |
877| pss  | bigint |  Yes | Size of the occupied physical memory (including the proportionally allocated memory occupied by the shared library), in KB.    |
878| vss  | bigint |  Yes | Size of the occupied virtual memory (including the memory occupied by the shared library), in KB.      |
879| rss  | bigint |  Yes | Size of the occupied physical memory (including the memory occupied by the shared library), in KB.        |
880| sharedDirty  | bigint |  Yes | Size of the shared dirty memory, in KB.     |
881| privateDirty  | bigint |  Yes | Size of the private dirty memory, in KB.     |
882| sharedClean  | bigint |  Yes | Size of the shared clean memory, in KB.     |
883| privateClean  | bigint |  Yes | Size of the private clean memory, in KB.     |
884
885## SystemMemInfo<sup>12+</sup>
886
887Describes the system memory information.
888
889**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
890
891| Name     | Type  | Mandatory| Description        |
892| --------- | ------ | ---- | ------------ |
893| totalMem  | bigint |  Yes | Total memory of the system, in KB.    |
894| freeMem  | bigint |  Yes | Free memory of the system, in KB.      |
895| availableMem  | bigint |  Yes | Available memory of the system, in KB.     |
896
897## TraceFlag<sup>12+</sup>
898
899Defines the type of the trace collection thread.
900
901**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
902
903| Name                        | Value| Description                   |
904| --------------------------- |---| ----------------------- |
905| MAIN_THREAD                 | 1 | The main thread of the application.|
906| ALL_THREADS                 | 2 | All threads of the application.|
907
908## GcStats<sup>12+</sup>
909
910type GcStats = Record&lt;string, number&gt;
911
912Key-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.
913
914System capability: SystemCapability.HiviewDFX.HiProfiler.HiDebug
915
916| Type     | Description                         |
917| -----------| ---------------------------- |
918| Record&lt;string, number&gt;     | Indicates the value is in **Record** key-value pair format.    |
919
920**GcStats** contain the following key values:
921
922| Name                    | Type  | Description                     |
923|-------------------------| ------ |------------------------- |
924| ark.gc.gc-count         | number |  Count of GC of the calling thread.|
925| ark.gc.gc-time          | number |  GC time triggered by the calling thread, in milliseconds.|
926| ark.gc.gc-bytes-allocated | number | Memory size allocated to the Ark VM of the calling thread, in bytes.|
927| ark.gc.gc-bytes-freed   | number | Memory freed by the GC of the calling thread, in bytes.|
928| ark.gc.fullgc-longtime-count | number |  Count of long fullGC of the calling thread.|
929
930## hidebug.isDebugState<sup>12+</sup>
931
932isDebugState(): boolean
933
934Obtains 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.
935
936**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
937
938**Return value**
939
940| Type | Description                     |
941| ------ | -------------------------- |
942| boolean | Whether an application process is being debugged.|
943
944**Example**
945
946```ts
947import { hidebug,hilog } from '@kit.PerformanceAnalysisKit';
948
949hilog.info(0x000, "testTag", "isDebugState = %{public}s", hidebug.isDebugState())
950```
951
952## hidebug.getGraphicsMemory<sup>14+</sup>
953
954getGraphicsMemory(): Promise&lt;number&gt;
955
956Obtains the size of the GPU memory. This API uses a promise to return the result.
957
958**Atomic service API**: This API can be used in atomic services since API version 14.
959
960**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
961
962**Return value**
963
964| Type                   | Description                          |
965|-----------------------|------------------------------|
966| Promise&lt;number&gt; | Size of the GPU memory, in KB.|
967
968**Error codes**
969
970| ID| Error Message|
971| ------- | ----------------------------------------------------------------- |
972| 11400104 | Failed to get the application memory due to a remote exception. |
973
974**Example**
975
976```ts
977import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
978import { BusinessError } from '@kit.BasicServicesKit';
979
980hidebug.getGraphicsMemory().then((ret: number) => {
981    hilog.info(0x000, "testTag", `graphicsMemory: ${ret}`)
982}).catch((error: BusinessError) => {
983    hilog.info(0x000, "testTag", `error code: ${error.code}, error msg: ${error.message}`);
984})
985```
986
987## hidebug.getGraphicsMemorySync<sup>14+</sup>
988
989getGraphicsMemorySync(): number
990
991Obtains the size of the GPU memory. This API uses a synchronous callback to return the result.
992
993**Note**: This API involves multiple cross-process communications and may have performance problems. The asynchronous API **getGraphicsMemory** is recommended.
994
995**Atomic service API**: This API can be used in atomic services since API version 14.
996
997**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
998
999**Return value**
1000
1001| Type | Description        |
1002| ------ |------------|
1003| number | Size of the GPU memory, in KB.|
1004
1005**Error codes**
1006
1007| ID| Error Message|
1008| ------- | ----------------------------------------------------------------- |
1009| 11400104 | Failed to get the application memory due to a remote exception. |
1010
1011**Example**
1012
1013```ts
1014import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
1015import { BusinessError } from '@kit.BasicServicesKit';
1016
1017try {
1018    hilog.info(0x000, "testTag", `graphicsMemory: ${hidebug.getGraphicsMemorySync()}`)
1019} catch (error) {
1020    hilog.info(0x000, "testTag", `error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
1021}
1022```
1023