Created
September 15, 2018 09:28
-
-
Save Stwissel/5da33222bad880952d40eec341719f22 to your computer and use it in GitHub Desktop.
Simple nodeJS function to crash with a stackoverflow
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
/* This program will use a recursion to provoke a stackoverflow | |
use it at your own risk. It might take more than the runtime down. | |
*/ | |
const looper = function(instanceCount) { | |
console.log( | |
`Calling instance ${instanceCount} with ${process.memoryUsage().heapUsed}` | |
); | |
var body = ''; | |
for (var i = 0; i < 1000000; i++) { | |
body += 'Big fat body, big fat body, really big and fat\n'; | |
} | |
instanceCount++; | |
looper(instanceCount); | |
// Need to use body after the recursion to avoid tail | |
// optimization that just would recycle body | |
// It never gets executed | |
console.log(`String size is ${body.length}`); | |
}; | |
// Event loop to stop if we panic | |
const stdin = process.stdin; | |
console.log('Press the famous anykey to stop the madness'); | |
stdin.resume(); | |
stdin.on('data', d => { | |
console.log('Good bye!'); | |
process.exit(); | |
}); | |
// Start the whole mess | |
looper(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment