Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much memory does an inet stream socket use in Node.js?

Of course data can be buffered and grow if the client is too slow to read the server's writes [1].

But what is the default buffer size? I assume it's whatever is configured in /proc/sys/net/ipv4/tcp_rmem and tcp_wmem (assuming Linux)...

I'm trying to do some basic capacity planning. If I have a VPS with 512 MB RAM, and I assume the OS et al will use ~ 100MB, my app has ~ 400MB for whatever it wants to do. If each connected client (regular old TCP/IP socket) requires say 8KB (4KB read, 4KB write) by default, I have capacity for 400MB / 8KB = ~ 50000 clients.

[1] http://nodejs.org/docs/v0.4.7/api/all.html#socket.bufferSize

like image 956
horton Avatar asked Feb 27 '26 22:02

horton


1 Answers

I don't know off the top of my head and it probably varies from platform to platform but here's how you can find out!

Use this code:

var net = require('net');

net.createServer(function (socket) {
    socket.on('data', function(data) {
      console.log('chunk length: ' + data.length);
    });
}).listen(function() {
    console.log("Server listening on %j", this.address());
});

And then cat a large file (like an ISO) through 'nc localhost $port' using the port number that the script spits out when it starts up, and watch the output to see what the largest chunk size is. On my OS X machine, it looks like the largest buffer is 40960 bytes, but it might be different on yours.

like image 142
clee Avatar answered Mar 02 '26 11:03

clee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!