It's not just the React data structures that were kept alive. By hooks and their closures, this would often keep live all kinds of product objects. And in some situations, leaking the product code wasn't even required. React cleanup wasn't always immediate and therefore still leaks the entire fiber tree of the previous routes.
So we added the optimization in React by introducing a full traversal of fiber tree on our mount to do more aggressive cleanup. This allows the garbage collector to do a better job at cleaning up an unbounded fiber tree. And also any accidental memory leaks are bounded to a smaller leak size.
To measure all these improvements we actually run A-B testing on Facebook.com and the number of auto memory crashes in the browser dropped by 50% due to that optimization. And surprisingly, the full traversal didn't slow down the page. The memory we actually slightly improved the performance of the page and we observe significant improvements in many other metrics.
MemoryLab also includes an in-browser tool called MemoryLens to visualize memory leaks directly on the web page's UI debugging layer. The tool is a JS module running the application. It monitors the changes in DOM tree and React fiber. And MemoryLens highlights the outline of leaked React components when their DOM elements get detached from the DOM tree but are still kept alive in memory. It also attaches a special memory ID to each DOM element, making it easy to link them back to the view in heap snapshots.
You can also quickly search the memory ID in the Chrome dev tools when you see a leaked component outline and its ID in the UI debugging layer. We have more details in the MemoryLab documentation page for complete guidance for MemoryLens. Finally, a large language model and AI coding are pretty powerful these days. So I want to talk a little bit about how MemoryLab with AI can make memory detection, memory analysis, and memory debugging even easier.
In MemoryLab's GitHub repo, we've added skills files teaching AI how to properly use MemoryLab tooling and MemoryLab APIs. For example, you can provide skills files to Meta-AI, ChattBot, or CloudCode, asking them to write a MemoryLab test scenario file for web application. You can also copy and paste the leak detection results with those AI tooling, asking them to explain or fix the memory leak based on MemoryLab's output. You can also ask AI to help you create a custom analyzer plugin for advanced memory optimization using the MemoryLab API.
Finally, I want to introduce the MemoryLab MCP server package. This allows you to ask AI coding tools like CloudCode to investigate memory leaks in JavaScript heap snapshots more accurately. The MCP server loads heap snapshots only once in a stateful local process and provides all the MCP tools AI needs to query heap snapshots as a heap graph. With MCP servers, AI can traverse and explore the heap graph as much as it wants and do it in a more reliable and efficient way. Instead of parsing the raw heap snapshot from scratch for every follow-up analysis and prompts, it will also save you a lot of the tokens when the heap snapshot is huge.
With the MMP server, you can ask AI all kinds of questions. For example, load heap snapshots in this location and tell me what's using the most memory. Why is this fiber node still alive? Are there any more memory leaks and memory optimization opportunities? Can you create a code commit to fix this memory leak, etc.? The MCP server also supports an eval tool that allows AI to write the heap analysis code using MemoryLab API and executes it in a sandbox environment. So AI can do a very advanced heap analysis based on simple prompts. For example, with MCP server you can give AI prompts like finding inconsistent shapes of objects or hidden classes that waste memory. AI will be able to find examples of different property insertion order that causes the polymorphism of different shapes for the same class of objects. It's a pretty powerful tool to help us fix many memory issues at Meta with a single shot. We highly recommend you give it a try.
Okay, that's the end of my presentation. More demos, documentations, links, resources are available in MemoryLab's GitHub repo. Thanks for listening.
Comments