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
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.
精彩评论