js-apis-hidebug.md 4.6 KB
Newer Older
S
shawn_he 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# HiDebug

> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**
> 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.

You can run the hidebug command 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.

## Modules to Import

```
import hidebug from '@ohos.hidebug';
```


## hidebug.getNativeHeapSize

getNativeHeapSize(): bigint

Obtains the total size of the native heap memory.

S
shawn_he 已提交
21
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
22

S
shawn_he 已提交
23
**Return Value**
S
shawn_he 已提交
24

S
shawn_he 已提交
25 26 27
| Type  | Description                       |
| ------ | --------------------------- |
| bigint | Total size of the native heap memory.|
S
shawn_he 已提交
28

S
shawn_he 已提交
29 30

**Example**
S
shawn_he 已提交
31 32 33 34 35 36 37 38 39 40 41
  ```
  let nativeHeapSize = hidebug.getNativeHeapSize();
  ```


## hidebug.getNativeHeapAllocatedSize

getNativeHeapAllocatedSize(): bigint

Obtains the size of the allocated native heap memory.

S
shawn_he 已提交
42
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
43 44


S
shawn_he 已提交
45 46 47 48
**Return Value**
| Type  | Description                             |
| ------ | --------------------------------- |
| bigint | Size of the allocated native heap memory.|
S
shawn_he 已提交
49

S
shawn_he 已提交
50 51

**Example**
S
shawn_he 已提交
52 53 54 55 56 57 58 59 60 61 62
  ```
  let nativeHeapAllocatedSize = hidebug.getNativeHeapAllocatedSize();
  ```


## hidebug.getNativeHeapFreeSize

getNativeHeapFreeSize(): bigint

Obtains the size of the free native heap memory.

S
shawn_he 已提交
63
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
64 65


S
shawn_he 已提交
66 67 68 69
**Return Value**
| Type  | Description                           |
| ------ | ------------------------------- |
| bigint | Size of the free native heap memory.|
S
shawn_he 已提交
70

S
shawn_he 已提交
71 72

**Example**
S
shawn_he 已提交
73 74 75 76 77 78 79 80 81 82 83
  ```
  let nativeHeapFreeSize = hidebug.getNativeHeapFreeSize();
  ```


## hidebug.getPss

getPss(): bigint

Obtains the PSS of this process.

S
shawn_he 已提交
84
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
85 86


S
shawn_he 已提交
87 88 89 90
**Return Value**
| Type  | Description                     |
| ------ | ------------------------- |
| bigint | PSS of the process.|
S
shawn_he 已提交
91

S
shawn_he 已提交
92 93

**Example**
S
shawn_he 已提交
94 95 96 97 98 99 100 101 102 103 104
  ```
  let pss = hidebug.getPss();
  ```


## hidebug.getSharedDirty

getSharedDirty(): bigint

Obtains the size of the shared dirty memory of this process.

S
shawn_he 已提交
105
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug
S
shawn_he 已提交
106 107


S
shawn_he 已提交
108 109 110 111
**Return Value**
| Type  | Description                      |
| ------ | -------------------------- |
| bigint | Size of the shared dirty memory of the process.|
S
shawn_he 已提交
112

S
shawn_he 已提交
113 114

**Example**
S
shawn_he 已提交
115
  ```
S
shawn_he 已提交
116
  let sharedDirty = hidebug.getSharedDirty();
S
shawn_he 已提交
117 118 119 120 121 122 123 124 125
  ```


## hidebug.startProfiling

startProfiling(filename : string) : void

Starts the profiling method. `startProfiling()` and `stopProfiling()` are called in pairs. `startProfiling()` always occurs before `stopProfiling()`; that is, calling the functions in the following sequences is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.

S
shawn_he 已提交
126 127
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
128 129
**Parameters**

S
shawn_he 已提交
130
| Name  | Type  | Mandatory| Description                                                        |
S
shawn_he 已提交
131
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
132
| 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`.|
S
shawn_he 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
// Code block
// ...
// Code block
hidebug.stopProfiling();
```



## hidebug.stopProfiling

stopProfiling() : void

Stops the profiling method. `stopProfiling()` and `startProfiling()` are called in pairs. `stopProfiling()` always occurs after `startProfiling()`; that is, calling the functions in the following sequences is prohibited: `start->start->stop`, `start->stop->stop`, and `start->start->stop->stop`.

S
shawn_he 已提交
152 153
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
**Example**

```js
hidebug.startProfiling("cpuprofiler-20220216");
// Code block
// ...
// Code block
hidebug.stopProfiling();
```

## hidebug.dumpHeapData

dumpHeapData(filename : string) : void

Exports the heap data.

S
shawn_he 已提交
170 171
**System capability**: SystemCapability.HiviewDFX.HiProfiler.HiDebug

S
shawn_he 已提交
172 173
**Parameters**

S
shawn_he 已提交
174
| Name  | Type  | Mandatory| Description                                                        |
S
shawn_he 已提交
175
| -------- | ------ | ---- | ------------------------------------------------------------ |
S
shawn_he 已提交
176
| filename | string | Yes  | User-defined heap file name. The `filename.heapsnapshot` file is generated in the `files` directory of the app based on the specified `filename`.|
S
shawn_he 已提交
177 178 179 180 181 182

**Example**

```js
hidebug.dumpHeapData("heap-20220216");
```