server.js
var net=require(“net”)
var server = net.createServer(function (socket) { socket.on("error",function(e){ console.log(e) }) });
server.listen(7888,function() { address = server.address(); console.log("opened server on %j", address); });
client.js
var net=require(“net”) max=100000 count=0 client=net.createConnection(7888,function(){ console.log(“con”) buf = new Buffer(16*1024).toString() for(i=0;i<max;i++){ b = client.write(buf,function(){ count++; if(count==max){ console.log(“end”) client.end() } }) } })
setTimeout(function(){ console.log(1) },100000)
当 我看到end 时内存 top -p pid ,发现内存rss 1.3g 一直不下去了。
但是当我把 buf = new Buffer(161024).toString() 变成 buf = new Buffer(161024) 时内存就很小
大侠能给解释一下么
值得怀疑的地方 src/stream_wap.cc:346
char * storage = new char[sizeof(WriteWrap)+storgize+15] this is temporary
in src/stream_wap.cc:463
MakeCallback(); --> client.write(buf,function(){ count++; if(count==max){ console.log(“end”) client.end() } }) req_wrap->~WriteWrap(); delete[] reinterpret_cast(req_wrap)
var net=require(“net”) max=100000 count=0 buf = new Buffer(16*1024).toString() client=net.createConnection(7888,function(){ write() })
function write(){ client.write(buf,function(){ count++ if(count==max){ console.log(“e”) return; } write() })
}
setTimeout(function(){ console.log(1) },1000000000)
这么做就是正确的,不知道为什么那里占用内存了