[JavaScript] jsdom memory leak

Tadashi Shigeoka ·  Sun, January 11, 2015

When I used jsdom with Node.js to scrape tens of thousands of web pages, an out of memory error occurred.

Node.js Error Message

FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

jsdom Memory Leak Solution

It seems you need to call the window.close() method to free memory associated with the window object used in the callback method passed to the jsdom.env method.

jsdom.env(html, function (errors, window) {
  // free memory associated with the window
  window.close();
});

I really felt the pain of not using it exactly as written in the documentation…


Reference Information

tmpvar/jsdom

jsdom and node.js leaking memory - Stack Overflow

javascript - Memory leak in Node.js scraper - Stack Overflow

That’s all from the Gemba.