1e41f4b71Sopenharmony_ci# @ohos.hiviewdfx.jsLeakWatcher (JSLeakWatcher)
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ciThis module provides the capability of monitoring whether JS objects are leaked.
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci> **NOTE**
6e41f4b71Sopenharmony_ci>
7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Modules to Import
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci```js
12e41f4b71Sopenharmony_ciimport { jsLeakWatcher } from '@kit.PerformanceAnalysisKit';
13e41f4b71Sopenharmony_ci```
14e41f4b71Sopenharmony_ci
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci## jsLeakWatcher.enable
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_cienable(isEnable: boolean): void;
19e41f4b71Sopenharmony_ci
20e41f4b71Sopenharmony_ciEnables the detection for JS object leak. This function is disabled by default.
21e41f4b71Sopenharmony_ci
22e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiChecker
23e41f4b71Sopenharmony_ci
24e41f4b71Sopenharmony_ci**Parameters**
25e41f4b71Sopenharmony_ci
26e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description |
27e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
28e41f4b71Sopenharmony_ci| isEnable | boolean | Yes | Indicates whether to enable **jsLeakWatcher**. |
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci**Example**
31e41f4b71Sopenharmony_ci
32e41f4b71Sopenharmony_ci```js
33e41f4b71Sopenharmony_cijsLeakWatcher.enable(true);
34e41f4b71Sopenharmony_ci```
35e41f4b71Sopenharmony_ci
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci## jsLeakWatcher.watch
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ciwatch(obj: object, msg: string): void;
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ciRegisters the object to be checked.
42e41f4b71Sopenharmony_ci
43e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiChecker
44e41f4b71Sopenharmony_ci
45e41f4b71Sopenharmony_ci**Parameters**
46e41f4b71Sopenharmony_ci
47e41f4b71Sopenharmony_ci| Parameter | Type | Mandatory | Description |
48e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
49e41f4b71Sopenharmony_ci| obj | object | Yes | Name of the object to be checked. |
50e41f4b71Sopenharmony_ci| msg | string | Yes | User-defined object information. |
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ci**Example**
53e41f4b71Sopenharmony_ci
54e41f4b71Sopenharmony_ci```js
55e41f4b71Sopenharmony_cilet obj:Object = new Object();
56e41f4b71Sopenharmony_cijsLeakWatcher.watch(obj, "Trace Object");
57e41f4b71Sopenharmony_ci```
58e41f4b71Sopenharmony_ci
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci## jsLeakWatcher.check
61e41f4b71Sopenharmony_ci
62e41f4b71Sopenharmony_cicheck(): string;
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ciObtains the list of objects that are registered using **jsLeakWatcher.watch()** and may leak. Objects that are not reclaimed after GC is triggered are marked as leaked.
65e41f4b71Sopenharmony_ci
66e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiChecker
67e41f4b71Sopenharmony_ci
68e41f4b71Sopenharmony_ci**Return value**
69e41f4b71Sopenharmony_ci
70e41f4b71Sopenharmony_ci| Type   | Description                                                      |
71e41f4b71Sopenharmony_ci| ------- | ---------------------------------------------------------- |
72e41f4b71Sopenharmony_ci| string | List of objects that are suspected to leak, in JSON format. |
73e41f4b71Sopenharmony_ci
74e41f4b71Sopenharmony_ci**Example**
75e41f4b71Sopenharmony_ci```js
76e41f4b71Sopenharmony_cilet leakObjlist:string = jsLeakWatcher.check();
77e41f4b71Sopenharmony_ci```
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ci## jsLeakWatcher.dump
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_cidump(filePath: string): Array<string>;
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ciExports the list of leaked objects and VM memory snapshot.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci**System capability**: SystemCapability.HiviewDFX.HiChecker
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci**Parameters**
89e41f4b71Sopenharmony_ci
90e41f4b71Sopenharmony_ci| Parameter | Type | Mandatory | Description |
91e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- |
92e41f4b71Sopenharmony_ci| filePath | string | Yes | The path for storing exported information files. |
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci**Return value**
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci| Type   | Description                                                      |
97e41f4b71Sopenharmony_ci| ------- | ---------------------------------------------------------- |
98e41f4b71Sopenharmony_ci| Array<string> | Array of exported results. Index **0** indicates the name of the leak list file, and the file name extension is **.jsleaklist**. Index **1** indicates the name of the VM memory snapshot file, and the file name extension is **.heapsnapshort**. |
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci**Example**
101e41f4b71Sopenharmony_ci
102e41f4b71Sopenharmony_ci```js
103e41f4b71Sopenharmony_cilet context = getContext(this);
104e41f4b71Sopenharmony_cilet files:Array<string> = jsLeakWatcher.dump(context.filesDir);
105e41f4b71Sopenharmony_ci```
106