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