|
| 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. |
0 commit comments