"done" callback cause memory leak
See original GitHub issueI 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);
})();
});
the figure shows 4 Window instances, but there are hundreds in my actual app.
this one is no leaking version
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
There have also been some great fixes in vm memory usage in Node v6; you should see if upgrading fixes the issue.
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).