memlab

1 min read


Hello Stackers, While JavaScript runtimes use garbage collection to manage memory, this does not rule out the possibility of leaking memory. To detect all possible cases where memory is leaked, Facebook created and open-sourced MemLab, a tool able to run predefined, automatable test scenarios and analyze heapshot diffs.

In a nutshell, MemLab finds memory leaks by running a headless browser through predefined test scenarios and diffing and analyzing the JavaScript heap snapshots.

Analyzes JavaScript heap and finds memory leaks in browser and node.js

memlab

Quick Start

Define E2E test scenarios on browser interaction:

// test.js
function url() {
  return 'https://www.google.com/maps/';
}
async function action(page) {
  await page.click('button[aria-label="Hotels"]');
}
async function back(page) {
  await page.click('[aria-label="Clear search"]');
}

module.exports = {action, back, url};

Run memlab :

$ memlab run --scenario test.js
# Analyze duplicated string in heap
$ memlab analyze string
# Check unbound object growth
$ memlab analyze unbound-object
# Get shapes with unbound growth
$ memlab analyze unbound-shape
# discover more memory analyses
$ memlab analyze -h

Example Result :

memlab run --scenario ./test-google-maps.js                                
page-load[24.6MB](baseline)[s1] > action-on-page[30.4MB](target)[s2] > revert[29.4MB](fina
l)[s3]                                                                                    
                                                                                          
total time: 1.3min                                                                        
Memory usage across all steps:                                                            
35.0 _________                                                                            
30.7 _________                                                                            
26.5 ___  _▄▄_                                                                            
22.2 ▄▄_  _  _                                                                            
18.0   _  _  _                                                                            
13.7   _  _  _                                                                            
 9.5   _  _  _                                                                            
 5.2   _  _  _                                                                            
 1.0   _  _  _                                                                            
     1  2  3                                                                              
                                                                                          
MemLab found 7 leak(s)

That’s all, for the full documentation please visit here … cyaa ….

Bima Sena

Leave a Reply

Your email address will not be published. Required fields are marked *