Javascript and Browser useage

I have created the following JavaScript for downloading crypto-currency charts. I have seen that after some time the browser RAM usage goes up from this JavaScript run. And it can go up till it reach 100% and browser stops to work and I am asking now myself if with every JavaScript code the browser RAM usage goes up after some hours or if I can optimize my code to prevent it from going up?

In my test I did see for example after 5 hours the RAM goes up from 8% to 23% and this grow can continue then till maximum is reached.

var Programstart = 0;
var Zaehler = 0;
var ZaehlerCoinsIndex = 0;
var AnzahlArrayIndexe = 0;


//--check json syntax
function isJson(item) {
  item = typeof item !== "string"
    ? JSON.stringify(item)
    : item;

  try {
    item = JSON.parse(item);
  } catch (e) {
    return false;
  }

  if (typeof item === "object" && item !== null) {
    return true;
  }

  return false;
}


//--Objecte nur bei start erstellen, spart vielleicht Resourcen
if (Programstart == 0) {
  var xhttp3 = new XMLHttpRequest();
  var xhttpvier = new XMLHttpRequest();
  var Downloadliste_Coins_Object = new Object();
  console.log("Program started");
}
Programstart = 1;


//-- Server Funktion, ladet sich immer von neu          
function ServerFunktion() {
  DownloadCoins();
  document.getElementById("test").innerHTML = "Zaehler " + Zaehler;

  //---- 1000 = 1 sekunde
  setTimeout(ServerFunktion, 2000);
}


//----
function DownloadCoins() {
  //--Coins in Array Object
  if (Zaehler == 0) {
    //-- Coinnamen into Array
    var Downloadliste_Textinhalt = $.ajax({
      url: "Downloadliste_CoinsToken.txt",
      contentType: "application/json",
      dataType: "json",
      async: false
    }).responseText;
    if (isJson(Downloadliste_Textinhalt) == true) {
      Downloadliste_Coins_Object = JSON.parse(Downloadliste_Textinhalt);
    }//json Check
    else alert("Download Liste json synatx error");

  }
  //--Bitcoins Charts
  else if (Zaehler == 1) {
    //--Bitcoin
    //console.log($.ajax({ url: strurl, contentType:"application/json", dataType:"json", async: false }).responseText);
    var strurl = "Save_Chartdata_Poloniex.php?symbol=USDT_BTC&name=Bitcoin";
    $.ajax({url: strurl, contentType: "application/json", dataType: "json", async: false});
  }
  else if (Zaehler == 2) {
    //--Bitcoin
    var strurl = "Save_Chartdata_Binance.php?symbol=BTCUSDT&name=Bitcoin";
    $.ajax({url: strurl, contentType: "application/json", dataType: "json", async: false});
  }
  //-- USD Charts
  else if (Zaehler > 2) {
    var Anzahl = Downloadliste_Coins_Object.Coins.length;
    var str = Object.keys(Downloadliste_Coins_Object);
    if (str.indexOf("Coins") >= 0) {
      //console.log("Anzahl "+Anzahl+"  "+Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].broker+"  "+Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].name+"  "+Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].symbol);
      if (Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].broker == "Poloniex") {
        var strurl = "Save_Chartdata_Poloniex.php?symbol=BTC_" + Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].symbol + "&name=" + Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].name;
        var res = $.ajax({url: strurl, contentType: "application/json", dataType: "json", async: false}).responseText;
        document.getElementById("test2").innerHTML = res;
      }
      else if (Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].broker == "Binance") {
        var strurl = "Save_Chartdata_Binance.php?symbol=" + Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].symbol + "BTC&name=" + Downloadliste_Coins_Object.Coins[ZaehlerCoinsIndex].name;
        var res = $.ajax({url: strurl, contentType: "application/json", dataType: "json", async: false}).responseText;
        document.getElementById("test2").innerHTML = res;
      }

      //--Zaehler Array Index
      ZaehlerCoinsIndex++;

      //--Zaehler zurueck setzen
      if (ZaehlerCoinsIndex >= Anzahl) {
        Zaehler = 0;
     

   ZaehlerCoinsIndex = 0;
          }
        }
      }
      Zaehler++;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<html>
<body onload="ServerFunktion()">

<span id="test"></span>
<span id="test1"></span>
</body>
</html>

The following article helps to explain common techniques for tracking down memory leaks.

2 Likes

If you look to my code can you find thinks where you know that can be the reason?
I am declaring for example some variables as global variables, is that better to declare thel that way or do you think its better to declare them inside the function?

I have declare them outside the functions, because i think if i declare them inside the functions they will be generated always again and create that way more RAM use or ?

I have also a Object in my code which is outside the function where it is used, do you think is better to declare that object inside the function or its good like i did it now outside the function?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.