Created
September 21, 2015 19:58
-
-
Save srikumarks/863d656c52109da9a537 to your computer and use it in GitHub Desktop.
Sneaky memory leak in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeBigObject() { | |
var big = new Float64Array(10000000); // Allocate big object. | |
return { | |
one: function () { return big.length; }, // Has ref to big object. | |
two: function () { return "hello"; } // Doesn't have ref to big object. | |
}; | |
} | |
function leak() { | |
var l = makeBigObject(); // l has ref to big object via "one". | |
delete l.one; // Delete the "one" property. | |
return l; // The big object is not supposed to be | |
// reachable via l. However, l.two holds a | |
// ref to the big object via its closure | |
// scope. | |
} | |
var thing = []; | |
setTimeout(function doitagain() { | |
thing.push(leak()); // big object allocated in makeBigObject() is | |
// supposed to be unreachable from thing. | |
setTimeout(doitagain, 1000); | |
}, 1000); | |
// Watch your system die! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment