当前位置 : 主页 > 网页制作 > Nodejs >

node.js – socket.io随着时间的推移RAM使用率上升

来源:互联网 收集:自由互联 发布时间:2021-06-16
我能做些什么来将ram保持在合理的水平? 在我启动服务器之前,我有大约140mb ram free. 16个小时后,我剩下大约4mb的免费ram. 我在256mb ram的机架空间云上运行它. var maxMsgs = 50;var express = requ
我能做些什么来将ram保持在合理的水平?
在我启动服务器之前,我有大约140mb ram free.
16个小时后,我剩下大约4mb的免费ram.
我在256mb ram的机架空间云上运行它.

var maxMsgs = 50;
var express = require('express'), sio = require('socket.io'), redis = require('redis'), RedisStore = require('socket.io/lib/stores/redis');
var app = express.createServer(), pub = redis.createClient(), sub = redis.createClient(), client = redis.createClient();
app.configure(function () {
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(express.static(__dirname + '/public'));
  app.use(app.router);
});

app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
});
app.listen(8002, function () {
  var addr = app.address();
  console.log('app listening on http://' + addr.address + ':' + addr.port);
});

var io = sio.listen(app, {log: false}), nicknames = {}, history = [], user_count = 0, topic = {topic: '', setBy: 'Server'}, ytvid = {vid: '', setBy: 'Server'};
io.enable('browser client minification');
io.enable('browser client etag');
io.enable('browser client gzip');
io.set('store', new RedisStore({redisPub : pub, redisSub : sub, redisClient : client}));
//io.set('resource', 'socket');

io.sockets.on('connection', function(socket) {
  socket.on('join', function(cu) {
    if(cu.username && cu.username != 'Guest') {
      socket.nickname = cu.username;
      socket.emit('connected', nicknames, history, topic, ytvid);
      nicknames[cu.username] = cu;
      socket.broadcast.emit('nicknames', nicknames);
      user_count++;
      //socket.broadcast.emit('announcement', {msg: socket.nickname + ' connected'});
    }
  });
  socket.on('message', function(msg, cb) {
    if(msg.msg && msg.msg != '') {
      msg.time = Date.now() / 1000;
      history.push(msg);
      while(history.length > maxMsgs) history.shift();
      cb(true, msg.time);
      socket.broadcast.emit('message', msg);
    }
  });
  socket.on('stopic', function(t) {
    if(t.topic && t.setBy && t.topic != '') {
      topic = t;
      io.sockets.emit('topic', t);
    } else {
      topic = {topic: 'No topic set', setBy: 'Admin'};
      io.sockets.emit('topic', topic);
    }
  });
  socket.on('sytvid', function(v) {
    if(v.vid && v.setBy && v.vid != '') {
      ytvid = v;
      io.sockets.emit('ytvid', v);
    } else {
      ytvid = {vid: false, setBy: 'Admin'};
      io.sockets.emit('ytvid', ytvid);
    }
  });
  socket.on('get debug', function() {
    socket.emit('debug', {users: nicknames, history: history, user_count: user_count, topic: topic});
  });
  socket.on('send command', function(c) {
    if(c.type == 'empty') history = [];
    io.sockets.emit('command', c);
  });
  socket.on('disconnect', function() {
    if(!socket.nickname) return;
    if(!nicknames[socket.nickname]) return;
    //nicknames[socket.nickname].status = 'offline';
    delete nicknames[socket.nickname];
    //socket.broadcast.emit('announcement', {msg: socket.nickname + ' disconnected'});
    socket.broadcast.emit('nicknames', nicknames);
    user_count--;
  });
});

function inArray(needle,haystack){for(var key in haystack){if(needle===haystack[key]){return true;}}return false;}
function zeroPad(digits,n){n=n.toString();while(n.length<digits){n='0'+n;}return n;}
function time(time){if(time==null)time=new Date();else if((time instanceof Date)===false)time=new Date(time);return time;}
在socket.on(‘join’)点看起来像问题.

我建议你开始使用

var profiler = require('v8-profiler');
setInterval(function() {
    profiler.takeSnapshot('snappy');
},1000);

像这里描述的http://code.google.com/p/v8/wiki/V8Profiler

所以你现在将在哪里开始泄漏.

还要仔细检查每个变量,对象和范围的分配和释放.

如果您有疑问,请告诉我.

网友评论