• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.hidebug (HiDebug)
2
3The **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.
4
5> **NOTE**<br>
6> 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.
7
8
9## Modules to Import
10
11```js
12import hidebug from '@ohos.hidebug';
13```
14
15
16## hidebug.getNativeHeapSize
17
18getNativeHeapSize(): bigint
19
20Obtains the total heap memory size of this application.
21
22This API is defined but not implemented in OpenHarmony 3.1 Release.
23
24**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
25
26**Return value**
27
28| Type   | Description                 |
29| ------ | --------------------------- |
30| bigint | Total heap memory size of the application, in KB.|
31
32
33**Example**
34  ```js
35  let nativeHeapSize = hidebug.getNativeHeapSize();
36  ```
37
38
39## hidebug.getNativeHeapAllocatedSize
40
41getNativeHeapAllocatedSize(): bigint
42
43Obtains the allocated heap memory size of this application.
44
45This API is defined but not implemented in OpenHarmony 3.1 Release.
46
47**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
48
49**Return value**
50
51| Type  | Description                             |
52| ------ | --------------------------------- |
53| bigint | Allocated heap memory of the application, in KB.|
54
55
56**Example**
57  ```js
58  let nativeHeapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
59  ```
60
61
62## hidebug.getNativeHeapFreeSize
63
64getNativeHeapFreeSize(): bigint
65
66Obtains the free heap memory size of this application.
67
68This API is defined but not implemented in OpenHarmony 3.1 Release.
69
70**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
71
72**Return value**
73
74| Type  | Description                           |
75| ------ | ------------------------------- |
76| bigint | Free heap memory size of the application, in KB.|
77
78**Example**
79  ```js
80  let nativeHeapFreeSize = hidebug.getNativeHeapFreeSize();
81  ```
82
83
84## hidebug.getPss
85
86getPss(): bigint
87
88Obtains the size of the physical memory actually used by the application process.
89
90**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
91
92**Return value**
93
94| Type  | Description                     |
95| ------ | ------------------------- |
96| bigint | Size of the physical memory actually used by the application process, in KB.|
97
98**Example**
99  ```js
100  let pss = hidebug.getPss();
101  ```
102
103
104## hidebug.getSharedDirty
105
106getSharedDirty(): bigint
107
108Obtains the size of the shared dirty memory of a process.
109
110**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
111
112**Return value**
113
114| Type  | Description                      |
115| ------ | -------------------------- |
116| bigint | Size of the shared dirty memory of the process, in KB.|
117
118
119**Example**
120  ```js
121  let sharedDirty = hidebug.getSharedDirty();
122  ```
123
124## hidebug.getPrivateDirty<sup>9+<sup>
125
126getPrivateDirty(): bigint
127
128Obtains the size of the private dirty memory of a process.
129
130**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
131
132**Return value**
133
134| Type  | Description                      |
135| ------ | -------------------------- |
136| bigint | Size of the private dirty memory of the process, in KB.|
137
138
139**Example**
140  ```js
141  let privateDirty = hidebug.getPrivateDirty();
142  ```
143
144## hidebug.getCpuUsage<sup>9+<sup>
145
146getCpuUsage(): number
147
148Obtains the CPU usage of a process.
149
150For example, if the CPU usage is **50%**, **0.5** is returned.
151
152**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
153
154**Return value**
155
156| Type  | Description                      |
157| ------ | -------------------------- |
158| number | CPU usage of the process.|
159
160
161**Example**
162  ```js
163  let cpuUsage = hidebug.getCpuUsage();
164  ```
165
166## hidebug.startProfiling<sup>(deprecated)</sup>
167
168startProfiling(filename : string) : void
169
170> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.startJsCpuProfiling](#hidebugstartjscpuprofiling9) instead.
171
172Starts 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`.
173
174**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
175
176**Parameters**
177
178| Name  | Type  | Mandatory| Description                                                        |
179| -------- | ------ | ---- | ------------------------------------------------------------ |
180| 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`.|
181
182**Example**
183
184```js
185hidebug.startProfiling("cpuprofiler-20220216");
186// code block
187// ...
188// code block
189hidebug.stopProfiling();
190```
191
192
193
194## hidebug.stopProfiling<sup>(deprecated)</sup>
195
196stopProfiling() : void
197
198> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.stopJsCpuProfiling](#hidebugstopjscpuprofiling9) instead.
199
200Stops 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`.
201
202**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
203
204**Example**
205
206```js
207hidebug.startProfiling("cpuprofiler-20220216");
208// code block
209// ...
210// code block
211hidebug.stopProfiling();
212```
213
214## hidebug.dumpHeapData<sup>(deprecated)</sup>
215
216dumpHeapData(filename : string) : void
217
218> **NOTE**<br>This API is deprecated since API version 9. You are advised to use [hidebug.dumpJsHeapData](#hidebugdumpjsheapdata9) instead.
219
220Exports the heap data.
221
222**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
223
224**Parameters**
225
226| Name  | Type  | Mandatory| Description                                                        |
227| -------- | ------ | ---- | ------------------------------------------------------------ |
228| 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`.|
229
230**Example**
231
232```js
233hidebug.dumpHeapData("heap-20220216");
234```
235
236## hidebug.getServiceDump<sup>9+<sup>
237
238getServiceDump(serviceid : number, fd : number, args : Array\<string>) : void
239
240Obtains system service information.
241
242**Required permissions**: ohos.permission.DUMP
243
244**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
245
246**Parameters**
247
248| Name  | Type  | Mandatory| Description                                                        |
249| -------- | ------ | ---- | ------------------------------------------------------------ |
250| serviceid | number | Yes  | Obtains the system service information based on the specified service ID.|
251| fd | number | Yes  | File descriptor to which data is written by the API.|
252| args | Array\<string> | Yes  | Parameter list corresponding to the **Dump** API of the system service.|
253
254
255**Example**
256
257```js
258import fileio from '@ohos.fileio'
259import hidebug from '@ohos.hidebug'
260import featureAbility from '@ohos.ability.featureAbility'
261
262let context = featureAbility.getContext();
263context.getFilesDir().then((data) => {
264  var path = data + "/serviceInfo.txt"
265  console.info("output path: " + path)
266  let fd = fileio.openSync(path, 0o102, 0o666)
267  var serviceId = 10
268  var args = new Array("allInfo")
269  try {
270    hidebug.getServiceDump(serviceId, fd, args)
271  } catch (error) {
272    console.info(error.code)
273    console.info(error.message)
274  }
275  fileio.closeSync(fd);
276})
277```
278
279## hidebug.startJsCpuProfiling<sup>9+</sup>
280
281startJsCpuProfiling(filename : string) : void
282
283Starts 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`.
284
285**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
286
287**Parameters**
288
289| Name  | Type  | Mandatory| Description                                                        |
290| -------- | ------ | ---- | ------------------------------------------------------------ |
291| 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`.|
292
293**Example**
294
295```js
296import hidebug from '@ohos.hidebug'
297
298try {
299  hidebug.startJsCpuProfiling("cpu_profiling");
300  // ...
301  hidebug.stopJsCpuProfiling();
302} catch (error) {
303  console.info(error.code)
304  console.info(error.message)
305}
306```
307
308## hidebug.stopJsCpuProfiling<sup>9+</sup>
309
310stopJsCpuProfiling() : void
311
312Stops 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`.
313
314**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
315
316**Parameters**
317
318| Name  | Type  | Mandatory| Description                                                        |
319| -------- | ------ | ---- | ------------------------------------------------------------ |
320| 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`.|
321
322**Example**
323
324```js
325import hidebug from '@ohos.hidebug'
326
327try {
328  hidebug.startJsCpuProfiling("cpu_profiling");
329  // ...
330  hidebug.stopJsCpuProfiling();
331} catch (error) {
332  console.info(error.code)
333  console.info(error.message)
334}
335```
336
337## hidebug.dumpJsHeapData<sup>9+</sup>
338
339dumpJsHeapData(filename : string) : void
340
341Exports the heap data.
342
343**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
344
345**Parameters**
346
347| Name  | Type  | Mandatory| Description                                                        |
348| -------- | ------ | ---- | ------------------------------------------------------------ |
349| 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`.|
350
351**Example**
352
353```js
354import hidebug from '@ohos.hidebug'
355
356try {
357  hidebug.dumpJsHeapData("heapData");
358} catch (error) {
359  console.info(error.code)
360  console.info(error.message)
361}
362```
363