Skip to content

Loop of console.log causes out of memory #3524

Description

@tforgione

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

Activity

  1. bnoordhuis commented on Oct 26, 2015

    @bnoordhuis
    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();
  2. added
    invalidIssues and PRs that are invalid.
    questionIssues asking questions about Node.js.
    on Oct 26, 2015
  3. tforgione commented on Oct 26, 2015

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

  4. bnoordhuis commented on Oct 26, 2015

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

  5. tforgione commented on Oct 26, 2015

    @tforgione
    Author

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

  6. added
    duplicateIssues and PRs that are duplicates of other issues or PRs.
    memoryIssues and PRs related to Node.js memory management or memory footprint.
    on Nov 2, 2015
  7. ChALkeR commented on Nov 2, 2015

    @ChALkeR
    Member

    This is in fact a duplicate of #1741.

  8. litmit commented on Feb 26, 2017

    @litmit

    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?

  9. sam-github commented on Feb 28, 2017

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

  10. litmit commented on Mar 1, 2017

    @litmit

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateIssues and PRs that are duplicates of other issues or PRs.invalidIssues and PRs that are invalid.memoryIssues and PRs related to Node.js memory management or memory footprint.questionIssues asking questions about Node.js.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions