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应用调用startAppTraceCapture接口启动采集trace,当采集的trace大小超过了limitSize,系统将自动调用stopAppTraceCapture接口停止采集,因此limitSize大小设置不当,将导致采集trace数据不足,无法满足故障分析。所以要求开发者根据实际情况,评估limitSize大小。
482
483评估方法:limitSize = 预期trace采集时长 * trace单位流量。
484
485预期trace采集时长:开发者根据分析的故障场景自行决定,单位秒。
486
487trace单位流量:应用每秒产生的trace大小,系统推荐值为300Kb/s,建议开发者采用自身应用的实测值,单位Kb/秒。
488
489trace单位流量实测方法:limitSize设置为最大值500M,调用startAppTraceCapture接口,在应用上操作N秒后,调用stopAppTraceCapture停止采集,然后查看trace大小S Kb。那么trace单位流量 = S/N。
490
491**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
492
493**参数:**
494
495| 参数名   | 类型     | 必填 | 说明                                   |
496| -------- | ------   | ---- |--------------------------------------|
497| tags     | number[] | 是   | 详情请见[tags](#hidebugtags12)           |
498| flag     | TraceFlag| 是   | 详情请见[TraceFlag](#traceflag12)        |
499| limitSize| number   | 是   | 开启trace文件大小限制,单位为Byte,单个文件大小上限为500MB |
500
501**返回值:**
502
503| 类型             | 说明                                           |
504| -----------------| -----------------------------------------------|
505| string           | 返回trace文件名路径                            |
506
507**错误码:**
508
509以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
510
511| 错误码ID | 错误信息 |
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**示例:**
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
537停止应用trace采集,在停止采集前,需要通过'[startAppTraceCapture()](#hidebugstartapptracecapture12)'方法开始采集。
538
539先开启后关闭,严禁使用'start->start->stop','start->stop->stop','start->start->stop->stop'等类似的顺序调用。
540
541调用startAppTraceCapture接口,如果没有合理传入limitSize参数,生成trace的大小大于传入的limitSize大小,系统内部会自动调用stopAppTraceCapture,再次手动调用stopAppTraceCapture就会抛出错误码11400105。
542
543**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
544
545**错误码:**
546
547以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
548
549| 错误码ID | 错误信息 |
550| ------- | ----------------------------------------------------------------- |
551| 11400104 | The status of the trace is abnormal                                |
552| 11400105 |   No capture trace running                                       |
553
554**示例:**
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
573获取应用程序进程内存限制。
574
575**系统能力**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
576
577**返回值**
578
579| 类型  | 说明                      |
580| ------ | -------------------------- |
581| [MemoryLimit](#memorylimit12) | 应用程序进程内存限制|
582
583**示例**
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
595获取系统的CPU资源占用情况。
596
597例如,当系统资源CPU占用为 **50%**,将返回**0.5**。
598
599**系统能力**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
600
601**返回值**
602
603| 类型     | 说明          |
604|--------|-------------|
605| number | 系统CPU资源占用情况。|
606
607**错误码:**
608
609以下错误码的详细介绍请参见[Hidebug-CpuUsage错误码](errorcode-hiviewdfx-hidebug-cpuusage.md)。
610
611| 错误码ID | 错误信息                                            |
612| ------- |-------------------------------------------------|
613| 11400104 | The status of the system CPU usage is abnormal. |
614
615**示例**
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
626设置应用的fd数量、线程数量、js内存或者native内存资源限制。
627**注意:** 当设置的开发者选项开关打开并重启设备后,此功能有效。
628
629**原子化服务API:** 从API version 12开始,该接口支持在原子化服务中使用。
630
631**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
632
633**参数:**
634
635| 参数名   | 类型   | 必填 | 说明                                                         |
636| -------- | ------ | ---- | ------------------------------------------------------------ |
637| type | string |  是  | 泄漏资源类型,共四种类型:pss_memory(native内存)、js_heap(js堆内存)、fd(文件描述符)或thread(线程) |
638| value | number |  是  | 对应泄漏资源类型的最大值。范围:pss_memory类型`[1024, 4 * 1024 * 1024](单位:KB)`, js_heap类型`[85, 95](分配给JS堆内存上限的85%~95%)`, fd类型`[10, 10000]`, thread类型`[1, 1000]` |
639| enableDebugLog | boolean |  是  | 是否启用外部调试日志,默认值为false,请仅在灰度版本中设置为true,因为收集调试日志会花费太多的cpu或内存 |
640
641**错误码:**
642
643以下错误码的详细介绍请参见[Hidebug错误码](errorcode-hiviewdfx-hidebug.md)。
644
645| 错误码ID | 错误信息 |
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**示例:**
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
665获取应用进程内存信息。
666
667**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
668
669**返回值:**
670
671| 类型  | 说明                      |
672| ------ | -------------------------- |
673| [NativeMemInfo](#nativememinfo12) | 应用进程内存信息|
674
675**示例**
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
701获取系统内存信息。
702
703**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
704
705**返回值:**
706
707| 类型  | 说明                      |
708| ------ | -------------------------- |
709| [SystemMemInfo](#systemmeminfo12) | 系统内存信息|
710
711**示例**
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
729获取系统gc全部统计信息。
730
731**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
732
733**返回值:**
734
735| 类型                    | 说明       |
736|-----------------------|----------|
737| [GcStats](#gcstats12) | 系统GC统计信息。 |
738
739**示例**
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
756根据参数获取指定的系统gc统计信息。
757
758**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
759
760**参数:**
761
762| 参数名   | 类型   | 必填 | 说明          |
763| -------- | ------ | ---- |-------------|
764| item | string | 是   | 需要获取GC信息的类型。 |
765
766| 输入参数                         | 返回值说明          |
767|------------------------------|----------------|
768| ark.gc.gc-count | 当前线程的GC次数。     |
769| ark.gc.gc-time | 当前线程触发的GC总耗时,以ms为单位。 |
770| ark.gc.gc-bytes-allocated | 当前线程Ark虚拟机已分配的内存大小,以B为单位。|
771| ark.gc.gc-bytes-freed | 当前线程GC成功回收的内存,以B为单位。 |
772| ark.gc.fullgc-longtime-count | 当前线程超长fullGC次数。 |
773
774**错误码:**
775
776| 错误码ID | 错误信息                                                                                                       |
777| ------- |------------------------------------------------------------------------------------------------------------|
778| 401 | Possible causes:1. Invalid parameter, a string parameter required. 2. Invalid parameter, unknown property. |
779
780**示例**
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
794应用程序进程内存限制。
795
796**系统能力**:SystemCapability.HiviewDFX.HiProfiler.HiDebug
797
798| 名称      | 类型   | 必填 | 说明         |
799| --------- | ------ | ---- | ------------ |
800| rssLimit    | bigint |  是  | 应用程序进程的驻留集的限制,以KB为单位     |
801| vssLimit  | bigint |  是  | 进程的虚拟内存限制,以KB为单位       |
802| vmHeapLimit | bigint |  是  | 当前线程的 JS VM 堆大小限制,以KB为单位      |
803| vmTotalHeapSize | bigint |  是  | 当前进程的 JS 堆内存大小限制,以KB为单位      |
804
805## VMMemoryInfo<sup>12+</sup>
806
807描述VM内存信息。
808
809**系统能力:** 以下各项对应的系统能力均为SystemCapability.HiviewDFX.HiProfiler.HiDebug
810
811| 名称               | 类型    | 可读 | 可写 | 说明                                |
812| -------------------| ------- | ---- | ---- | ---------------------------------- |
813| totalHeap          | bigint  | 是   | 否   | 表示当前虚拟机的堆总大小,以KB为单位    |
814| heapUsed           | bigint  | 是   | 否   | 表示当前虚拟机使用的堆大小,以KB为单位  |
815| allArraySize       | bigint  | 是   | 否   | 表示当前虚拟机的所有数组对象大小,以KB为单位 |
816
817## ThreadCpuUsage<sup>12+</sup>
818
819描述线程CPU使用情况。
820
821**系统能力:** 以下各项对应的系统能力均为SystemCapability.HiviewDFX.HiProfiler.HiDebug
822
823| 名称               | 类型    | 可读 | 可写 | 说明                                |
824| -------------------| ------- | ---- | ---- | ----------------------------------- |
825| threadId           | number  | 是   | 否   | 线程号                           |
826| cpuUsage           | number  | 是   | 否   | 线程CPU使用率                       |
827
828## hidebug.tags<sup>12+</sup>
829
830描述支持/使用场景标签。
831
832**系统能力:** 以下各项对应的系统能力均为SystemCapability.HiviewDFX.HiProfiler.HiDebug
833
834| 名称                     | 类型    | 只读  |说明                                |
835| -------------------------| ------- |-----|----------------------------------- |
836| ABILITY_MANAGER          | number  | 是 |  能力管理标签                         |
837| ARKUI                    | number  | 是 |  ArkUI开发框架标签                    |
838| ARK                      | number  | 是 |  JSVM虚拟机标签                       |
839| BLUETOOTH                | number  | 是 |  蓝牙标签                            |
840| COMMON_LIBRARY           | number  | 是 |  公共库子系统标签                     |
841| DISTRIBUTED_HARDWARE_DEVICE_MANAGER | number  | 是 |  分布式硬件设备管理标签     |
842| DISTRIBUTED_AUDIO        | number  | 是 |        分布式音频标签                 |
843| DISTRIBUTED_CAMERA       | number  | 是 |  分布式相机标签                       |
844| DISTRIBUTED_DATA         | number  | 是 |  分布式数据管理模块标签                |
845| DISTRIBUTED_HARDWARE_FRAMEWORK | number  | 是 |  分布式硬件框架标签              |
846| DISTRIBUTED_INPUT        | number  | 是 |  分布式输入标签                       |
847| DISTRIBUTED_SCREEN       | number  | 是 |  分布式屏幕标签                       |
848| DISTRIBUTED_SCHEDULER    | number  | 是 |  分布式调度器标签                     |
849| FFRT                     | number  | 是 |  FFRT任务标签.                        |
850| FILE_MANAGEMENT          | number  | 是 |  文件管理系统标签                     |
851| GLOBAL_RESOURCE_MANAGER  | number  | 是 |  全局资源管理标签                     |
852| GRAPHICS                 | number  | 是 |  图形模块标签                        |
853| HDF                      | number  | 是 |  HDF子系统标签                       |
854| MISC                     | number  | 是 |  MISC模块标签                        |
855| MULTIMODAL_INPUT         | number  | 是 |  多模态输入模块标签                   |
856| NET                      | number  | 是 |  网络标签                             |
857| NOTIFICATION             | number  | 是 |  通知模块标签                         |
858| NWEB                     | number  | 是 |  Nweb标签                            |
859| OHOS                     | number  | 是 |  OHOS通用标签                         |
860| POWER_MANAGER            | number  | 是 |  电源管理标签                         |
861| RPC                      | number  | 是 |  RPC标签                             |
862| SAMGR                    | number  | 是 |  系统能力管理标签                     |
863| WINDOW_MANAGER           | number  | 是 |  窗口管理标签                         |
864| AUDIO                    | number  | 是 |  音频模块标签                        |
865| CAMERA                   | number  | 是 |  相机模块标签                        |
866| IMAGE                    | number  | 是 |  图片模块标签                        |
867| MEDIA                    | number  | 是 |  媒体模块标签                        |
868
869## NativeMemInfo<sup>12+</sup>
870
871描述应用进程内存信息。
872
873**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
874
875| 名称      | 类型   | 必填 | 说明         |
876| --------- | ------ | ---- | ------------ |
877| pss  | bigint |  是  | 实际占用的物理内存的大小(比例分配共享库占用的内存),以KB为单位     |
878| vss  | bigint |  是  | 占用虚拟内存大小(包括共享库所占用的内存),以KB为单位       |
879| rss  | bigint |  是  | 实际占用的物理内存的大小(包括共享库占用),以KB为单位         |
880| sharedDirty  | bigint |  是  | 共享脏内存的大小,以KB为单位      |
881| privateDirty  | bigint |  是  | 专用脏内存的大小,以KB为单位      |
882| sharedClean  | bigint |  是  | 共享干净内存的大小,以KB为单位      |
883| privateClean  | bigint |  是  | 专用干净内存的大小,以KB为单位      |
884
885## SystemMemInfo<sup>12+</sup>
886
887描述系统内存信息。
888
889**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
890
891| 名称      | 类型   | 必填 | 说明         |
892| --------- | ------ | ---- | ------------ |
893| totalMem  | bigint |  是  | 系统总的内存,以KB为单位     |
894| freeMem  | bigint |  是  | 系统空闲的内存,以KB为单位       |
895| availableMem  | bigint |  是  | 系统可用的内存,以KB为单位      |
896
897## TraceFlag<sup>12+</sup>
898
899描述采集trace线程的类型。
900
901**系统能力**:SystemCapability.HiviewDFX.HiProfiler.HiDebug
902
903| 名称                         | 值 | 说明                    |
904| --------------------------- |---| ----------------------- |
905| MAIN_THREAD                 | 1 | 只采集当前应用主线程。|
906| ALL_THREADS                 | 2 | 采集当前应用下所有线程。 |
907
908## GcStats<sup>12+</sup>
909
910type GcStats = Record&lt;string, number&gt;
911
912用于存储GC统计信息的键值对。该类型不是多线程安全的,如果应用中存在多线程同时操作该类派生出的实例,注意加锁保护。
913
914**系统能力:**  SystemCapability.HiviewDFX.HiProfiler.HiDebug
915
916| 类型      | 说明                          |
917| -----------| ---------------------------- |
918| Record&lt;string, number&gt;     | 表示值类型为Record键值对。     |
919
920其中GcStats中可包含的键值信息如下:
921
922| 参数名                     | 类型   | 说明                      |
923|-------------------------| ------ |------------------------- |
924| ark.gc.gc-count         | number |  当前线程的GC次数。|
925| ark.gc.gc-time          | number |  当前线程触发的GC总耗时,以ms为单位。 |
926| ark.gc.gc-bytes-allocated | number | 当前线程Ark虚拟机已分配的内存大小,以B为单位。 |
927| ark.gc.gc-bytes-freed   | number | 当前线程GC成功回收的内存,以B为单位。|
928| ark.gc.fullgc-longtime-count | number |  当前线程超长fullGC次数。 |
929
930## hidebug.isDebugState<sup>12+</sup>
931
932isDebugState(): boolean
933
934获取应用进程被调试状态,如果应用进程的ark层或者native层处于被调试状态,则返回true,否则返回false。
935
936**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
937
938**返回值:**
939
940| 类型  | 说明                      |
941| ------ | -------------------------- |
942| boolean | 应用进程被调试状态|
943
944**示例**
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
956使用异步方式,获取应用显存大小。
957
958**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
959
960**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
961
962**返回值:**
963
964| 类型                    | 说明                           |
965|-----------------------|------------------------------|
966| Promise&lt;number&gt; | promise对象,调用结束后返回应用显存大小,单位KB |
967
968**错误码:**
969
970| 错误码ID | 错误信息 |
971| ------- | ----------------------------------------------------------------- |
972| 11400104 | Failed to get the application memory due to a remote exception. |
973
974**示例**
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
991使用同步方式,获取应用显存大小。
992
993**注意:** 该接口涉及多次跨进程通信,可能存在性能问题,推荐使用异步接口getGraphicsMemory。
994
995**原子化服务API:** 从API version 14开始,该接口支持在原子化服务中使用。
996
997**系统能力:** SystemCapability.HiviewDFX.HiProfiler.HiDebug
998
999**返回值:**
1000
1001| 类型  | 说明         |
1002| ------ |------------|
1003| number | 应用显存大小(KB) |
1004
1005**错误码:**
1006
1007| 错误码ID | 错误信息 |
1008| ------- | ----------------------------------------------------------------- |
1009| 11400104 | Failed to get the application memory due to a remote exception. |
1010
1011**示例**
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```