You are reading a single comment by @Robin and its replies. Click here to read the full conversation.
  • I'm pretty new to Javascript so I'm hoping this is something that is easy to fix in my code. I've tried setting up my ESP32 to connect to WIFI and MQTT and no matter how I write it, I am getting memory leaks. Luckily, I do not need Bluetooth and I saw on here how to disable that (which practically doubled my available memory).

    Below is my current version that will connect to my WIFI and publish MQTT. It will reconnect on a lost WIFI signal or lost MQTT connection. However, every 30 mins or so my free memory drops by 50 or so. I've tried writing this various different ways such as using setInterval vs setTimeout functions. I don't think I am mixing up the scope of any of my global variables. I even once tried putting everything all in one function and periodically resetting it to null which just seemed to make the problem worse. Is there a way to do this that will not run through all the memory?

    //WIFI config
    var wifi_options = 
         ssid: 'xxxxxx',
         password: 'xxxxxx',
    //MQTT config
    var mqtt_freq = 1000; //how often to send MQTT(ms)
    var mqtt_options = 
    //    keep_alive: 0,
        server: "",
        port: 1883,
    var led_speed = 500;
    var wifi_stat;
    var wifi_recon_count=0;
    var wifi_pre_mem;
    var wifi_post_mem;
    var Wifi = require('Wifi');
    //get wifi status every second
    function iw1() {
      wifi_stat = Wifi.getDetails().status;
      //set led blink rate
      if(wifi_stat !== "connected") {led_speed = 100;}
      else {led_speed=500;}
    //reconnect to WIFI (only try once every 30 seconds)  
    function iw2(){
      if(wifi_stat !== "connected") {
        wifi_pre_mem = process.memory().free;
        print("WIFI disconnected: Reconecting...");
        Wifi.connect(wifi_options.ssid, {password: wifi_options.password});
        wifi_post_mem = process.memory().free;
    var  D2_setting = false;
    function iLED () {
      D2_setting = !D2_setting;
      digitalWrite(D2, D2_setting);
      setTimeout(iLED, led_speed);
    //mqtt globals
    var mqtt_r=require("MQTT");
    mqtt = mqtt_r.create(mqtt_options.server, {
      port: mqtt_options.port,//8883
    var mqtt_conn_count = 0;
    var mqtt_stat = false; 
    function mqt0(){
      function mqt1 () {
        mqtt_stat = mqtt.connected;
      var imq1 = setInterval(mqt1,1000);
      //Reconnect MQTT if disconnected:
      function mqt2() {
        if (mqtt_stat === false){
          print("MQTT disconnected, reconnecting...");
      var imq2 = setInterval(mqt2,30*1000); //only try to reconnect every xx seconds
      //publish free memory every minute
      function mqt3(){
        if (mqtt_stat === true){
          free_mem = process.memory().free; 
          print("memory publishing: " + free_mem);
       var imq3 = setInterval(mqt3,1000*60);
      //loop back to mqt0 and clear intervals
        imq1 = null;
        imq2 = null;
        imq3 = null;
      },60*1000*15); //setloop time to 15 mins
  • Wed 2019.10.09

    Hello @dwinfiel

    Would you mind posting the results of process.env and separatelyprocess.memory() before uploading, then again after the memory leak is observed, so that we may get a better understanding of the scope of this issue. Thanks


Avatar for Robin @Robin started