closure-context-scan 0.1.0 → 0.1.1

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 (2) hide show
  1. package/README.md +106 -106
  2. package/package.json +47 -47
package/README.md CHANGED
@@ -1,106 +1,106 @@
1
- # closure-context-scan
2
-
3
- Finds memory that is kept alive **only** because a live closure shares its V8 context with a dead one.
4
-
5
- ## The bug it targets
6
-
7
- ```js
8
- function setup() {
9
- const big = loadHugeData();
10
- const small = 42;
11
- function debug() { return big.length; } // never kept
12
- handlers.push(() => small); // kept, never touches big
13
- }
14
- ```
15
-
16
- V8 creates **one context** per scope, shared by every closure in that scope. Because `debug` reads `big`, `big` is stored in that context. The arrow function that is kept alive keeps the context alive, and therefore `big`, even though it never uses it.
17
-
18
- ESLint and TypeScript cannot see this. In DevTools you only get a `system / Context` entry, with no hint of which code is responsible.
19
-
20
- ## What the tool does
21
-
22
- 1. Takes a heap snapshot (`.heapsnapshot`) of your program.
23
- 2. Computes the dominator tree: how much memory would be freed if a given object went away.
24
- 3. For each closure context, finds the **live** closures that can read it. It reads their source code (embedded in the snapshot) and lists the variables they actually use.
25
- 4. Reports a context variable when all three conditions hold:
26
- - no live closure uses it;
27
- - the context is the only path keeping it in memory;
28
- - its retained size is above the threshold.
29
- 5. Tells you where it is declared and which (now gone) function used it.
30
-
31
- ## Usage
32
-
33
- ```bash
34
- npm i -D closure-context-scan
35
- ```
36
-
37
- ```json
38
- "scripts": {
39
- "mem:closures": "closure-context-scan run -- src/index.js",
40
- "mem:closures:server": "closure-context-scan run --delay 5000 -- server.js",
41
- "mem:closures:ci": "closure-context-scan run --fail-on 5mb -- scripts/scenario.js"
42
- }
43
- ```
44
-
45
- Or without installing: `npx closure-context-scan run -- app.js`.
46
-
47
- Prefer your editor? The **Closure Context Scan** VS Code extension shows the same results in the Problems panel (see [`vscode-extension/`](vscode-extension/)).
48
-
49
- Requires Node.js 22 or later.
50
-
51
- | Command / option | Description |
52
- |---|---|
53
- | `run -- <script> [args]` | Runs the script and takes the snapshot at exit, or after `--delay` |
54
- | `analyze <file.heapsnapshot>` | Analyzes an existing snapshot (including one taken in DevTools) |
55
- | `--delay <ms>` | For servers: take the snapshot after this delay, then stop |
56
- | `--min-size <size>` | Reporting threshold, `100kb` by default |
57
- | `--fail-on <size>` | Exit with code 1 if the total wasted memory exceeds this size (CI) |
58
- | `--ignore <regex>` | Ignore matching files, e.g. `node_modules` |
59
- | `--json` | JSON output |
60
- | `--keep <file>` | Keep the snapshot at this path |
61
-
62
- On an already running process (Linux/macOS): `node --require closure-context-scan/src/register.js app.js`, then `kill -USR2 <pid>`, then `closure-context-scan analyze closure-scan-<pid>.heapsnapshot`. (`SIGUSR2` is not available on Windows; use `run --delay` there.)
63
-
64
- Tip for servers: send some traffic during the `--delay` window so that request-scoped closures exist when the snapshot is taken.
65
-
66
- ## Sample output
67
-
68
- ```
69
- 1. "report" — 1.5 MB wasted [object Array]
70
- Declared in makeRoute (app.js:5)
71
- Kept alive by closure (anonymous) (app.js:8:17), which does not use it.
72
- Only used by: debug (line 7) — function(s) no longer in memory.
73
- ```
74
-
75
- ## Conservative by design
76
-
77
- The tool would rather miss a leak than blame healthy code. It draws no conclusion when:
78
- - `eval` or `with` is present;
79
- - the context is retained by something other than a closure (suspended generator or `async` function, a function currently executing, native code);
80
- - the source cannot be parsed.
81
-
82
- Variable usage detection is deliberately broad: a name that appears anywhere in the closure counts as used.
83
-
84
- ## Security note
85
-
86
- A heap snapshot is a full dump of your process memory: it can contain secrets (environment variables, tokens, passwords, user data).
87
- - Snapshots taken by `run` are written to the OS temp directory and deleted after analysis, unless you pass `--keep`.
88
- - Do not commit or share `.heapsnapshot` files; add `*.heapsnapshot` to your `.gitignore`.
89
- - Only run the tool against environments and data you are allowed to inspect.
90
-
91
- ## Limitations
92
-
93
- - Only analyzes JavaScript as it is executed. With TypeScript or a bundler, reported lines refer to the compiled code (no source map support yet).
94
- - The `.heapsnapshot` file is loaded fully into memory. A 114 MB heap took about 5 s to analyze; very large heaps need a lot of RAM.
95
- - Only closures that exist at snapshot time are visible.
96
- - V8 internals can change between Node versions (tested on Node 22 and 24).
97
-
98
- ## Tests
99
-
100
- ```bash
101
- npm test
102
- ```
103
-
104
- ## License
105
-
106
- MIT
1
+ # closure-context-scan
2
+
3
+ Finds memory that is kept alive **only** because a live closure shares its V8 context with a dead one.
4
+
5
+ ## The bug it targets
6
+
7
+ ```js
8
+ function setup() {
9
+ const big = loadHugeData();
10
+ const small = 42;
11
+ function debug() { return big.length; } // never kept
12
+ handlers.push(() => small); // kept, never touches big
13
+ }
14
+ ```
15
+
16
+ V8 creates **one context** per scope, shared by every closure in that scope. Because `debug` reads `big`, `big` is stored in that context. The arrow function that is kept alive keeps the context alive, and therefore `big`, even though it never uses it.
17
+
18
+ ESLint and TypeScript cannot see this. In DevTools you only get a `system / Context` entry, with no hint of which code is responsible.
19
+
20
+ ## What the tool does
21
+
22
+ 1. Takes a heap snapshot (`.heapsnapshot`) of your program.
23
+ 2. Computes the dominator tree: how much memory would be freed if a given object went away.
24
+ 3. For each closure context, finds the **live** closures that can read it. It reads their source code (embedded in the snapshot) and lists the variables they actually use.
25
+ 4. Reports a context variable when all three conditions hold:
26
+ - no live closure uses it;
27
+ - the context is the only path keeping it in memory;
28
+ - its retained size is above the threshold.
29
+ 5. Tells you where it is declared and which (now gone) function used it.
30
+
31
+ ## Usage
32
+
33
+ ```bash
34
+ npm i -D closure-context-scan
35
+ ```
36
+
37
+ ```json
38
+ "scripts": {
39
+ "mem:closures": "closure-context-scan run -- src/index.js",
40
+ "mem:closures:server": "closure-context-scan run --delay 5000 -- server.js",
41
+ "mem:closures:ci": "closure-context-scan run --fail-on 5mb -- scripts/scenario.js"
42
+ }
43
+ ```
44
+
45
+ Or without installing: `npx closure-context-scan run -- app.js`.
46
+
47
+ Prefer your editor? The **Closure Context Scan** VS Code extension shows the same results in the Problems panel (see [`vscode-extension/`](vscode-extension/)).
48
+
49
+ Requires Node.js 22 or later.
50
+
51
+ | Command / option | Description |
52
+ |---|---|
53
+ | `run -- <script> [args]` | Runs the script and takes the snapshot at exit, or after `--delay` |
54
+ | `analyze <file.heapsnapshot>` | Analyzes an existing snapshot (including one taken in DevTools) |
55
+ | `--delay <ms>` | For servers: take the snapshot after this delay, then stop |
56
+ | `--min-size <size>` | Reporting threshold, `100kb` by default |
57
+ | `--fail-on <size>` | Exit with code 1 if the total wasted memory exceeds this size (CI) |
58
+ | `--ignore <regex>` | Ignore matching files, e.g. `node_modules` |
59
+ | `--json` | JSON output |
60
+ | `--keep <file>` | Keep the snapshot at this path |
61
+
62
+ On an already running process (Linux/macOS): `node --require closure-context-scan/src/register.js app.js`, then `kill -USR2 <pid>`, then `closure-context-scan analyze closure-scan-<pid>.heapsnapshot`. (`SIGUSR2` is not available on Windows; use `run --delay` there.)
63
+
64
+ Tip for servers: send some traffic during the `--delay` window so that request-scoped closures exist when the snapshot is taken.
65
+
66
+ ## Sample output
67
+
68
+ ```
69
+ 1. "report" — 1.5 MB wasted [object Array]
70
+ Declared in makeRoute (app.js:5)
71
+ Kept alive by closure (anonymous) (app.js:8:17), which does not use it.
72
+ Only used by: debug (line 7) — function(s) no longer in memory.
73
+ ```
74
+
75
+ ## Conservative by design
76
+
77
+ The tool would rather miss a leak than blame healthy code. It draws no conclusion when:
78
+ - `eval` or `with` is present;
79
+ - the context is retained by something other than a closure (suspended generator or `async` function, a function currently executing, native code);
80
+ - the source cannot be parsed.
81
+
82
+ Variable usage detection is deliberately broad: a name that appears anywhere in the closure counts as used.
83
+
84
+ ## Security note
85
+
86
+ A heap snapshot is a full dump of your process memory: it can contain secrets (environment variables, tokens, passwords, user data).
87
+ - Snapshots taken by `run` are written to the OS temp directory and deleted after analysis, unless you pass `--keep`.
88
+ - Do not commit or share `.heapsnapshot` files; add `*.heapsnapshot` to your `.gitignore`.
89
+ - Only run the tool against environments and data you are allowed to inspect.
90
+
91
+ ## Limitations
92
+
93
+ - Only analyzes JavaScript as it is executed. With TypeScript or a bundler, reported lines refer to the compiled code (no source map support yet).
94
+ - The `.heapsnapshot` file is loaded fully into memory. A 114 MB heap took about 5 s to analyze; very large heaps need a lot of RAM.
95
+ - Only closures that exist at snapshot time are visible.
96
+ - V8 internals can change between Node versions (tested on Node 22 and 24).
97
+
98
+ ## Tests
99
+
100
+ ```bash
101
+ npm test
102
+ ```
103
+
104
+ ## License
105
+
106
+ MIT
package/package.json CHANGED
@@ -1,47 +1,47 @@
1
- {
2
- "name": "closure-context-scan",
3
- "version": "0.1.0",
4
- "description": "Find memory retained by dead closures through V8 shared contexts",
5
- "main": "src/analyze.js",
6
- "directories": {
7
- "test": "test"
8
- },
9
- "scripts": {
10
- "test": "node --test test/*.test.js",
11
- "demo": "closure-context-scan run -- fixtures/leak.js"
12
- },
13
- "keywords": [
14
- "memory-leak",
15
- "closure",
16
- "v8",
17
- "heap-snapshot",
18
- "heapsnapshot",
19
- "nodejs",
20
- "profiling",
21
- "cli",
22
- "devtools"
23
- ],
24
- "license": "MIT",
25
- "author": "appystore",
26
- "repository": {
27
- "type": "git",
28
- "url": "git+https://github.com/IT-Skill31/closure-context-scan.git"
29
- },
30
- "homepage": "https://github.com/IT-Skill31/closure-context-scan#readme",
31
- "bugs": {
32
- "url": "https://github.com/IT-Skill31/closure-context-scan/issues"
33
- },
34
- "dependencies": {
35
- "acorn": "^8.19.0"
36
- },
37
- "bin": {
38
- "closure-context-scan": "bin/cli.js"
39
- },
40
- "engines": {
41
- "node": ">=22"
42
- },
43
- "files": [
44
- "bin",
45
- "src"
46
- ]
47
- }
1
+ {
2
+ "name": "closure-context-scan",
3
+ "version": "0.1.1",
4
+ "description": "Find memory retained by dead closures through V8 shared contexts",
5
+ "main": "src/analyze.js",
6
+ "directories": {
7
+ "test": "test"
8
+ },
9
+ "scripts": {
10
+ "test": "node --test test/*.test.js",
11
+ "demo": "closure-context-scan run -- fixtures/leak.js"
12
+ },
13
+ "keywords": [
14
+ "memory-leak",
15
+ "closure",
16
+ "v8",
17
+ "heap-snapshot",
18
+ "heapsnapshot",
19
+ "nodejs",
20
+ "profiling",
21
+ "cli",
22
+ "devtools"
23
+ ],
24
+ "license": "MIT",
25
+ "author": "appystore",
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/IT-Skill31/closure-context-scan.git"
29
+ },
30
+ "homepage": "https://github.com/IT-Skill31/closure-context-scan#readme",
31
+ "bugs": {
32
+ "url": "https://github.com/IT-Skill31/closure-context-scan/issues"
33
+ },
34
+ "dependencies": {
35
+ "acorn": "^8.19.0"
36
+ },
37
+ "bin": {
38
+ "closure-context-scan": "bin/cli.js"
39
+ },
40
+ "engines": {
41
+ "node": ">=22"
42
+ },
43
+ "files": [
44
+ "bin",
45
+ "src"
46
+ ]
47
+ }