Skip to content

Commit 35cce1b

Browse files
JacksonGLfacebook-github-bot
authored andcommitted
doc(lens): add a new tutorial page for MemLens
Summary: This diff adds a tutorial page for MemLens on the MemLab documentation site: {F1981229357} Reviewed By: twobassdrum Differential Revision: D80237422 fbshipit-source-id: b7316fc89d56f6eea16116c42750f05f6112bc67
1 parent f9749f2 commit 35cce1b

5 files changed

Lines changed: 106 additions & 4 deletions

File tree

.gitignore

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@ build.log
1414
node_modules
1515
package-lock.json
1616
package-oss.json
17-
packages/**/dist
1817
yarn-error.log
1918
yarn.lock
2019

21-
# But do not ignore these files
22-
!packages/lens/dist/*.bundle.*.(js|ts)
20+
# ignore all dist folders under packages
21+
packages/**/dist/
22+
23+
# unignore the lens/dist directory
24+
!packages/lens/dist/
25+
26+
# re-ignore everything inside lens/dist
27+
packages/lens/dist/*
28+
29+
# But do not ignore these bundle files
30+
!packages/lens/dist/memlens.run.bundle.min.js
31+
!packages/lens/dist/memlens.run.bundle.js
32+
!packages/lens/dist/memlens.lib.bundle.min.js
33+
!packages/lens/dist/memlens.lib.bundle.js

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Features:
2929
snapshots taken from Chromium-based browsers, Node.js, Electron.js, and Hermes
3030
- **Memory CLI toolbox** - Built-in toolbox and APIs for finding memory
3131
optimization opportunities (not necessarily just memory leaks)
32+
- **MemLens: Browser Memory Debugging Tools** - Enables visualization of memory
33+
leaks and interactive memory debugging in the browser.
3234
- **Memory assertions in Node.js** - Enables unit tests or running node.js
3335
programs to take a heap snapshot of their own state, perform self memory
3436
checking, or write advanced memory assertions
@@ -186,6 +188,14 @@ const scenario = {
186188
memlab.run({scenario});
187189
```
188190

191+
## Visual Debugging for Memory Leaks in Browser
192+
193+
Check out this [tutorial page](https://facebook.github.io/memlab/docs/guides/visually-debug-memory-leaks-with-memlens)
194+
on how to use MemLens (a debugging utility) to
195+
visualize memory leaks in the browser for easier memory debugging.
196+
197+
![](./website/static/img/memlens.png)
198+
189199
## Memory Assertions
190200

191201
memlab makes it possible to enable a unit test or running node.js program

packages/lens/.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ out
55
dist/*
66

77
# But do not ignore these files
8-
!dist/*.bundle.*.(js|ts)
8+
!dist/memlens.run.bundle.min.js
9+
!dist/memlens.run.bundle.js
10+
!dist/memlens.lib.bundle.min.js
11+
!dist/memlens.lib.bundle.js
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
id: 'visually-debug-memory-leaks-with-memlens'
3+
---
4+
5+
# Visual Debugging for Memory Leaks
6+
7+
Memory Lens (MemLens) is a utility tool that helps visually debug memory leaks
8+
in web browsers. It highlights leaking components with an overlay,
9+
allowing you to identify leaked elements right after interactions and
10+
trace their retainer paths in the browser’s DevTools using memory IDs
11+
annotated by MemLens.
12+
13+
You can use MemLens in two ways:
14+
1. Manually copy and paste MemLens’ code into the browser console
15+
to debug leaks in the current page.
16+
2. Run MemLens through MemLab.
17+
18+
The sections below explain how to use MemLens for browser memory leak debugging.
19+
20+
## Manually Running MemLens in a Browser
21+
22+
1. Copy and paste the [MemLens bundle code](https://github.com/facebook/memlab/tree/main/packages/lens/dist/memlens.run.bundle.min.js)
23+
into your browser’s console (close DevTools to avoid leaks caused by
24+
Chrome DevTools).
25+
2. Interact with the page without triggering a full refresh, as a hard
26+
refresh clears the entire JS heap and makes leak debugging unnecessary.
27+
3. When MemLens outlines possible memory leaks, take a heap snapshot and
28+
search for the displayed memory IDs.
29+
30+
To build the bundle yourself, clone the
31+
[MemLab repository](https://github.com/facebook/memlab),
32+
run `npm run build`, and copy the MemLens bundle code from
33+
`packages/lens/dist/memlens.run.bundle.min.js` or
34+
`packages/lens/dist/memlens.run.bundle.js`.
35+
36+
The screen recording below demonstrates how to use MemLens on a web page.
37+
38+
<div style={{ position: 'relative', paddingBottom: '56.25%', height: 0, overflow: 'hidden', maxWidth: '100%' }}>
39+
<iframe
40+
src="https://player.vimeo.com/video/1110113648?title=0&byline=0&portrait=0"
41+
style={{ position: 'absolute', top: 0, left: 0, width: '100%', height: '100%' }}
42+
frameBorder="0"
43+
allow="autoplay; fullscreen; picture-in-picture"
44+
allowFullScreen
45+
/>
46+
</div>
47+
48+
Once MemLens is active, it tracks React components that leak DOM elements and
49+
outlines them on the page. Each outline includes a memory ID. Hover over the
50+
outline to view the memory ID and the corresponding React component stack,
51+
or click to pin it.
52+
53+
With the memory ID, take a heap snapshot in Chrome DevTools and search for
54+
it in the Memory panel. This will reveal the leaked component’s retained
55+
path in the JS heap, helping you understand why it remains in memory.
56+
57+
58+
**Note:** The target page should ideally be a React app serving minified
59+
JS code. When searching for the memory ID in Chrome DevTools, the detached
60+
DOM element’s retainer path will also appear minified.
61+
62+
## Using MemLens with MemLab
63+
64+
MemLens can also run during MemLab executions in headful mode to display
65+
leak outlines:
66+
67+
```bash
68+
memlab run --scenario your_test_scenario_file_path.js --headful --display-leak-outlines
69+
````
70+
71+
* `--headful`: Runs the browser in visible mode so you can see the UI.
72+
* `--display-leak-outlines`: Enables MemLens to track and display outlines
73+
of leaked components in the test page.
74+
75+
If you want to pause a MemLab run at steps defined in your scenario file
76+
and interact with MemLens in the headful browser session, use the `--debug`
77+
option. This pauses automated browser actions before each step until you
78+
press **Enter** in the terminal.

website/static/img/memlens.png

1.85 MB
Loading

0 commit comments

Comments
 (0)