1# @ohos.hidebug (Debug调试)
2
3> **说明:**
4>
5> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
6
7使用hidebug,可以获取应用内存的使用情况,包括应用进程的静态堆内存(native heap)信息、应用进程内存占用PSS(Proportional Set Size)信息等;可以完成虚拟机内存切片导出,虚拟机CPU Profiling采集等操作。
8
9## 导入模块
10
11```ts
12import { hidebug } from '@kit.PerformanceAnalysisKit';
13```
14
15## hidebug.getNativeHeapSize
16
17getNativeHeapSize(): bigint
18
19获取内存分配器统计的进程持有的堆内存大小(含分配器元数据)。
20
21**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
22
23**返回值:**
24
25| 类型   | 说明                        |
26| ------ | --------------------------- |
27| bigint | 内存分配器统计的进程持有的堆内存大小(含分配器元数据),单位为Byte。 |
28
29**示例:**
30
31```ts
32import { hidebug } from '@kit.PerformanceAnalysisKit';
33
34let nativeHeapSize: bigint = hidebug.getNativeHeapSize();
35```
36
37## hidebug.getNativeHeapAllocatedSize
38
39getNativeHeapAllocatedSize(): bigint
40
41获取内存分配器统计的进程业务分配的堆内存大小。
42
43**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
44
45**返回值:**
46
47| 类型   | 说明                              |
48| ------ | --------------------------------- |
49| bigint | 返回内存分配器统计的进程业务分配的堆内存大小,单位为Byte。 |
50
51
52**示例:**
53```ts
54import { hidebug } from '@kit.PerformanceAnalysisKit';
55
56let nativeHeapAllocatedSize: bigint = hidebug.getNativeHeapAllocatedSize();
57```
58
59## hidebug.getNativeHeapFreeSize
60
61getNativeHeapFreeSize(): bigint
62
63获取内存分配器持有的缓存内存大小。
64
65**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
66
67**返回值:**
68
69| 类型   | 说明                            |
70| ------ | ------------------------------- |
71| bigint | 返回内存分配器持有的缓存内存大小,单位为Byte。 |
72
73**示例:**
74```ts
75import { hidebug } from '@kit.PerformanceAnalysisKit';
76
77let nativeHeapFreeSize: bigint = hidebug.getNativeHeapFreeSize();
78```
79
80## hidebug.getPss
81
82getPss(): bigint
83
84获取应用进程实际使用的物理内存大小。
85
86**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
87
88**返回值:**
89
90| 类型   | 说明                      |
91| ------ | ------------------------- |
92| bigint | 返回应用进程实际使用的物理内存大小,单位为KB。 |
93
94**示例:**
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
105获取应用进程虚拟耗用内存大小。
106
107**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
108
109**返回值:**
110
111| 类型   | 说明                                     |
112| ------ | ---------------------------------------- |
113| bigint | 返回应用进程虚拟耗用内存大小,单位为KB。 |
114
115**示例:**
116
117```ts
118import { hidebug } from '@kit.PerformanceAnalysisKit';
119
120let vss: bigint = hidebug.getVss();
121```
122
123## hidebug.getSharedDirty
124
125getSharedDirty(): bigint
126
127获取进程的共享脏内存大小。
128
129**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
130
131**返回值:**
132
133| 类型   | 说明                       |
134| ------ | -------------------------- |
135| bigint | 返回进程的共享脏内存大小,单位为KB。 |
136
137
138**示例:**
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
149获取进程的私有脏内存大小。
150
151**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
152
153**返回值:**
154
155| 类型   | 说明                       |
156| ------ | -------------------------- |
157| bigint | 返回进程的私有脏内存大小,单位为KB。 |
158
159**示例:**
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
170获取进程的CPU使用率。
171
172如占用率为50%,则返回0.5。
173
174**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
175
176**返回值:**
177
178| 类型   | 说明                       |
179| ------ | -------------------------- |
180| number | 获取进程的CPU使用率。 |
181
182
183**示例:**
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
194获取系统服务信息。
195
196**需要权限**: ohos.permission.DUMP,仅系统应用可申请。
197
198**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
199
200**参数:**
201
202| 参数名   | 类型   | 必填 | 说明                                                         |
203| -------- | ------ | ---- | ------------------------------------------------------------ |
204| serviceid | number | 是   | 基于该用户输入的service id获取系统服务信息。|
205| fd | number | 是   | 文件描述符,该接口会往该fd中写入数据。|
206| args | Array\<string> | 是   | 系统服务的Dump接口所对应的参数列表。|
207
208**错误码:**
209
210以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
211
212| 错误码ID | 错误信息 |
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**示例:**
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
252启动虚拟机Profiling方法跟踪,`startJsCpuProfiling()`方法的调用需要与`stopJsCpuProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
253
254**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
255
256**参数:**
257
258| 参数名   | 类型   | 必填 | 说明                                                         |
259| -------- | ------ | ---- | ------------------------------------------------------------ |
260| filename | string | 是   | 用户自定义的profiling文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.json`文件。 |
261
262**错误码:**
263
264以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
265
266| 错误码ID | 错误信息 |
267| ------- | ----------------------------------------------------------------- |
268| 401 | the parameter check failed,Parameter type error                        |
269
270**示例:**
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
289停止虚拟机Profiling方法跟踪,`startJsCpuProfiling()`方法的调用需要与`stopJsCpuProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
290
291**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
292
293**示例:**
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
312虚拟机堆导出。
313
314**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
315
316**参数:**
317
318| 参数名   | 类型   | 必填 | 说明                                                         |
319| -------- | ------ | ---- | ------------------------------------------------------------ |
320| filename | string | 是   | 用户自定义的虚拟机堆文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.heapsnapshot`文件。 |
321
322**错误码:**
323
324以下错误码的详细介绍请参见[通用错误码](../errorcode-universal.md)。
325
326| 错误码ID | 错误信息 |
327| ------- | ----------------------------------------------------------------- |
328| 401 | the parameter check failed, Parameter type error                      |
329
330**示例:**
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> **说明:**
348> 从 API Version 9 开始废弃,建议使用[hidebug.startJsCpuProfiling](#hidebugstartjscpuprofiling9)替代。
349
350启动虚拟机Profiling方法跟踪,`startProfiling()`方法的调用需要与`stopProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
351
352**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
353
354**参数:**
355
356| 参数名   | 类型   | 必填 | 说明                                                         |
357| -------- | ------ | ---- | ------------------------------------------------------------ |
358| filename | string | 是   | 用户自定义的profiling文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.json`文件。 |
359
360**示例:**
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> **说明:**
377> 从 API Version 9 开始废弃,建议使用[hidebug.stopJsCpuProfiling](#hidebugstopjscpuprofiling9)替代。
378
379停止虚拟机Profiling方法跟踪,`stopProfiling()`方法的调用需要与`startProfiling()`方法的调用一一对应,先开启后关闭,严禁使用`start->start->stop`,`start->stop->stop`,`start->start->stop->stop`等类似的顺序调用。
380
381**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
382
383**示例:**
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> **说明:**
400> 从 API Version 9 开始废弃,建议使用[hidebug.dumpJsHeapData](#hidebugdumpjsheapdata9)替代。
401
402虚拟机堆导出。
403
404**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
405
406**参数:**
407
408| 参数名   | 类型   | 必填 | 说明                                                         |
409| -------- | ------ | ---- | ------------------------------------------------------------ |
410| filename | string | 是   | 用户自定义的虚拟机堆文件名,根据传入的`filename`,将在应用的`files`目录生成`filename.heapsnapshot`文件。 |
411
412**示例:**
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
424获取VM内存相关信息。
425
426**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
427
428**返回值:**
429
430| 类型         | 说明                                    |
431| -------------| --------------------------------------- |
432| [VMMemoryInfo](#vmmemoryinfo12) |  返回VM内存信息  |
433
434**示例:**
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
449获取应用线程CPU使用情况。
450
451**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
452
453**返回值:**
454
455| 类型             | 说明                                                        |
456| -----------------| ------------------------------------------------------------|
457| [ThreadCpuUsage](#threadcpuusage12)[] | 返回当前应用进程下所有ThreadCpuUsage数组 |
458
459
460
461**示例:**
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
477启动应用trace采集,'startAppTraceCapture()'方法的调用需要与'[stopAppTraceCapture()](#hidebugstopapptracecapture12)'方法的调用一一对应。
478
479先开启后关闭,严禁使用'start->start->stop','start->stop->stop','start->start->stop->stop'等类似的顺序调用。
480
481**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
482
483**参数:**
484
485| 参数名   | 类型     | 必填 | 说明                                                                                  |
486| -------- | ------   | ---- | ------------------------------------------------------------------------------------- |
487| tags     | number[] | 是   | 详情请见[tags](#tags12)                                                      |
488| flag     | TraceFlag| 是   | 详情请见[TraceFlag](#traceflag12)          |
489| limitSize| number   | 是   | 开启trace文件大小限制,单位为Byte,单个文件大小上限为500MB                                                       |
490
491**返回值:**
492
493| 类型             | 说明                                           |
494| -----------------| -----------------------------------------------|
495| string           | 返回trace文件名路径                            |
496
497**错误码:**
498
499以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
500
501| 错误码ID | 错误信息 |
502| ------- | ----------------------------------------------------------------- |
503| 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|
504| 11400102 | Capture trace already enabled.                                         |
505| 11400103 | No write permission on the file.                                |
506| 11400104 | Abnormal trace status.                                 |
507
508**示例:**
509
510```ts
511import { hidebug } from '@kit.PerformanceAnalysisKit';
512
513let tags: number[] = [hidebug.tags.ABILITY_MANAGER, hidebug.tags.ARKUI];
514let flag: hidebug.TraceFlag = hidebug.TraceFlag.MAIN_THREAD;
515let limitSize: number = 1024 * 1024;
516let fileName: string = hidebug.startAppTraceCapture(tags, flag, limitSize);
517// code block
518// ...
519// code block
520hidebug.stopAppTraceCapture();
521```
522
523## hidebug.stopAppTraceCapture<sup>12+</sup>
524
525stopAppTraceCapture() : void
526
527停止应用trace采集,在停止采集前,需要通过'[startAppTraceCapture()](#hidebugstartapptracecapture12)'方法开始采集。
528
529先开启后关闭,严禁使用'start->start->stop','start->stop->stop','start->start->stop->stop'等类似的顺序调用。
530
531**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
532
533**错误码:**
534
535以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
536
537| 错误码ID | 错误信息 |
538| ------- | ----------------------------------------------------------------- |
539| 11400104 | The status of the trace is abnormal                                |
540| 11400105 |   No capture trace running                                       |
541
542**示例:**
543
544```ts
545import { hidebug } from '@kit.PerformanceAnalysisKit';
546
547let tags: number[] = [hidebug.tags.ABILITY_MANAGER, hidebug.tags.ARKUI];
548let flag: hidebug.TraceFlag = hidebug.TraceFlag.MAIN_THREAD;
549let limitSize: number = 1024 * 1024;
550let fileName: string = hidebug.startAppTraceCapture(tags, flag, limitSize);
551// code block
552// ...
553// code block
554hidebug.stopAppTraceCapture();
555```
556
557## hidebug.getAppMemoryLimit<sup>12+</sup>
558
559getAppMemoryLimit() : MemoryLimit
560
561获取应用程序进程内存限制。
562
563**系统能力**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
564
565**返回值**
566
567| 类型  | 说明                      |
568| ------ | -------------------------- |
569| [MemoryLimit](#memorylimit12) | 应用程序进程内存限制|
570
571**示例**
572
573```ts
574import { hidebug } from '@kit.PerformanceAnalysisKit';
575
576let appMemoryLimit:hidebug.MemoryLimit = hidebug.getAppMemoryLimit();
577```
578
579## hidebug.getSystemCpuUsage<sup>12+</sup>
580
581getSystemCpuUsage() : number
582
583获取系统的CPU资源占用情况。
584
585例如,当系统资源CPU占用为 **50%**,将返回**0.5**。
586
587**系统能力**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
588
589**返回值**
590
591| 类型     | 说明          |
592|--------|-------------|
593| number | 系统CPU资源占用情况。|
594
595**错误码:**
596
597以下错误码的详细介绍请参见[Hidebug-CpuUsage错误码](errorcode-hiviewdfx-hidebug-cpuusage.md)。
598
599| 错误码ID | 错误信息                                            |
600| ------- |-------------------------------------------------|
601| 11400104 | The status of the system CPU usage is abnormal. |
602
603**示例**
604```ts
605import { hidebug } from '@kit.PerformanceAnalysisKit';
606
607let cpuUsage: number = hidebug.getSystemCpuUsage();
608```
609
610## hidebug.setAppResourceLimit<sup>12+</sup>
611
612setAppResourceLimit(type: string, value: number, enableDebugLog: boolean) : void
613
614设置应用的fd数量、线程数量、js内存或者native内存资源限制。
615**注意:** 当设置的开发者选项开关打开时,此功能有效。
616
617**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
618
619**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
620
621**参数:**
622
623| 参数名   | 类型   | 必填 | 说明                                                         |
624| -------- | ------ | ---- | ------------------------------------------------------------ |
625| type | string |  是  | 泄漏资源类型,共四种类型:pss_memory(native内存)、js_heap(js堆内存)、fd(文件描述符)或thread(线程) |
626| value | number |  是  | 对应泄漏资源类型的最大值。范围:pss_memory类型`[1024, 4 * 1024 * 1024](单位:KB)`, js_heap类型`[85, 95](分配给JS堆内存上限的85%~95%)`, fd类型`[10, 10000]`, thread类型`[1, 1000]` |
627| enableDebugLog | boolean |  是  | 是否启用外部调试日志,默认值为false,请仅在灰度版本中设置为true,因为收集调试日志会花费太多的cpu或内存 |
628
629**错误码:**
630
631以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
632
633| 错误码ID | 错误信息 |
634| ------- | ----------------------------------------------------------------- |
635| 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  |
636| 11400104 | Set limit failed due to remote exception |
637
638**示例:**
639
640```ts
641import { hidebug } from '@kit.PerformanceAnalysisKit';
642
643let type: string = 'js_heap';
644let value: number = 85;
645let enableDebugLog: boolean = false;
646hidebug.setAppResourceLimit(type, value, enableDebugLog);
647```
648
649## hidebug.getAppNativeMemInfo<sup>12+</sup>
650
651getAppNativeMemInfo(): NativeMemInfo
652
653获取应用进程内存信息。
654
655**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
656
657**返回值:**
658
659| 类型  | 说明                      |
660| ------ | -------------------------- |
661| [NativeMemInfo](#nativememinfo12) | 应用进程内存信息|
662
663**示例**
664
665```ts
666import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
667
668let nativeMemInfo: hidebug.NativeMemInfo = hidebug.getAppNativeMemInfo();
669
670hilog.info(0x0000, 'testTag', "pss = %{public}d", nativeMemInfo.pss);
671
672hilog.info(0x0000, 'testTag', "vss = %{public}d", nativeMemInfo.vss);
673
674hilog.info(0x0000, 'testTag', "rss = %{public}d", nativeMemInfo.rss);
675
676hilog.info(0x0000, 'testTag', "sharedDirty = %{public}d", nativeMemInfo.sharedDirty);
677
678hilog.info(0x0000, 'testTag', "privateDirty = %{public}d", nativeMemInfo.privateDirty);
679
680hilog.info(0x0000, 'testTag', "sharedClean = %{public}d", nativeMemInfo.sharedClean);
681
682hilog.info(0x0000, 'testTag', "privateClean = %{public}d", nativeMemInfo.privateClean);
683```
684
685## hidebug.getSystemMemInfo<sup>12+</sup>
686
687getSystemMemInfo(): SystemMemInfo
688
689获取系统内存信息。
690
691**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
692
693**返回值:**
694
695| 类型  | 说明                      |
696| ------ | -------------------------- |
697| [SystemMemInfo](#systemmeminfo12) | 系统内存信息|
698
699**示例**
700
701```ts
702import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
703
704let systemMemInfo: hidebug.SystemMemInfo = hidebug.getSystemMemInfo();
705
706hilog.info(0x0000, 'testTag', "totalMem = %{public}d", systemMemInfo.totalMem);
707
708hilog.info(0x0000, 'testTag', "freeMem = %{public}d", systemMemInfo.freeMem);
709
710hilog.info(0x0000, 'testTag', "availableMem = %{public}d", systemMemInfo.availableMem);
711```
712
713## hidebug.getVMRuntimeStats<sup>12+</sup>
714
715getVMRuntimeStats(): GcStats
716
717获取系统gc全部统计信息。
718
719**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
720
721**返回值:**
722
723| 类型                    | 说明       |
724|-----------------------|----------|
725| [GcStats](#gcstats12) | 系统GC统计信息。 |
726
727**示例**
728
729```ts
730import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
731
732let vMRuntimeStats: hidebug.GcStats = hidebug.getVMRuntimeStats();
733hilog.info(0x0000, "testTag", `gc-count: ${vMRuntimeStats['ark.gc.gc-count']}`);
734hilog.info(0x0000, "testTag", `gc-time: ${vMRuntimeStats['ark.gc.gc-time']}`);
735hilog.info(0x0000, "testTag", `gc-bytes-allocated: ${vMRuntimeStats['ark.gc.gc-bytes-allocated']}`);
736hilog.info(0x0000, "testTag", `gc-bytes-freed: ${vMRuntimeStats['ark.gc.gc-bytes-freed']}`);
737hilog.info(0x0000, "testTag", `fullgc-longtime-count: ${vMRuntimeStats['ark.gc.fullgc-longtime-count']}`);
738```
739
740## hidebug.getVMRuntimeStat<sup>12+</sup>
741
742getVMRuntimeStat(item : string): number
743
744根据参数获取指定的系统gc统计信息。
745
746**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
747
748**参数:**
749
750| 参数名   | 类型   | 必填 | 说明          |
751| -------- | ------ | ---- |-------------|
752| item | string | 是   | 需要获取GC信息的类型。 |
753
754| 输入参数                         | 返回值说明          |
755|------------------------------|----------------|
756| ark.gc.gc-count | 当前线程的GC次数。     |
757| ark.gc.gc-time | 当前线程触发的GC总耗时,以ms为单位。 |
758| ark.gc.gc-bytes-allocated | 当前线程Ark虚拟机已分配的内存大小,以B为单位。|
759| ark.gc.gc-bytes-freed | 当前线程GC成功回收的内存,以B为单位。 |
760| ark.gc.fullgc-longtime-count | 当前线程超长fullGC次数。 |
761
762**错误码:**
763
764| 错误码ID | 错误信息                                                                                                       |
765| ------- |------------------------------------------------------------------------------------------------------------|
766| 401 | Possible causes:1. Invalid parameter, a string parameter required. 2. Invalid parameter, unknown property. |
767
768**示例**
769
770```ts
771import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
772
773hilog.info(0x0000, "testTag", `gc-count: ${hidebug.getVMRuntimeStat('ark.gc.gc-count')}`);
774hilog.info(0x0000, "testTag", `gc-time: ${hidebug.getVMRuntimeStat('ark.gc.gc-time')}`);
775hilog.info(0x0000, "testTag", `gc-bytes-allocated: ${hidebug.getVMRuntimeStat('ark.gc.gc-bytes-allocated')}`);
776hilog.info(0x0000, "testTag", `gc-bytes-freed: ${hidebug.getVMRuntimeStat('ark.gc.gc-bytes-freed')}`);
777hilog.info(0x0000, "testTag", `fullgc-longtime-count: ${hidebug.getVMRuntimeStat('ark.gc.fullgc-longtime-count')}`);
778```
779
780## MemoryLimit<sup>12+</sup>
781
782应用程序进程内存限制。
783
784**系统能力**:SystemCapability.HiviewDFX.HiProfiler.HiDebug
785
786| 名称      | 类型   | 必填 | 说明         |
787| --------- | ------ | ---- | ------------ |
788| rssLimit    | bigint |  是  | 应用程序进程的驻留集的限制,以KB为单位     |
789| vssLimit  | bigint |  是  | 进程的虚拟内存限制,以KB为单位       |
790| vmHeapLimit | bigint |  是  | 当前线程的 JS VM 堆大小限制,以KB为单位      |
791| vmTotalHeapSize | bigint |  是  | 当前进程的 JS 堆内存大小限制,以KB为单位      |
792
793## VMMemoryInfo<sup>12+</sup>
794
795描述VM内存信息。
796
797**系统能力:** 以下各项对应的系统能力均为SystemCapability.HiviewDFX.HiProfiler.HiDebug
798
799| 名称               | 类型    | 可读 | 可写 | 说明                                |
800| -------------------| ------- | ---- | ---- | ---------------------------------- |
801| totalHeap          | bigint  | 是   | 否   | 表示当前虚拟机的堆总大小,以KB为单位    |
802| heapUsed           | bigint  | 是   | 否   | 表示当前虚拟机使用的堆大小,以KB为单位  |
803| allArraySize       | bigint  | 是   | 否   | 表示当前虚拟机的所有数组对象大小,以KB为单位 |
804
805## ThreadCpuUsage<sup>12+</sup>
806
807描述线程CPU使用情况。
808
809**系统能力:** 以下各项对应的系统能力均为SystemCapability.HiviewDFX.HiProfiler.HiDebug
810
811| 名称               | 类型    | 可读 | 可写 | 说明                                |
812| -------------------| ------- | ---- | ---- | ----------------------------------- |
813| threadId           | number  | 是   | 否   | 线程号                           |
814| cpuUsage           | number  | 是   | 否   | 线程CPU使用率                       |
815
816## tags<sup>12+</sup>
817
818描述支持/使用场景标签。
819
820**系统能力:** 以下各项对应的系统能力均为SystemCapability.HiviewDFX.HiProfiler.HiDebug
821
822| 名称                     | 类型    |  说明                                |
823| -------------------------| ------- |  ----------------------------------- |
824| ABILITY_MANAGER          | number  |  能力管理标签                         |
825| ARKUI                    | number  |  ArkUI开发框架标签                    |
826| ARK                      | number  |  JSVM虚拟机标签                       |
827| BLUETOOTH                | number  |  蓝牙标签                            |
828| COMMON_LIBRARY           | number  |  公共库子系统标签                     |
829| DISTRIBUTED_HARDWARE_DEVICE_MANAGER | number  |  分布式硬件设备管理标签     |
830| DISTRIBUTED_AUDIO        | number  |        分布式音频标签                 |
831| DISTRIBUTED_CAMERA       | number  |  分布式相机标签                       |
832| DISTRIBUTED_DATA         | number  |  分布式数据管理模块标签                |
833| DISTRIBUTED_HARDWARE_FRAMEWORK | number  |  分布式硬件框架标签              |
834| DISTRIBUTED_INPUT        | number  |  分布式输入标签                       |
835| DISTRIBUTED_SCREEN       | number  |  分布式屏幕标签                       |
836| DISTRIBUTED_SCHEDULER    | number  |  分布式调度器标签                     |
837| FFRT                     | number  |  FFRT任务标签.                        |
838| FILE_MANAGEMENT          | number  |  文件管理系统标签                     |
839| GLOBAL_RESOURCE_MANAGER  | number  |  全局资源管理标签                     |
840| GRAPHICS                 | number  |  图形模块标签                        |
841| HDF                      | number  |  HDF子系统标签                       |
842| MISC                     | number  |  MISC模块标签                        |
843| MULTIMODAL_INPUT         | number  |  多模态输入模块标签                   |
844| NET                      | number  |  网络标签                             |
845| NOTIFICATION             | number  |  通知模块标签                         |
846| NWEB                     | number  |  Nweb标签                            |
847| OHOS                     | number  |  OHOS通用标签                         |
848| POWER_MANAGER            | number  |  电源管理标签                         |
849| RPC                      | number  |  RPC标签                             |
850| SAMGR                    | number  |  系统能力管理标签                     |
851| WINDOW_MANAGER           | number  |  窗口管理标签                         |
852| AUDIO                    | number  |  音频模块标签                        |
853| CAMERA                   | number  |  相机模块标签                        |
854| IMAGE                    | number  |  图片模块标签                        |
855| MEDIA                    | number  |  媒体模块标签                        |
856
857## NativeMemInfo<sup>12+</sup>
858
859描述应用进程内存信息。
860
861**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
862
863| 名称      | 类型   | 必填 | 说明         |
864| --------- | ------ | ---- | ------------ |
865| pss  | bigint |  是  | 实际占用的物理内存的大小(比例分配共享库占用的内存),以KB为单位     |
866| vss  | bigint |  是  | 占用虚拟内存大小(包括共享库所占用的内存),以KB为单位       |
867| rss  | bigint |  是  | 实际占用的物理内存的大小(包括共享库占用),以KB为单位         |
868| sharedDirty  | bigint |  是  | 共享脏内存的大小,以KB为单位      |
869| privateDirty  | bigint |  是  | 专用脏内存的大小,以KB为单位      |
870| sharedClean  | bigint |  是  | 共享干净内存的大小,以KB为单位      |
871| privateClean  | bigint |  是  | 专用干净内存的大小,以KB为单位      |
872
873## SystemMemInfo<sup>12+</sup>
874
875描述系统内存信息。
876
877**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
878
879| 名称      | 类型   | 必填 | 说明         |
880| --------- | ------ | ---- | ------------ |
881| totalMem  | bigint |  是  | 系统总的内存,以KB为单位     |
882| freeMem  | bigint |  是  | 系统空闲的内存,以KB为单位       |
883| availableMem  | bigint |  是  | 系统可用的内存,以KB为单位      |
884
885## TraceFlag<sup>12+</sup>
886
887描述采集trace线程的类型。
888
889**系统能力**:SystemCapability.HiviewDFX.HiProfiler.HiDebug
890
891| 名称                         | 值 | 说明                    |
892| --------------------------- |---| ----------------------- |
893| MAIN_THREAD                 | 1 | 只采集当前应用主线程。|
894| ALL_THREADS                 | 2 | 采集当前应用下所有线程。 |
895
896## GcStats<sup>12+</sup>
897
898type GcStats = Record&lt;string, number&gt;
899
900用于存储GC统计信息的键值对。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。
901
902**系统能力:**  SystemCapability.HiviewDFX.HiProfiler.HiDebug
903
904| 类型      | 说明                          |
905| -----------| ---------------------------- |
906| Record&lt;string, number&gt;     | 表示值类型为Record键值对。     |
907
908其中GcStats中可包含的键值信息如下:
909
910| 参数名                     | 类型   | 说明                      |
911|-------------------------| ------ |------------------------- |
912| ark.gc.gc-count         | number |  当前线程的GC次数。|
913| ark.gc.gc-time          | number |  当前线程触发的GC总耗时,以ms为单位。 |
914| ark.gc.gc-bytes-allocated | number | 当前线程Ark虚拟机已分配的内存大小,以B为单位。 |
915| ark.gc.gc-bytes-freed   | number | 当前线程GC成功回收的内存,以B为单位。|
916| ark.gc.fullgc-longtime-count | number |  当前线程超长fullGC次数。 |
917
918## hidebug.isDebugState<sup>12+</sup>
919
920isDebugState(): boolean
921
922获取应用进程被调试状态,如果应用进程的ark层或者native层处于被调试状态,则返回true,否则返回false。
923
924**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
925
926**返回值:**
927
928| 类型  | 说明                      |
929| ------ | -------------------------- |
930| boolean | 应用进程被调试状态|
931
932**示例**
933
934```ts
935import { hidebug,hilog } from '@kit.PerformanceAnalysisKit';
936
937hilog.info(0x000, "testTag", "isDebugState = %{public}s", hidebug.isDebugState())
938```
939
940## hidebug.getGraphicsMemory<sup>13+</sup>
941
942getGraphicsMemory(): Promise&lt;number&gt;
943
944使用异步方式,获取应用显存大小。
945
946**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
947
948**返回值:**
949
950| 类型                    | 说明                           |
951|-----------------------|------------------------------|
952| Promise&lt;number&gt; | promise对象,调用结束后返回应用显存大小,单位KB |
953
954**错误码:**
955
956| 错误码ID | 错误信息 |
957| ------- | ----------------------------------------------------------------- |
958| 11400104 | Failed to get the application memory due to a remote exception. |
959
960**示例**
961
962```ts
963import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
964import { BusinessError } from '@kit.BasicServicesKit';
965
966hidebug.getGraphicsMemory().then((ret: number) => {
967    hilog.info(0x000, "testTag", `graphicsMemory: ${ret}`)
968}).catch((error: BusinessError) => {
969    hilog.info(0x000, "testTag", `error code: ${error.code}, error msg: ${error.message}`);
970})
971```
972
973## hidebug.getGraphicsMemorySync<sup>13+</sup>
974
975getGraphicsMemorySync(): number
976
977使用同步方式,获取应用显存大小。
978
979**注意:** 该接口涉及多次跨进程通信,可能存在性能问题,推荐使用异步接口getGraphicsMemory。
980
981**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
982
983**返回值:**
984
985| 类型  | 说明         |
986| ------ |------------|
987| number | 应用显存大小(KB) |
988
989**错误码:**
990
991| 错误码ID | 错误信息 |
992| ------- | ----------------------------------------------------------------- |
993| 11400104 | Failed to get the application memory due to a remote exception. |
994
995**示例**
996
997```ts
998import { hidebug, hilog } from '@kit.PerformanceAnalysisKit';
999import { BusinessError } from '@kit.BasicServicesKit';
1000
1001try {
1002    hilog.info(0x000, "testTag", `graphicsMemory: ${hidebug.getGraphicsMemorySync()}`)
1003} catch (error) {
1004    hilog.info(0x000, "testTag", `error code: ${(error as BusinessError).code}, error msg: ${(error as BusinessError).message}`);
1005}
1006```