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.

"done" callback cause memory leak

See original GitHub issue

I am trying to wrap “env” in Promise, but memory grows heavily in production.

Below code shows no memory leaks. If replacing the no leaks with the leaking code, Window object would not be GCed.

In addition, uncomment injecting jquery source causes it always leaks.

All are tested and use chrome profiles to investigate.

import * as jsdom from "jsdom";
import * as fs from "fs";
import * as _ from "lodash";
var heapdump = require('heapdump');

var jquery = fs.readFileSync("assets/jquery.min.js", "utf-8");

function envAsync() {
    return new Promise<Window>(function (resolve, reject) {
        jsdom.env({
            html: "https://www.google.com/?gws_rd=ssl",
            // always leak if uncomment
            //src: [jquery],
            done: function (err, window) {
                if (err) {
                    return reject(err);
                }

                // no leaks version
                window.close();
                resolve(null);

                // memory leaking version
                // resolve(window);
            }
        });
    });
}

heapdump.writeSnapshot(function (err, sname) {
    console.log(sname);

    (async () => {
        for (var index = 0; index < 10; index++) {
            var window = await envAsync();

            // memory leaking version
            // window.close();
        }

        setTimeout(function () {
            global.gc();
            heapdump.writeSnapshot(function (err, ename) {
                console.log(ename);
            });
        }, 5000);
    })();
});

leak the figure shows 4 Window instances, but there are hundreds in my actual app.

no leak this one is no leaking version

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
domeniccommented, Apr 28, 2016

There have also been some great fixes in vm memory usage in Node v6; you should see if upgrading fixes the issue.

1reaction
Sebmastercommented, Apr 27, 2016

Your testing code is wrong I think. There’ll be at least 1 window around because you save your window with a var which is still referenced when the GC runs (because function scoping). If google has a frame somewhere, that might explain the additional instances (although I’m not sure about that).

Read more comments on GitHub >

github_iconTop Results From Across the Web

A memory leak when capturing callb… - Apple Developer
A memory leak when capturing callback function from block of setTerminationHandler of NSTask.
Read more >
java - How to avoid memory leaks in callback? - Stack Overflow
The best way to ensure that callbacks are garbage collected promptly is to store only weak references to them, for instance, by storing...
Read more >
Causes of Memory Leaks in JavaScript and How to Avoid Them
In this article, we will explore programming patterns that cause memory leaks in JavaScript and explain how to improve memory management.
Read more >
A quick story about async callbacks, memory leaks ...
My co-worker told me the following: “You said there can be a memory leak here. I looked for a solution and saw that...
Read more >
Memory Leaks in JavaScript and how to avoid them. - Medium
We just need to remember that memory is limited , so when it comes to call stack and memory heap ,those are two...
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