Repository navigation
Loop of console.log causes out of memory #3524
Description
Activity
It's expected behavior: console.log is asynchronous, the memory associated with each call cannot be reclaimed until the next tick of the event loop. In your example that next tick never happens because of the infinite loop. If you rewrite your example to a callback-driven approach, it keeps running forever:
let i = 0; const next = () => process.stdout.write(`${i++}\n`, next); next();
- addedinvalidIssues and PRs that are invalid.Issues and PRs that are invalid.questionIssues asking questions about Node.js.Issues asking questions about Node.js.
on Oct 26, 2015 Thanks for the clarification,
I had a lot of trouble to find wether
console.logwas sync or async (for example this post says that Starting with Node 0.6 [...] stdout is synchronous now.Starting with Node 0.6 [...] stdout is synchronous now.
That was a half-truth, it wasn't always synchronous. In v4.x it's always asynchronous except when stdio is redirected to file (which can block but normally won't.)
I see, thank you for taking time to answer my questions !
- addedduplicateIssues and PRs that are duplicates of other issues or PRs.Issues and PRs that are duplicates of other issues or PRs.memoryIssues and PRs related to Node.js memory management or memory footprint.Issues and PRs related to Node.js memory management or memory footprint.
on Nov 2, 2015 This is in fact a duplicate of #1741.
Any suggestion to work around this BUG?
I'm using Node for small console app. No asynchronous code at all.
For debug purposes i'm always usingconsole.log().
All working well in node v4.8.0 but crashes in node v6.x with message
FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory@bnoordhuis It is possible to write synchronous applications with modern Node.js?
@litmit Node is an asynchronous I/O engine, with a bit of sync support in some places. If you read the issue @ChALkeR pointed you to, you would find at least one workaround: #1741 (comment)
Or, you can write async code.
Reacted by Wyatt Arent@sam-github
As decribed by @Fishrock123 in comments to #11568 the console should be synchronous when stdout redirected to a file.
Butnode console-loop.js > out.txtwork properly on Node v4 and crash on latest Node v6.
Currently I'm using console-sync as workaround.PS. I known well that Node is an asynchronous I/O engine. But such simple thing like
consoleshould just work in any case (IMHO of course).
The other day, I went through something that I didn't understand in node.
When I do this, at a certain moment, my nodejs just stops printing stuff, giving me an output that looks like this
And after a while, I got this :
I was wondering, what can console.log do that could lead to an out of memory error ?
At first, I asked that question on stackoverflow, but someone suggested that I create an issue here, and I thought it could be a good idea.
What do you think about this ?
EDIT : Apparently, it has also be discussed here