Facebook open source framework to detect memory leaks in JavaScript

The Facebook company (banned in the Russian Federation) has opened the source texts of the memlab toolkit, designed to analyze slices of the state of dynamically allocated memory (heap), determine a strategy for optimizing work with memory and detect memory leaks that occur when executing JavaScript code. The code is open source under the MIT license.

The framework was created to analyze the causes of high memory consumption when working with websites and web applications. For example, memlab was used to analyze memory consumption when using a new version of the Facebook.com site, which revealed leaks that led to a browser crash on the client side due to the exhaustion of free memory.

The causes of memory leaks when executing JavaScript code can be the presence of hidden references to objects, due to which the garbage collector cannot free the memory occupied by the object, unreasonable caching of values, or the implementation of infinite scrolling without evicting old list items. For example, in the code below in Chrome, a memory leak occurs because of the "obj" object, despite the fact that it is set to null, because Chrome stores internal references to the output objects to allow them to be later inspected in the web console. var obj = {}; console log(obj); obj = null;

Main features of memlab:

  • Detection of memory leaks in the browser. Memlab allows you to automatically compare dynamic memory snapshots, detect memory leaks, and aggregate results.
  • An object-oriented heap traversal API that allows you to implement your own leak detection algorithms and implement systems for analyzing heap snapshots. Heap analysis is supported for browsers based on the Chromium engine, as well as for the Node.js, Electron, and Hermes platforms.
  • Command line interface and API for finding opportunities to optimize memory usage.
  • An assert system for Node.js that allows you to create unit tests and run Node.js-based programs to create slices of your own state, test your memory, or write advanced assert tests.

Source: opennet.ru

Add a comment