Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No memory leak in ESP8266WiFi... #7528

Closed
peychart opened this issue Aug 15, 2020 · 3 comments
Closed

No memory leak in ESP8266WiFi... #7528

peychart opened this issue Aug 15, 2020 · 3 comments

Comments

@peychart
Copy link

peychart commented Aug 15, 2020

#7384 ## Platform

  • Hardware: ESP8266
  • Core Version: current version
  • Development Env: Platformio
  • Operating System: Ubuntu

Settings in IDE

  • Module: Wemos D1 mini r2
  • Flash Mode: [qio|dio|other]
  • Flash Size: 4MB
  • lwip Variant: [v1.4|v2 Lower Memory|Higher Bandwidth]
  • Reset Method: [ck|nodemcu]
  • Flash Frequency: 40Mh]
  • CPU Frequency: 80Mhz
  • Upload Using: OTA & SERIAL
  • Upload Speed: 115200 (serial upload only)

Problem Description

Mermory leak on WiFi use, particularly quickly in case of DNS problems, until the device reboot...

Hi friends and thank you for your work.

After several hours in google searching, I understood that there is no memory leak in the ESP8266WiFi library.
However, the fact remains that the device ends up rebooting, particularly quickly (every mn) when DNS (bind9) problems occur (on cut of internet link for example).

My use : https://github.com/peychart/WiFiPowerStrip/blob/master/Next-version/WiFiManager.cpp, line 109

MCVE Sketch

My project : https://github.com/peychart/WiFiPowerStrip/tree/master/Next-version

So, yes this project isn't very small (between 2k & 18k of free memory)... But, the punishment is the same with the lightweight C version of the prototype (https://github.com/peychart/WiFiPowerStrip) in operation for many months (stable most of the time, except on DNS failure - flashing house lighting)... :-(

Code:
#include <Arduino.h>
#include <ESP8266HTTPUpdateServer.h>
#include <uart.h>

#include "untyped.h" //<-- de/serialization object for JSON communications and backups on SD card
#include "WiFiManager.h" //<-- WiFi connection manager
#include "pins.h" //<-- pins object manager with serial pin extension manager
#include "switches.h" //<-- inputs management
#include "webPage.h" //<-- definition of a web interface
#ifdef DEFAULT_MQTT_BROKER
#include "mqtt.h" //<-- mqtt input/output manager
#endif
#ifdef DEFAULT_NTPSOURCE
#include "ntp.h" //<-- time-setting manager
#endif

#include "setting.h" //<--Can be adjusted according to the project...
#include "debug.h" //<-- telnet and serial debug traces

WiFiManager myWiFi;
...
void setup() {
...
myWiFi.version ( VERSION )
.onConnect ( onWiFiConnect )
.onStaConnect ( onStaConnect )
.ifStaConnected ( ifStaConnected )
.ifConnected ( ifWiFiConnected )
.onStaDisconnect ( onStaDisconnect )
.hostname ( DEFAULTHOSTNAME )
.setOutputPower ( 17.5 )
.restoreFromSD ();
...
}

void loop() {
ESPWebServer.handleClient(); delay(1L); //WEB server
myWiFi.loop(); //WiFi manager
mySwitches.inputEvent(intr, rebound_completed); //Switches management
myPins.timers(); //Timers control for outputs
myPins.serialEvent(); //Serial communication for the serial slave management
#ifdef DEFAULT_MQTT_BROKER
myMqtt.loop(); //MQTT manager
#endif
}

Debug Messages

no msg, device reboot on lack of memory...
FreeMem: 19000
FreeMem: 18808
FreeMem: 18768
FreeMem: 17800
FreeMem: 17736 //<- on every ESPWebServer.send call...
etc...

Workaround (but, not very elegant) :

#ifdef WIFI_MEMORY_LEAKS
struct tcp_pcb;
extern struct tcp_pcb* tcp_tw_pcbs;
extern "C" void tcp_abort (struct tcp_pcb* pcb);
inline void tcpCleanup(){while (tcp_tw_pcbs != NULL) tcp_abort(tcp_tw_pcbs);}
#endif

void ifWiFiConnected() { // run every 30s
#ifdef WIFI_MEMORY_LEAKS
ulong m=ESP.getFreeHeap();
DEBUG_print( F("FreeMem: ") ); DEBUG_print(m); DEBUG_print( F("\n") );
if( m < WIFI_MEMORY_LEAKS ){
ESPWebServer.stop(); ESPWebServer.close(); myWiFi.disconnect(1); //<- auto reconnect...
tcpCleanup();
ESPWebServer.begin();
DEBUG_print(F("TCP cleanup...\n"));
}
#endif
...

@peychart
Copy link
Author

peychart commented Aug 16, 2020

Thank you for your courteous help...;-)

@earlephilhower
Copy link
Collaborator

Fair enough @peychart , I probably should have explained the reason for closing.

This was closed because you've posted a sample that doesn't compile, using an external library and headers, with a very vague OOM error report that's not reproducible outside of your own setup. So, there's really nothing we can do here. Your best bet is https://www.esp8266.com or the Gitter chat at https://gitter.im/esp8266/Arduino .

If you can pull together a complete, failing MCVE we can give it a go, but w/o one (as said in the issue template) it's not going to be possible.

@peychart
Copy link
Author

peychart commented Aug 16, 2020

809/5000
Yes, I understand ... but if I could make a simple, compilable example, which reproduces this error in ten lines, of course I would ... ;-)
The problem is that this malfunction only appears on important sources, which do not leave enough memory to allow this drift.
I worked dozens of hours on this problem, checking my own code hundreds of times ... Each time, I come back to the same Wifi/Ethernet problem, especially as I said, when the DNS is no longer network access.
the web server only sends const __FlashStringHelper * , I comment the analysis of the responses ... so no memory leaks possible on that ... when I stop the communications (close the client), no more drift (and the amount of free memory goes up a little)...
Here is the state of my report ... :-(
I am running out of solution (although the workaround will do the trick and keep the memory pool free within 18k ... but at the cost of periodic disconnections / reconnections of the Wifi).
I think that this circumvention used, which stabilizes the situation, is nevertheless in itself very significant of the problem.
Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants