Home > database >  Memory issues how to lighten the code for node.js
Memory issues how to lighten the code for node.js

Time:01-24

Good evening, I thought I completed my node.js application, but yesterday after putting it into production, I notice several crashes every hour. It's the usual memory problem that everyone talks about it, I tried to increase the memory to 2gb with this code " node --max_old_space_size=2048 app.js" but without success, my server consists of 2gb ram and 1 cpu. Surely the problem lies in the setIntervals in my application, almost all files have at least one. I use the setInterval because I want in a certain time interval, my application downloads some bees and loads them in my mongodb. How can I lighten up my Interval cycle? show me some code

var stato= false;
var SetIntervallo= null;

module.exports= function attivaAggiornamentoCoinscap(){

function update_db_500 (){

 axios({
  "method":"GET",
  "url":env.api_coinraking_list5000,
  "headers": {
    "accept": "application/json",
    "x-access-token": env.api_KEY_coinraking

  } })
 .then(function (response){
 onSuccess(response)
 })
 .catch(function (err){
   console.log(err)
 });

//creation of model and scheme to be saved on the db

var Data = mongoose.connessionedbfruitfinance.model('Coincap ' , coinrakingSchema)
var Coinscap_Light = mongoose.connessionedbfruitfinance.model('Coincap_light ' , coinrakingSchema)

function onSuccess(response) {

  
 
 for(var i = 0; i < response.data.data.coins.length; i  ) {
    var name = response.data.data.coins[i].name;
    var symbol = response.data.data.coins[i].symbol;
    var img = response.data.data.coins[i].iconUrl;
    var price = response.data.data.coins[i].price;
    var rank = response.data.data.coins[i].rank;
    var change = response.data.data.coins[i].change;
    var Mcap = response.data.data.coins[i].marketCap;
    var listed = response.data.data.coins[i].listedAt;
    var idtoken = response.data.data.coins[i].uuid;
    var Volume_24 = response.data.data.coins[i]["24hVolume"];


    assignDataValue(name,symbol,img,price,rank,change,Mcap,listed,idtoken,Volume_24) 
  } 
}
  

  function  assignDataValue(name,symbol,img,price,rank,change,Mcap,listed,idtoken,Volume_24) {

    var upData = ({
    name : name,
    symbol : symbol,
    img: img,
    price : price,
    rank : rank,
    change : change,
    Mcap : Mcap,
    listed : listed,
    idtoken : idtoken,
    data : Date.now(),
    Circulating_supply: (Mcap / price),
    Volume_24: Volume_24
    })
   

    Data.updateMany({rank}, upData, function(errore,eseguito){if(errore){console.log('trovato errore '   errore)}})
    Coinscap_Light.updateMany({rank},{name:name,symbol:symbol, rank:rank,price:price,change:change,Mcap:Mcap,Volume_24: Volume_24},
      (err,res)=>{if(err){console.log('trovato errore'   err)}})
       

   }
  }

// database update every 60 seconds
  if(stato===false){SetIntervallo=setInterval(update_db_500, 60000), stato=true, console.log(`${stato} attivato aggiornamenti coinscap `)}
  else{clearInterval(SetIntervallo), SetIntervallo=null, stato= false, console.log(`${stato} disattivato aggiornamenti coinscap `)  }

}

CodePudding user response:

function clearData() {
    name = null;
    symbol = null;
    img = null;
    price = null;
    rank = null;
    change = null;
    Mcap = null;
    listed = null;
    idtoken = null;
    Volume_24 = null;
    upData = null;
}

You should call this function after the lines

Data.updateMany({rank}, upData, function(errore,eseguito){if(errore){console.log('trovato errore '   errore)}})
Coinscap_Light.updateMany({rank},{name:name,symbol:symbol, rank:rank,price:price,change:change,Mcap:Mcap,Volume_24: Volume_24},
  (err,res)=>{if(err){console.log('trovato errore'   err)}})

so the code will look like this:

Data.updateMany({rank}, upData, function(errore,eseguito){if(errore){console.log('trovato errore '   errore)}})
Coinscap_Light.updateMany({rank},{name:name,symbol:symbol, rank:rank,price:price,change:change,Mcap:Mcap,Volume_24: Volume_24},
  (err,res)=>{if(err){console.log('trovato errore'   err)}})
clearData()

This will help to free up the memory used by these variables and prevent the memory leak.

  • Related