A set of closures is created in your code. This prevents the release of memory. In your example, memory will be released after all timeouts have completed.
This can be seen (after 100 seconds):
var index = 0; var timeout; function leak() { index++; timeout = setTimeout(leak, 0); } leak(); setTimeout(function() { clearTimeout(timeout); }, 100000); setInterval(function() { console.log(process.memoryUsage()); }, 2000);
source share