exebook exebook - 3 months ago 10x
Node.js Question

Why does writing to stdout in a hot loop cause an out of memory shutdown?

for (i = 0; i < 3e11; i++) {

Or this code:

for (i = 0; i < 3e11; i++) {

Just run in a terminal like this:
node test.js

After about one minute produces this:

<--- JS stacktrace --->
Cannot get stack trace in GC.
FATAL ERROR: MarkCompactCollector: semi-space copy, fallback in old gen Allocation failed - JavaScript heap out of memory
Aborted (core dumped)

Where does it eat memory?

$ node -v


It's covered in this bug: https://github.com/nodejs/node/issues/1741

It's considered an open bug in node.js. It seems to have to do with an asynchronous property of stdout and can be fixed with:


I'm honestly confused since I understood console.log was blocking. The issue may be more subtle than "blocking" v. "not blocking," I imagine it's blockingly writing to a buffer that's nonblockingly writing to out, but not sure. (Consider this answer to be incomplete.)