Friday, 4 September 2020

Strange Nodejs memory leak

Hey I experience strange memory leak on my nodejs server hosted in heroku. I try to find the leak for two days and after I remove my functions one by one I find that this one causes the problem but I'm still not sure where exactly is the problem. I can clearly see that I have a memory leak from heroku metrics. enter image description here Do someone have an idea why this func produce memory leak. Thanks

completeTicTacToeGame: function (game, winnerIndex, reasonForFinish, userId) {
    if (userId) {
      User.findOne({
        _id: userId
      }, function (err, user) {

        if (err || user == null) {
        }
        else {
          userLivesController.removeUserHeartWithoutResponeAndSaving(user, 'ticTacToeHearts', function (user, canPlay) {
            if (canPlay) {
              dateFormatterController.checkIfDateIsToday(user.lastCompitedTicTacToeGame, function (isToday, isPrevious) {
                var earnCredits = 0
                if (winnerIndex == 0) { earnCredits = 1 }
                if (winnerIndex == 1) { earnCredits = 4 }

                user.credits = user.credits + earnCredits
                user.lifetimeCredits = user.lifetimeCredits + earnCredits


                //increase ad today counter
                user.lastCompitedTicTacToeGame = (new Date()).getTime().toString()

                userBadgesController.checkIfUserNeedToWinBadgeForCredits(user, function (user) {

                  user.save(function (err, user) {
                    if (err) {
                      errorHandlingController.generalSendErrorWithMessage(req, res, 'serverError')
                    }
                    else {
                      var canPlayMoreGames = user.ticTacToeHearts > 0
                      if (earnCredits > 0) {
                        notificationController.sendNotificationToUserForNewTicTacToeGame(user, earnCredits)
                        rankingController.updateRankings(user, earnCredits, 'ticTacToe')
                      }

                      var canWatchAdToDoubleCredits = false
                      if (user.gamesWithoutAd >= ConfigParams.numberOfGamesForAd() && earnCredits > 0) {
                        canWatchAdToDoubleCredits = true
                      }
                      var timeLeftToNextHeart = (((ConfigParams.minutesForHeart() * 60000)) + parseInt(user.lastGivenHeart)) - ((new Date()).getTime())

                      var haveMaxHearts = user.mathGameHearts == ConfigParams.maxHearts() && user.memoryHearts == ConfigParams.maxHearts() && user.ticTacToeHearts == ConfigParams.maxHearts()
                      var canWatchVideoForHeart = true && haveMaxHearts == false
                      if (user.lastWatchedVideoForHeart) {
                        var canWatchVideoForHeart = (((ConfigParams.minutesForHeartWatchedAd() * 60000)) + parseInt(user.lastWatchedVideoForHeart)) - ((new Date()).getTime()) < 0 && haveMaxHearts == false
                      }

                      module.exports.sendMessageToSocketForComlitedGame(game, user._id.toString(), user.credits, earnCredits, canPlayMoreGames, canWatchAdToDoubleCredits, reasonForFinish, user.ticTacToeHearts, ConfigParams.maxHearts, null, user.gamesWithoutAd >= ConfigParams.numberOfGamesForAd() - 1, timeLeftToNextHeart, canWatchVideoForHeart)
                    }
                  })
                })
              })
            }
            else {
              var timeLeftToNextHeart = (((ConfigParams.minutesForHeart() * 60000)) + parseInt(user.lastGivenHeart)) - ((new Date()).getTime())

              var haveMaxHearts = user.mathGameHearts == ConfigParams.maxHearts() && user.memoryHearts == ConfigParams.maxHearts() && user.ticTacToeHearts == ConfigParams.maxHearts()
              var canWatchVideoForHeart = true && haveMaxHearts == false
              if (user.lastWatchedVideoForHeart) {
                var canWatchVideoForHeart = (((ConfigParams.minutesForHeartWatchedAd() * 60000)) + parseInt(user.lastWatchedVideoForHeart)) - ((new Date()).getTime()) < 0 && haveMaxHearts == false
              }

              module.exports.sendMessageToSocketForComlitedGame(game, user._id.toString(), user.credits, 0, false, false, reasonForFinish, user.ticTacToeHearts, ConfigParams.maxHearts, null, user.gamesWithoutAd >= ConfigParams.numberOfGamesForAd() - 1, timeLeftToNextHeart, canWatchVideoForHeart)
            }
          })
        }
      })
    }
  },


from Strange Nodejs memory leak

No comments:

Post a Comment