websocket 模块 ws 有用过的朋友讨论下
发布于 2个月前 作者 cxh80301983 364 次浏览 来自 问答

github地址:https://github.com/einaros/ws websocket 模块 内存泄露问题: server端 code: var WebSocketServer = require(‘ws’).Server; var m = require(“memwatch”) var wss = new WebSocketServer({ port: 8101 }); setInterval(function (argument) { var mem = process.memoryUsage(); console.log('clients’+wss.clients.length+’rss:’, Math.round((mem.rss / 1024/1024)) + “MB”); },5000)

wss.on('connection’, function(ws) { ws.on("close",function(err){

 })

});

客户端代码: var WebSocket = require(‘ws’); run(); function run() { if (count > 1000) return; var ws = new WebSocket(‘ws://localhost:8101’); ws.on('open’, function() { count++; console.log(‘count=’ + count); socket.push(ws); setInterval(function(){ //online.data.uid = count; //var online_str = JSON.stringify(online); //console.log(‘send:’ + online_str); ws.send(online_str, function(err){ //if (err) console.log(err); //else //console.log(‘send success’); })}, 1); run(); }); ws.on('message’, function(data, flags) {

});

}

断开后 内存没有释放,不断的创建链接 不断的增加

2 回复

应该是依赖于V8的垃圾回收吧。。

v8的垃圾回收比较懒, 如果有需求,可以手动调用gc. https://cnodejs.org/topic/547c509e0ae47dec03aa2954

回到顶部