question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Memory leak with fetching external sources

See original GitHub issue

It seems that externally fetched sources (like js-files) are not properly cleared from memory:

var jsdom = require('jsdom');

function get() {
  return new Promise((resolve, reject) => {
    jsdom.env({
      url: 'http://localhost:3200/some-page-with-js',
      features: {
        FetchExternalResources: ['script'],
        ProcessExternalResources: ['script'],
      },
      done: (err, window) => {
        if (err) {
          reject(err);
        } else {
          window.close();
          resolve();
        }
      },
    });
  });
}

function recurse(i) {
  console.log('recurse', i);
  if (i < 10000) {
    get().then(x => recurse(i+1));
  }
}

recurse(0);
<--- Last few GCs --->

   40573 ms: Mark-sweep 1336.1 (1447.4) -> 1337.8 (1447.4) MB, 787.7 / 0 ms [allocation failure] [GC in old space requested].
   41390 ms: Mark-sweep 1337.8 (1447.4) -> 1337.8 (1447.4) MB, 816.4 / 0 ms [allocation failure] [GC in old space requested].
   42260 ms: Mark-sweep 1337.8 (1447.4) -> 1287.9 (1447.4) MB, 869.9 / 0 ms [last resort gc].
   43173 ms: Mark-sweep 1287.9 (1447.4) -> 1334.9 (1447.4) MB, 913.7 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0x2a6aadc44a49 <JS Object>
    2: runInContext [vm.js:~42] [pc=0x361d28a9b004] (this=0x2a7ae5198239 <an Object with map 0xb6831c271a9>,code=0xf882304101 <Very long string[4912697]>,contextifiedSandbox=0x31599489d4e9 <a Window with map 0x3e12b0c06bf9>,options=0x1947d74ae761 <an Object with map 0xb6831c3e1d1>)
    3: processJavaScript(aka processJavaScript) [/Users/Jasper/Code/Q42/blink/test/functional/node_modules/jsdom/l...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

screen shot 2016-05-20 at 12 17 42

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
thymikeecommented, Dec 20, 2016

I’d like to bump this thread, since I think this is something more generic as it happens also for jsdom.jsdom() method. I’ve setup a basic repro with the issue: https://github.com/thymikee/jsdom-leak-repro. Hope it helps someone familiar with jsdom internals to see what’s going on.

Below code is sufficient to retain the Window object:

const jsdom = require('jsdom');
const toMB = bytes => Math.floor(bytes / 1024 / 1024);

function spawnWindow(index) {
  let document = jsdom.jsdom({ url: 'about:blank' });
  let window = document.defaultView;
  const bytes = process.memoryUsage().heapUsed;

  console.log(index, `(${toMB(bytes)} MB)`);

  window.close();
  global.gc(); // force GC to isolate the leak
}

for (let i = 0; i < 200; i++) {
  spawnWindow(i);
}

This is actual problem that we sometimes experience at Jest, here’s on of the relevant issues: https://github.com/facebook/jest/issues/1893

1reaction
alecc08commented, Oct 23, 2016

I have the same issue, going over 1.5GB of memory after about 30 iterations of loading a page, even when calling window.close() after each page load. Also happened when I disabled loading externalResources so I don’t think that’s the issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Causes of Memory Leaks in JavaScript and How to Avoid Them
Common Sources of Memory Leaks in JavaScript Code # ... A search for the memory leaks causes is actually a search for programming...
Read more >
4 Types of Memory Leaks in JavaScript and How to Get Rid Of ...
The main cause for leaks in garbage collected languages are unwanted references. To understand what unwanted references are, first we need to ...
Read more >
Understanding Memory Leaks in Java - Baeldung
A memory leak is bad because it blocks memory resources and degrades system performance over time. If not dealt with, the application will ......
Read more >
Memory leak when body is not used · Issue #83 · node-fetch ...
I wanted to test a URL for validity; verifying that it returns a 200 response. Using this code: const fetch = require('node-fetch'); ...
Read more >
How to handle a memory leak in an external DLL
Definitely hold that last idea for the absolute last resort. Try to find out what kind of memory or resource is being leaked...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found