Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loop of console.log causes out of memory #3524

Closed
tforgione opened this issue Oct 26, 2015 · 8 comments
Closed

Loop of console.log causes out of memory #3524

tforgione opened this issue Oct 26, 2015 · 8 comments
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. invalid Issues and PRs that are invalid. memory Issues and PRs related to the memory management or memory footprint. question Issues that look for answers.

Comments

@tforgione
Copy link

The other day, I went through something that I didn't understand in node.

var i = 0;
for(;;)
    console.log(i++)

When I do this, at a certain moment, my nodejs just stops printing stuff, giving me an output that looks like this

[...]
684665
684666
684667

And after a while, I got this :

<--- Last few GCs --->

   69097 ms: Scavenge 1397.2 (1456.7) -> 1397.2 (1456.7) MB, 0.8 / 0 ms (+ 1.7 ms in 1 steps since last GC) [allocation failure] [incremental marking delaying mark-sweep].
   70462 ms: Mark-sweep 1397.2 (1456.7) -> 1396.0 (1456.7) MB, 1364.9 / 0 ms (+ 2.8 ms in 2 steps since start of marking, biggest step 1.7 ms) [last resort gc].
   71833 ms: Mark-sweep 1396.0 (1456.7) -> 1397.1 (1456.7) MB, 1370.2 / 0 ms [last resort gc].


<--- JS stacktrace --->

==== JS stack trace =========================================

Security context: 0xcdf79d37399 <JS Object>
    1: formatPrimitive(aka formatPrimitive) [util.js:~411] [pc=0x634d9f4113f] (this=0xcdf79d04131 <undefined>,ctx=0x17b18f4d561 <an Object with map 0x32fd25043ef9>,value=16248021)
    2: formatValue(aka formatValue) [util.js:223] [pc=0x634d9f1fdbb] (this=0xcdf79d04131 <undefined>,ctx=0x17b18f4d561 <an Object with map 0x32fd25043ef9>,value=16248021,recurseTimes=2)
    3: inspect(aka inspect) [uti...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
[1]    19446 abort (core dumped)  node

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

@bnoordhuis
Copy link
Member

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();

@bnoordhuis bnoordhuis added invalid Issues and PRs that are invalid. question Issues that look for answers. labels Oct 26, 2015
@tforgione
Copy link
Author

Thanks for the clarification,

I had a lot of trouble to find wether console.log was sync or async (for example this post says that Starting with Node 0.6 [...] stdout is synchronous now.

@bnoordhuis
Copy link
Member

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.)

@tforgione
Copy link
Author

I see, thank you for taking time to answer my questions !

@ChALkeR ChALkeR added duplicate Issues and PRs that are duplicates of other issues or PRs. memory Issues and PRs related to the memory management or memory footprint. labels Nov 2, 2015
@ChALkeR
Copy link
Member

ChALkeR commented Nov 2, 2015

This is in fact a duplicate of #1741.

@litmit
Copy link

litmit commented Feb 26, 2017

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 using console.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?

@sam-github
Copy link
Contributor

@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.

@litmit
Copy link

litmit commented Mar 1, 2017

@sam-github
As decribed by @Fishrock123 in comments to #11568 the console should be synchronous when stdout redirected to a file.
But

node console-loop.js > out.txt

work 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 console should just work in any case (IMHO of course).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate Issues and PRs that are duplicates of other issues or PRs. invalid Issues and PRs that are invalid. memory Issues and PRs related to the memory management or memory footprint. question Issues that look for answers.
Projects
None yet
Development

No branches or pull requests

5 participants