socket.io 1.3.3版本内存泄露问题
发布于 1个月前 作者 JamCode 360 次浏览 来自 问答

各位有使用socket.io出现内存泄露问题吗,我的用法是一个cluster启动一个子进程socket.io来接收前端连接,提供实时消息服务

框架是这样的 /**

if (cluster.isMaster) { for (var i = 0; i < 1; ++i) { cluster.fork();

    cluster.on('exit', function(worker, code, signal) {
        log.error('socket worker ' + worker.process.pid + ' died, code is ' + code + ", signal is " + signal, log.getFileNameAndLineNum(__filename));
        cluster.fork();

        email.sendMail('socket worker ' + worker.process.pid + ' died');

    });

    cluster.on('listening', function(worker, address) {
        log.info("A socket worker with pid#" + worker.process.pid + " is now listening to:" + address.port,
            log.getFileNameAndLineNum(__filename));
    });
}

} else {

process.on('uncaughtException', function(err) {
    log.error('SOCKET SERVER Caught exception: ' + err.stack, log.getFileNameAndLineNum(__filename));
    email.sendMail('SOCKET SERVER Caught exception: ' + err.stack);
});

startSocketServer();

}

function startSocketServer() {

var io = require('socket.io').listen(socketPort);
log.logPrint(config.logLevel.DEBUG, "start listen socket");
io.sockets.on('connection', function(socket) {

    log.logPrint(config.logLevel.DEBUG, process.pid + " get the socket:" + socket.id);

    //do some event

    socket.on('disconnect', function(msg) {
        log.debug(msg, log.getFileNameAndLineNum(__filename));
        
        //delete io.sockets.sockets[socket.id];
        //delete socket;
        //socket.close();
    });
});

}

我的测试客户端每次建立一个连接,就会消耗内存,但是每次disconnect之后,内存不会释放掉,就这样一直增长下去,直到这个进程内存耗用过大被系统杀掉

是否在socket.io执行disconnect事件的时候需要做一些处理???

5 回复

同问,今天测试时候也遇见了这问题

@mengao89 这问题困扰我一个月了

没人遇到吗???

Mark 起来,明天试试

回到顶部