4

我该怎么做才能将 ram 保持在合理的水平?
在我启动服务器之前,我有大约 140mb 的可用内存。
16 小时后,我还剩下大约 4mb 的空闲内存。
我在具有 256mb 内存的机架空间云上运行它。

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;}
4

2 回答 2

1

看起来像 socket.on('join') 点的问题。

我建议你开始使用

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

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

所以你现在知道你的泄漏从哪里开始。

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

如果您有任何问题,请告诉我。

于 2012-06-17T21:47:46.857 回答
0

有些人认为 socket.io 在使用 websockets 传输时会泄漏内存。尝试禁用它。类似于以下内容:

io.configure('production', function(){
  io.enable('browser client etag');
  io.set('log level', 1);

  io.set('transports', [
  , 'htmlfile'
  , 'xhr-polling'
  , 'jsonp-polling'
  ]);
});

heroku也必须说以下

于 2012-06-17T23:34:48.393 回答