ESP32 memory leaks WIFI and MQTT #7159
Replies: 1 comment
-
Posted at 2019-10-10 by Robin Wed 2019.10.09 Hello @dwinfiel Would you mind posting the results of Posted at 2019-10-10 by AkosLukacs Hi! Just connecting to the AP, and then saving Wifi credentials with Wifi.save() was the best for me. Also, in the function
The status can be for example Posted at 2019-10-11 by maze1980 I guess there is a better way: Do not create new timer every 1000ms, but only once. Just to quote one function:
And how to do it better:
While there is a garbage collection, it might be called too late, or not working properly - I didn't look into it. But if you call setTimeout every 1000ms instead of setInterval once there's a really high chance it'll allocate a new block of memory for a new timer every 1000ms. Posted at 2019-10-14 by dwinfiel Thank everyone for their suggestions. I have just gotten back to trying this again (had some OT at work and other things going on). I am starting with a fresh install of everything (even my OS because when I tried to update Debian from stretch to buster, I broke it). The ESP32 board that I'm using are MELIFE 2 ESP-32S (ESP-WROOM-32 module with 4MB flash, no pSRAM). I've done a fresh flash of the firmware and I get the following right after flashing:
I then disabled bluetooth and get much more memory:
Then I turned on WIFI using
My WIFI does not re-connect on it's own (if I say reboot my WIFI router). However, it does connect when I reboot the ESP32. I'll try some of the other suggestions and see what I get. Posted at 2019-10-15 by dwinfiel I've modified the code to not try to connect if wifi status is "connecting". I've also modified it so that it won't try to reconnect MQTT if WIFI is disconnected. While connected with USB, I've tried starting and stopping my WIFI and/or my MQTT server so that I can see the printout from the ESP32. I'm noticing a few errors I'm hoping someone can clarify what the following errors are:
I get the following after reseting:
Also, I've noticed the device go into a state where the Wifi status is "NO_AP_FOUND" and it wouldn't reconnect. Once I disconnected power and re-connected then the WIFI was able to reconnect. Posted at 2019-10-15 by Robin Mon 2019.10.14 Thank you for the above postings @dwinfiel Are we still dealing with a memory leak? If so, would you please load your code and re-run Errors indicated above #6 are unknown to me. > 'does not re-connect on it's own (if I say reboot my WIFI router)' What logic is being used to reboot or dis-connect/re-connect when just the router is rebooted? Has a sufficient time interval been used to allow internal WiFI workings to settle? Posted at 2019-10-16 by dwinfiel After flashing new firmware to the ESP32, disabling Bluetooth, saving my wifi settings using the following:
I get the following memory (after sending a ESP32.reboot):
I found that my board does run the event wifi.on('disconnected',function()), at least sometimes. So I am trying the following code:
After performing a ESP32.reboot, I get the following:
And as long as my Wifi doesn't disconnect it seems to stay steady at 3981. Shortly after I shut off my WIFI, I get a few error messages and then my memory drops to 3963. It continues to drop each time it tries to reconnect. After a couple of minutes, I turned my WIFI back on. Just before I turned it on the free memory was down to 3895. After it finished reconnecting it dropped more, down to: Then it stays steady ad 3859. If I wait a few more minutes and turn off my WIFI again it doesn't seem to catch the change in WIFI status. If I manually type in wifi.getStatus(), I get the following
For some reason my ssid is now deleted. Any idea why it does that? Maybe I'm shutting my WIFI off too quickly. If I turn my WIFI back on, it doesn't re-connect a second time. I'll set it to run overnight and see how much, if any memory loss I get if I don't manually shut off the WIFI. Posted at 2019-10-17 by dwinfiel I tried resetting my router and then waiting about an hour before resetting a 2nd time and the ESP32 successfully reconnected, but memory dropped to 3799 after reconnecting. I just restarted my router a 3rd time and the ESP32 memory dropped to 3781 and then 3746 and then 3711 all while disconnected. After reconnecting it is down to 3709. Posted at 2019-10-17 by Robin
Are we still at ver 2.04 ? On a S.W.AG. hunch, try console.log() the value of 'iwico' to see if the handle is changing. Wondering if the call to setInterval() is gobbling up those bytes? Posted at 2019-10-17 by dwinfiel Yes my version is 2.04. Would trying an older vesion help? Not sure what you want me to console.log(), if I just put that I get "=undefined". When I do >E.getSizeOf(global, 2), I get the following:
There were not any memory loss during the night (WIFI probably didn't loose connection). Resetting it this morning dropped it from 3696 (what it dropped to after running a few commands such as the E.getSizeOf(global, 2)) to 3605 by the time it reconnected. Posted at 2019-10-18 by Robin Thr 2019.10.17
Examples at:
Thank you @dwinfiel for the postings. This will assist others should we not locate the offending binary culprit. While I'm primarily a supporter of official supported boards, I don't have access to an ESP32 to test your code. However, we still have a bit more to do to gather sufficient information to both rule out what isn't the cause and hopefully locate what is going on. Based on your postings, my first impression is that although you have diagnosed down to the connect/disconnect event and have observed memory decrease, I don't believe this is a result of the internal workings of Espruino. Possibility 1 > ref L8 in listing 3 of post #8 It might be that It is indicated in post #1 that Javascript is new to you, so forgive me if this is too basic;
Possibility 2 L28 is getting called, while a timeout is still being processed, also not freeing up memory. Use the same technique above to monitor the handle returned from that function. Also note that to clear a running one needs Possibility 3 Lines like #7 One way around that would be to define the string once outside the function, then pass the variable name to the console.log() function, rather than a new string each invocation. Possibility 4 > 'Would trying an older version help?' It would be a good comparison test, but we are not there yet. Too time consuming to flash, with most likely no additional troubleshooting detail. The above should provide sufficient detail to narrow this down. |
Beta Was this translation helpful? Give feedback.
-
Posted at 2019-10-09 by dwinfiel
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?
Beta Was this translation helpful? Give feedback.
All reactions