console.log memory leak ?

Showing 1-5 of 5 messages
console.log memory leak ? Filirom1 4/10/12 7:43 AM
Hi, 

I am struggling to understand why NodeJs do not decrease the resident memory after a lot of console.log.

If someone could explain me this behaviour I would be very happy.

    for (var i = 0; i< 100 * 1000; i++){
      console.log(i);
    }
        
    setTimeout(function(){ console.log('END') }, 10000000);

The last setTimeout is used to keep nodejs running, so gc happens when node is idle.

The ouput is the following: 

    $ while true; do sleep 1; cat /proc/`pgrep node`/status |grep RSS; done &
    $ node --trace_gc log.js
    
    ...
    99998
    99999
    VmRSS:   88808 kB
    VmRSS:   88808 kB
    VmRSS:   88808 kB
    VmRSS:   88808 kB
    VmRSS:   88808 kB
    Mark-sweep 39.5 -> 2.4 MB, 3 / 37 ms.
    VmRSS:   57120 kB
    Mark-compact 2.4 -> 1.9 MB, 24 ms.
    Mark-compact 1.9 -> 1.9 MB, 8 ms.
    VmRSS:   30344 kB
    VmRSS:   30344 kB
    VmRSS:   30344 kB


If I only do 1000 iterations (instead of 100 000), the final VmRSS (after gc) is less important: 

    ...
    998
    999
    VmRSS:   12084 kB
    VmRSS:   12084 kB
    VmRSS:   12084 kB
    VmRSS:   12084 kB
    VmRSS:   12084 kB
    Scavenge 2.4 -> 2.2 MB, 0 ms.
    Mark-sweep 2.2 -> 1.9 MB, 4 ms.
    Mark-compact 1.9 -> 1.9 MB, 15 ms.
    VmRSS:    9468 kB
    VmRSS:    9468 kB

This thread is cross posted in stack overflow : http://stackoverflow.com/questions/8856892/node-js-file-memory-leak
Re: [nodejs] console.log memory leak ? Ben Noordhuis 4/10/12 7:57 AM
On Tue, Apr 10, 2012 at 16:43, Romain <fili...@gmail.com> wrote:
> Hi,
>
> I am struggling to understand why NodeJs do not decrease the resident memory
> after a lot of console.log.
>
> If someone could explain me this behaviour I would be very happy.
>
>     for (var i = 0; i< 100 * 1000; i++){
>       console.log(i);
>     }
>
>     setTimeout(function(){ console.log('END') }, 10000000);
>
> The last setTimeout is used to keep nodejs running, so gc happens when node
> is idle.

This question has been asked in various forms on both the mailing list
and the bug tracker (and probably SO as well).

Each console.log() statement allocates some memory that is not
reclaimed until the next tick of the event loop. If you slice up the
loop with process.nextTick() into 100 or 1000 segments, memory usage
will be much more constant.

Re: [nodejs] console.log memory leak ? Filirom1 4/10/12 8:54 AM
Memory increase it's ok.

But I was thinking that the memory allocated by all the console.log would be free after 5 minutes of idle time.

I don't understand why I need to segment the loop, if I wait a very long time after the loop.

2012/4/10 Ben Noordhuis <in...@bnoordhuis.nl>

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nod...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Re: [nodejs] console.log memory leak ? Ben Noordhuis 4/10/12 9:17 AM
On Tue, Apr 10, 2012 at 17:54, Romain <fili...@gmail.com> wrote:
> Memory increase it's ok.
>
> But I was thinking that the memory allocated by all the console.log would be
> free after 5 minutes of idle time.
>
> I don't understand why I need to segment the loop, if I wait a very long
> time after the loop.

Oh, that's another thing: RSS doesn't work like that. free()ing memory
doesn't mean it's returned to the operating system. The reasons for
that are kind of complicated but google for keywords like 'heap
fragmentation' or 'memory pressure' and you'll understand why.

The bottom line is that RSS is not a very reliable metric for
measuring a program's actual memory usage.

Re: [nodejs] console.log memory leak ? Filirom1 4/10/12 11:41 AM
Many thanks, it's exactly what I was looking for.