memlab 1.1.15 → 1.1.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +100 -7
  2. package/bin/memlab +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,8 +1,25 @@
1
1
  # memlab
2
2
 
3
3
  memlab is an E2E testing and analysis framework for finding JavaScript memory
4
- leaks in Chromium. The CLI Toolbox and library provide extensible interfaces
5
- for analyzing heap snapshots taken from Chrome/Chromium, Node.js, Hermes, and Electron.js.
4
+ leaks and optimization opportunities.
5
+
6
+ Features:
7
+ * **Browser memory leak detection** - Write test scenario with puppeteer API,
8
+ memlab auto diffs JS heap snapshots, filters out memory leaks, and
9
+ aggregates results.
10
+ * **Object-oriented heap traversing API** - Supports self-defined memory leak
11
+ detector and programmatically analyzing JS heap snapshots taken from
12
+ Chromium-based browsers, Node.js, Electron.js, and Hermes
13
+ * **Memory CLI Toolbox** - Built-in toolbox and APIs for finding memory
14
+ optimization opportunities (not necessarily memory leaks)
15
+ * **Memory assertions in Node.js** - Enables unit test or running node.js
16
+ program to take a heap snapshot of its own state, do self memory checking,
17
+ or write advanced memory assertions
18
+
19
+ Online Resources:
20
+
21
+ * [Official Website and Demo](https://facebookincubator.github.io/memlab)
22
+ * [Documentation](https://facebookincubator.github.io/memlab/docs/intro)
6
23
 
7
24
  ## CLI Usage
8
25
 
@@ -14,7 +31,8 @@ npm install -g memlab
14
31
 
15
32
  ### Find Memory Leaks
16
33
 
17
- To find memory leaks in Google Maps, you can create a scenario file defining how
34
+ To find memory leaks in Google Maps, you can create a
35
+ [scenario file](https://facebookincubator.github.io/memlab/docs/api/interfaces/core_src.IScenario) defining how
18
36
  to interact with the Google Maps, let's name it `test-google-maps.js`:
19
37
 
20
38
  ```javascript
@@ -45,7 +63,41 @@ the web page and detect memory leaks with built-in leak detectors:
45
63
  memlab run --scenario test-google-maps.js
46
64
  ```
47
65
 
48
- If you want to use a self-defined leak detector, add a `filterLeak` callback
66
+ memlab will print memory leak results showing one representative
67
+ retainer trace for each cluster of leaked objects.
68
+
69
+ **Retainer traces**: This is the result from
70
+ [an example website](https://facebookincubator.github.io/memlab/docs/guides/guides-find-leaks),
71
+ the retainer trace is an object reference chain from the GC root to a leaked
72
+ object. The trace shows why and how a leaked object is still kept alive in
73
+ memory. Breaking the reference chain means the leaked object will no longer
74
+ be reachable from the GC root, and therefore can be garbage collected.
75
+ By following the leak trace one step at a time, you will be able to find
76
+ a reference that should be set to null (but it wasn't due to a bug).
77
+
78
+ ```bash
79
+ MemLab found 46 leak(s)
80
+ --Similar leaks in this run: 4--
81
+ --Retained size of leaked objects: 8.3MB--
82
+ [Window] (native) @35847 [8.3MB]
83
+ --20 (element)---> [InternalNode] (native) @130981728 [8.3MB]
84
+ --8 (element)---> [InternalNode] (native) @130980288 [8.3MB]
85
+ --1 (element)---> [EventListener] (native) @131009888 [8.3MB]
86
+ --1 (element)---> [V8EventListener] (native) @224808192 [8.3MB]
87
+ --1 (element)---> [eventHandler] (closure) @168079 [8.3MB]
88
+ --context (internal)---> [<function scope>] (object) @181905 [8.3MB]
89
+ --bigArray (variable)---> [Array] (object) @182925 [8.3MB]
90
+ --elements (internal)---> [(object elements)] (array) @182929 [8.3MB]
91
+ ...
92
+ ```
93
+ To get readable trace, the web site under test needs to serve non-minified code (or at least minified code
94
+ with readable variables, function name, and property names on objects).
95
+
96
+ Alternatively, you can debug the leak by loading the heap snapshot taken by memlab (saved in `$(memlab get-default-work-dir)/data/cur`)
97
+ in Chrome DevTool and search for the leaked object ID (`@182929`).
98
+
99
+ **Self-defined leak detector**: If you want to use a self-defined leak detector, add a `filterLeak` callback
100
+ ([doc](https://facebookincubator.github.io/memlab/docs/api/interfaces/core_src.IScenario/#-optional-beforeleakfilter-initleakfiltercallback))
49
101
  in the scenario file. `filterLeak` will be called for every unreleased heap
50
102
  object (`node`) allocated by the target interaction.
51
103
 
@@ -57,7 +109,8 @@ function filterLeak(node, heap) {
57
109
  ```
58
110
 
59
111
  `heap` is the graph representation of the final JavaScript heap snapshot.
60
- For more details, view the [doc site](https://facebookincubator.github.io/memlab).
112
+ For more details, view the
113
+ [doc site](https://facebookincubator.github.io/memlab/docs/api/interfaces/core_src.IHeapSnapshot).
61
114
 
62
115
  ### Heap Analysis and Investigation
63
116
 
@@ -66,13 +119,14 @@ View which object keeps growing in size during interaction in the previous run:
66
119
  memlab analyze unbound-object
67
120
  ```
68
121
 
69
- Analyze pre-fetched v8/hermes `.heapsnapshot` files:
122
+ Analyze pre-fetched V8/hermes `.heapsnapshot` files:
70
123
 
71
124
  ```bash
72
125
  memlab analyze unbound-object --snapshot-dir <DIR_OF_SNAPSHOT_FILES>
73
126
  ```
74
127
 
75
- Use `memlab analyze` to view all built-in memory analyses. For extension, view the [doc site](https://facebookincubator.github.io/memlab).
128
+ Use `memlab analyze` to view all built-in memory analyses.
129
+ For extension, view the [doc site](https://facebookincubator.github.io/memlab).
76
130
 
77
131
  View retainer trace of a particular object:
78
132
  ```bash
@@ -100,3 +154,42 @@ const scenario = {
100
154
  }
101
155
  memlab.run({scenario});
102
156
  ```
157
+
158
+ ## Memory Assertions
159
+
160
+ memlab makes it possible to enable a unit test or running node.js program
161
+ to take a heap snapshot of its own state, and write advanced memory assertions:
162
+
163
+ ```typescript
164
+ // save as example.test.ts
165
+ import type {IHeapSnapshot, Nullable} from '@memlab/core';
166
+ import {config, getNodeInnocentHeap} from '@memlab/core';
167
+
168
+ class TestObject {
169
+ public arr1 = [1, 2, 3];
170
+ public arr2 = ['1', '2', '3'];
171
+ }
172
+
173
+ test('memory test with heap assertion', async () => {
174
+ config.muteConsole = true; // no console output
175
+
176
+ let obj: Nullable<TestObject> = new TestObject();
177
+ // get a heap snapshot of the current program state
178
+ let heap: IHeapSnapshot = await getNodeInnocentHeap();
179
+
180
+ // call some function that may add references to obj
181
+ rabbitHole(obj)
182
+
183
+ expect(heap.hasObjectWithClassName('TestObject')).toBe(true);
184
+ obj = null;
185
+
186
+ heap = await getNodeInnocentHeap();
187
+ // if rabbitHole does not have any side effect that
188
+ // adds new references to obj, then obj can be GCed
189
+ expect(heap.hasObjectWithClassName('TestObject')).toBe(false);
190
+
191
+ }, 30000);
192
+ ```
193
+
194
+ For other APIs check out the
195
+ [API documentation](https://facebookincubator.github.io/memlab/docs/api/interfaces/core_src.IHeapSnapshot#hasobjectwithclassnameclassname).
package/bin/memlab CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env -S node --expose-gc --max-old-space-size=4096
1
+ #!/usr/bin/env node --expose-gc --max-old-space-size=4096
2
2
 
3
3
  /**
4
4
  * Copyright (c) Meta Platforms, Inc. and affiliates.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "memlab",
3
- "version": "1.1.15",
3
+ "version": "1.1.18",
4
4
  "license": "MIT",
5
5
  "description": "memlab is a framework that analyzes memory and finds memory leaks in JavaScript applications.",
6
6
  "main": "dist/index.js",