1# @ohos.hiviewdfx.jsLeakWatcher (JSLeakWatcher) 2 3This module provides the capability of monitoring whether JS objects are leaked. 4 5> **NOTE** 6> 7> 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. 8 9## Modules to Import 10 11```js 12import { jsLeakWatcher } from '@kit.PerformanceAnalysisKit'; 13``` 14 15 16## jsLeakWatcher.enable 17 18enable(isEnable: boolean): void; 19 20Enables the detection for JS object leak. This function is disabled by default. 21 22**System capability**: SystemCapability.HiviewDFX.HiChecker 23 24**Parameters** 25 26| Name | Type | Mandatory | Description | 27| -------- | -------- | -------- | -------- | 28| isEnable | boolean | Yes | Indicates whether to enable **jsLeakWatcher**. | 29 30**Example** 31 32```js 33jsLeakWatcher.enable(true); 34``` 35 36 37## jsLeakWatcher.watch 38 39watch(obj: object, msg: string): void; 40 41Registers the object to be checked. 42 43**System capability**: SystemCapability.HiviewDFX.HiChecker 44 45**Parameters** 46 47| Parameter | Type | Mandatory | Description | 48| -------- | -------- | -------- | -------- | 49| obj | object | Yes | Name of the object to be checked. | 50| msg | string | Yes | User-defined object information. | 51 52**Example** 53 54```js 55let obj:Object = new Object(); 56jsLeakWatcher.watch(obj, "Trace Object"); 57``` 58 59 60## jsLeakWatcher.check 61 62check(): string; 63 64Obtains 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. 65 66**System capability**: SystemCapability.HiviewDFX.HiChecker 67 68**Return value** 69 70| Type | Description | 71| ------- | ---------------------------------------------------------- | 72| string | List of objects that are suspected to leak, in JSON format. | 73 74**Example** 75```js 76let leakObjlist:string = jsLeakWatcher.check(); 77``` 78 79 80## jsLeakWatcher.dump 81 82dump(filePath: string): Array<string>; 83 84Exports the list of leaked objects and VM memory snapshot. 85 86**System capability**: SystemCapability.HiviewDFX.HiChecker 87 88**Parameters** 89 90| Parameter | Type | Mandatory | Description | 91| -------- | -------- | -------- | -------- | 92| filePath | string | Yes | The path for storing exported information files. | 93 94**Return value** 95 96| Type | Description | 97| ------- | ---------------------------------------------------------- | 98| 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**. | 99 100**Example** 101 102```js 103let context = getContext(this); 104let files:Array<string> = jsLeakWatcher.dump(context.filesDir); 105``` 106