Skip to content

Instantly share code, notes, and snippets.

@misterdjules
Created September 13, 2014 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save misterdjules/0800a480220fe445bd5a to your computer and use it in GitHub Desktop.
Save misterdjules/0800a480220fe445bd5a to your computer and use it in GitHub Desktop.
Memory leak fix for lab 4 of nodeconf's dtrace + mdb workshop
diff --git a/leak2.js b/leak2.js
index e4b6a47..7fe4c18 100644
--- a/leak2.js
+++ b/leak2.js
@@ -7,15 +7,16 @@ result = null;
console.log("STARTING");
process.nextTick(function run() {
- var script = vm.createScript('setInterval(function() {}, 0);', 'test.js');
+ var script = vm.createScript('var myInterval = setInterval(function() {}, 0); clearInterval(myInterval); ', 'test.js');
var sandbox = { setInterval: setInterval };
- script.runInNewContext(sandbox);
+ script.runInThisContext();
total--;
if (total) {
/* process.nextTick(run); */
- setTimeout(function() {
+ var myTimeout = setTimeout(function() {
run();
+ clearTimeout(myTimeout);
}, 1000);
} else {
console.log("COMPLETE");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment