简体   繁体   中英

iisnode node.js socket.io unexpected memory leak

my company is struggling with some issues. The node.exe is taking up too much memory and CPU.

The server looks like this:

var userList = [];

/* NODE.JS */
var server = require('http').createServer(function (req, res) {
    res.writeHead(200);
    res.end('ok');
}).listen((process.env.PORT || 88));

/* SOCKET.IO */
//var io = require('socket.io')(server);
var io = require('socket.io')(server);

io.on('connection', function (socket) {
    socket.on('connectUser', function (user) {
        if (!isAuthenticated(user.user, user.pass)) {
            socket.disconnect(socket.conn.id);
        } else {
            //Session des Users
            user.session = socket.conn.id;
            //if (userList.indexOf(user)== -1)
            userList.push(user);
            io.emit('userOnline', {onlineUser:  userList});
       }
    });

    socket.on('sendMessageToUser', function (sendObj) {
            if(sendObj && sendObj.to)   {
                var userArr = userList.filter(function(val, index, arr) {
                    if(sendObj.to.indexOf(val.id) > -1)
                        return val;
                });

                userArr.forEach(function(user) {
                   socket.to(user.session).emit('getMessageFromUser', sendObj);
                });
            }
    });

    socket.on('disconnect', function () {
        userList = userList.filter(function(val, index, arr) {
            if(val.session !== socket.conn.id)
                return val;
        });

        io.emit('userOnline', {onlineUser:  userList});
    });
});

function isAuthenticated(user, pass) {
    return true;
}

When a user connects (each page visit) will be a connect to the server. A userList will be filled with information about that user. After leaving the page or refresh the user is removed from that object.

The server also hosts another node.js application. After starting my script the other application is rising in memory and CPU also!

Can anyone confirm that this is a bug? Or do I have to treat two applications different?

My server looks like this: iisnode (v 0.2.18.0) node (v 0.12.4) npm (v 2.10.1) socket.io (v 1.3.5)

I know that most of them are really outdated! I hadn't had time to test current versions under iisnode.

How are users removed? You might have a memory leak there, if not done properly.

Best thing is to profile your node processes and see what values are being retained, they will point you to the cause of the memory leak. You can read more on how to do that here: http://www.alexkras.com/simple-guide-to-finding-a-javascript-memory-leak-in-node-js/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM