Facebook (banned in the RF) has released the source code for the memlab toolkit, designed for analyzing snapshots of the state of dynamically allocated memory (heap), identifying optimization strategies for memory usage, and detecting memory leaks that occur when executing JavaScript code. The code is open under the MIT license.
The framework was created to investigate the causes of high memory consumption when working with websites and web applications. For example, memlab was used to analyze memory consumption with the new version of facebook.com, which revealed leaks that led to browser crashes on the client side due to exhausted free memory.
Causes of memory leaks during the execution of JavaScript code may include hidden references to objects, preventing the garbage collector from freeing the memory occupied by the object, unreasonable caching of values, or the implementation of infinite scrolling without evicting older elements from the list. For example, in the code below, Chrome experiences a memory leak due to the object 'obj', even though it is assigned null, as Chrome retains internal references to output objects for the purpose of allowing subsequent inspection in the web console. var obj = {}; console.log(obj); obj = null;
Key features of memlab:
- Detection of memory leaks in the browser. Memlab allows for automatic comparison of snapshots of dynamic memory state, detecting memory leaks, and aggregating results.
- Object-oriented API for traversing the heap, allowing the implementation of custom leak detection algorithms and systems for analyzing heap state snapshots. Heap analysis is supported for browsers based on the Chromium engine, as well as for Node.js, Electron, and Hermes platforms.
- Command-line interface and API for finding opportunities to optimize memory usage.
- A system of asserts for Node.js that allows for creating unit tests and running Node.js-based programs to generate snapshots of their own state, check their memory, or write extended assert checks.
Source: opennet.ru
