ayushsharma82 / WebSerial

A remote terminal library for wireless microcontrollers to log, monitor or debug your firmware/product
https://webserial.pro
GNU Affero General Public License v3.0
461 stars 107 forks source link

ESP32 Memory Leak #53

Closed darrenlock closed 2 weeks ago

darrenlock commented 1 year ago

I have noticed that each call to WebSerial.println leaks about 20 of Heap. Once the browser connects it then leaks about 40. When the browser disconnects about 900 is lost. See test programme which builds on the example:

`/* WebSerial Demo

This example code works for both ESP8266 & ESP32 Microcontrollers WebSerial is accessible at your ESP's /webserial URL.

Author: Ayush Sharma Checkout WebSerial Pro: https://webserial.pro */

include

if defined(ESP8266)

include

include

elif defined(ESP32)

include

include

endif

include

include // https://github.com/alanswx/ESPAsyncWiFiManager

include

AsyncWebServer server(80);

const char ssid = ""; // Your WiFi SSID const char password = ""; // Your WiFi Password

/ Message callback of WebSerial / void recvMsg(uint8_t* data, size_t len) { WebSerial.println("Received Data..."); Serial.printf("Received Data... [%d] ", ESP.getFreeHeap()); String d = ""; for (int i = 0; i < len; i++) { d += char(data[i]); } WebSerial.println(d); Serial.printf(" %s [%d]\n", d.c_str(), ESP.getFreeHeap()); }

void setup() { Serial.begin(115200); Serial.println("Starting Wifi..."); DNSServer dns; AsyncWiFiManager wifiManager(&server, &dns); if (!wifiManager.autoConnect()) { Serial.println("wifiManager.autoConnect() failed to connect - restarting ESP"); delay(3000); ESP.restart(); delay(5000); } Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // WebSerial is accessible at "/webserial" in browser WebSerial.begin(&server); / Attach Message Callback / WebSerial.msgCallback(recvMsg); server.begin(); Serial.printf("Setup complete [%d]\n", ESP.getFreeHeap()); }

void loop() {

static uint32_t    _AtStart = ESP.getFreeHeap();
static unsigned int _lastSend;
static uint32_t    _After = 0;
static uint32_t    _Before = 0;    
static uint32_t    _SinceStart = 0;

if (millis() - _lastSend > 5000)
{
    _Before = ESP.getFreeHeap();
    Serial.printf("Last WebSerial Call [%d] Lost Since [%d]\n", _After, _After - _Before);
    Serial.printf("Before WebS [%d] ", _Before);
    WebSerial.println(ESP.getFreeHeap());
    _After = ESP.getFreeHeap();
    Serial.printf("After WebS [%d] Lost [%d] \n", _After, _Before - _After);
    Serial.printf("At Start [%d] Lost Since Start [%d] Lost this iteration [%d]\n", _AtStart, _AtStart - _After, (_AtStart - _After) - _SinceStart);
    _SinceStart = _AtStart - _After;
    _lastSend = millis();
}

}`

darrenlock commented 1 year ago

Here is a sample of the output: Setup complete [250792] Last WebSerial Call [0] Lost Since [-251012] Before WebS [251012] After WebS [250992] Lost [20] At Start [251012] Lost Since Start [20] Lost this iteration [20] Last WebSerial Call [250992] Lost Since [0] Before WebS [250992] After WebS [250960] Lost [32] At Start [251012] Lost Since Start [52] Lost this iteration [32] Last WebSerial Call [250960] Lost Since [0] Before WebS [250960] After WebS [250940] Lost [20] At Start [251012] Lost Since Start [72] Lost this iteration [20] Last WebSerial Call [250940] Lost Since [0] Before WebS [250940] After WebS [250920] Lost [20] At Start [251012] Lost Since Start [92] Lost this iteration [20] Last WebSerial Call [250920] Lost Since [-132] Before WebS [251052] After WebS [251032] Lost [20] At Start [251012] Lost Since Start [-20] Lost this iteration [-112] Last WebSerial Call [251032] Lost Since [0] Before WebS [251032] After WebS [251012] Lost [20] At Start [251012] Lost Since Start [0] Lost this iteration [20] Last WebSerial Call [251012] Lost Since [132] Before WebS [250880] After WebS [250860] Lost [20] At Start [251012] Lost Since Start [152] Lost this iteration [152] Last WebSerial Call [250860] Lost Since [1180] Before WebS [249680] After WebS [247608] Lost [2072] At Start [251012] Lost Since Start [3404] Lost this iteration [3252] Last WebSerial Call [247608] Lost Since [-1976] Before WebS [249584] After WebS [247560] Lost [2024] At Start [251012] Lost Since Start [3452] Lost this iteration [48] Last WebSerial Call [247560] Lost Since [-1964] Before WebS [249524] After WebS [247516] Lost [2008] At Start [251012] Lost Since Start [3496] Lost this iteration [44] Last WebSerial Call [247516] Lost Since [-980] Before WebS [248496] After WebS [248520] Lost [-24] At Start [251012] Lost Since Start [2492] Lost this iteration [-1004] Last WebSerial Call [248520] Lost Since [-1152] Before WebS [249672] After WebS [249652] Lost [20] At Start [251012] Lost Since Start [1360] Lost this iteration [-1132] Last WebSerial Call [249652] Lost Since [0] Before WebS [249652] After WebS [249632] Lost [20] At Start [251012] Lost Since Start [1380] Lost this iteration [20] Last WebSerial Call [249632] Lost Since [-132] Before WebS [249764] After WebS [249744] Lost [20] At Start [251012] Lost Since Start [1268] Lost this iteration [-112] Last WebSerial Call [249744] Lost Since [-440] Before WebS [250184] After WebS [250164] Lost [20] At Start [251012] Lost Since Start [848] Lost this iteration [-420]

ayushsharma82 commented 1 year ago

Hi @darrenlock ,

Were you able to narrow it down to what's causing this memory leak?

Looking forward to fixing it.

darrenlock commented 1 year ago

Hi @ayushsharma82 ,

unfortunately I have not had chance to investigate any further. Maybe one of my projects over the holiday :)

jksemple commented 1 year ago

I didn't see this behaviour when testing my pull request. Not sure if my changes have eliminated a memory leak but if so, I don't know why.

github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been stalled for 5 days with no activity.