Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Streams: Is the .on('data', ...) same as .pipe(..., { end: false}) ? #25343

Closed
edin-m opened this issue May 17, 2015 · 1 comment
Closed

Streams: Is the .on('data', ...) same as .pipe(..., { end: false}) ? #25343

edin-m opened this issue May 17, 2015 · 1 comment

Comments

@edin-m
Copy link

edin-m commented May 17, 2015

I have read about streams multiple times and I have tried and made some of my own small streams.

I have created stream of zeroes (with buffers) that are sent over TCP on TCP client's command.
Stream is generated by readable stream.

However if I use:

conn.zeroStream = new ZeroStream(size);
conn.zeroStream.on('data', function(data) { conn.write(data); });

memory usage goes to infinity with OS shutting down the process.

And if I use

conn.zeroStream.pipe(conn, { end: false });

I get no memory performance issues.
Am I misunderstanding streams or is this potentially real memory leak.

https://gist.github.com/edin-m/2c1391e36a0ee6a9c084

@edin-m edin-m changed the title Streams: Is the .on('data', ...) same as .pipe(..., { end: false}) Streams: Is the .on('data', ...) same as .pipe(..., { end: false}) ? May 17, 2015
@rlidwka
Copy link

rlidwka commented May 17, 2015

I suspect that pipe() slows ZeroStream down requesting data only when it's needed. But on('data') callback fires as soon as any data is available. Thus, in case of on('data') you'll get 100mb of zeroes all at once, but with pipe() you'll generate zeroes when previous data is sent over the wire.

I see no memory leak there, just an inefficient use of the memory. It should get freed up when tcp connection is closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants