对象内存泄漏(当nodejs配置为最大512M时,Heroku为1.5 GB内存使用率)

时间:2018-05-03 15:52:38

标签: node.js express heroku memory-leaks javascript-objects

代码非常简单,应该从内存中释放对象,此应用程序中大约有20个并发用户。

代码的想法是sessionID是存储对象的关键,当会话结束时,对象被释放。

CODE

'use strict'
const http = require('http')
const app = require('./config')
const Server = http.Server(app)
const PORT = process.env.PORT || 8000
const io = require('socket.io')(Server)

Server.listen(PORT, () => console.log('Game server running on:', PORT))

const players = {}
const moneys = {} 

var util = require('util');

io.on('connection', socket => {
    // When a player connects
    socket.on('new-player', state => {
    //console.log(socket.id+'New player joined with state:', state)
    players[socket.id] = state
    // Emit the update-players method in the client side
    io.emit('update-players', players)

    var pCount = Object.keys(players).length;
    console.log("players in mem "+pCount);      
  });

   socket.on('new-money', state => {
      moneycounter++
      moneys[socket.id] = state
      // Emit the update-players method in the client side
      if (moneycounter >2) {
          io.emit('update-moneys', moneys)
          moneycounter = 0
      }
  });

  socket.on('disconnect', state => {
      console.log(socket.id+' player discontinue with state:', state)

    try {
        if (typeof players[socket.id].playerName != 'undefined') delete players[socket.id].playerName
        if (typeof players[socket.id].speed != 'undefined') delete players[socket.id].speed
    }
    catch (err) {
        console.log(socket.id+' typeerror deleting 1')
    }
    delete players[socket.id]
    delete moneys[socket.id]
     //  io.emit('update-players', players)
  });

  // When a player moves
  socket.on('move-player', data => {
    //console.log(' move player' +socket.id)
    const { x, y, angle, playerName, speed } = data

    // If the player is invalid, return
    if (players[socket.id] === undefined) {
       console.log(socket.id+' invalid move')

      try {
         if (typeof players[socket.id].playerName != 'undefined') delete players[socket.id].playerName
         if (typeof players[socket.id].speed != 'undefined') delete players[socket.id].speed
       }
       catch (err) {
          console.log(socket.id+' typeerror 3 deleting')
       }

       delete players[socket.id]
       delete moneys[socket.id]
       socket.disconnect();
       return
    }

    // Update the player's data if he moved
    players[socket.id].x = x
    players[socket.id].y = y
    players[socket.id].angle = angle
    players[socket.id].playerName = {
      name: playerName.name,
      x: playerName.x,
      y: playerName.y
    }
    players[socket.id].speed = {
      value: speed.value,
      x: speed.x,
      y: speed.y
    }

    // Send the data back to the client
    io.emit('update-players', players)
  })
})

由于某些原因,我的应用程序开始~20mb ram 但是后来同一天占用了1.5GB,导致内存R14错误。

heroku 2hours log

我的配置应该像下面这样限制为512:

  

web node --optimize_for_size --max_old_space_size = 460   --gc_interval = 100 server / index.js

我当前的配置是2X。 (1024 mb计划)

如果发生内存错误R14,是否可以选择将heroku配置为重新启动。

1 个答案:

答案 0 :(得分:2)

您可以使用node-memwatch程序包来监视内存泄漏,当内存泄漏时,您可以从代码本身重新启动应用程序。

Restart a node.js app from code level