
Description
Today I noticed 2.4.1 was released for my Arduino IDE so I gave it a try. Unfortunately my app crashed after a few minutes. Memory pressure is always a good first guess so I added calls to ESP.getFreeHeap() and sure enough, the heap was smaller after each wifi client reconnection. It started at about 32000 bytes and dropped by a few kB each time. When the heap reached a few hundred bytes it paniced.
This was using lwip 2 for higher bandwidth. lwip 2 for lower memory acts the same way. I tried using lwip 1.4 forbandwidth but can't tell about the leak because it just flat fails to connect after a few connections.
My app is about 850 kB program and 45 kB RAM and not easy to post here. Basically it opens several web connections in a round robbin fashion, calling ::stop after finished with each. I noticed in the 2.4.1 release notes that ::stop no longer destroys ClientContext so I wonder if this might be related.
2.4.0 with lwip 1.4 precompiled is still the golden combo for me, it runs beautifully forever.
Thanks.
Activity
bobybobybob commentedon Mar 11, 2018
Yes that code https://blog.thesen.eu/wp-content/uploads/2017/05/ESP_TH_Logger_V2.zip - A Temperature Logger for ESP8266 ( https://blog.thesen.eu/wlan-lufttemperatur-und-feuchte-logger-mit-grafischer-darstellung-fuer-esp8266/ ) works perfect with 2.3.0 but 2.4.0 do not work... every time load the site it eats free RAM until RAM gets out and no working any more.
With 2.4.1 it is worser and the pages are not every time reachable and eating RAM every page refresh into dead faster.
but when i enter
delay(1000);
before client.stop(); the memory leak is some time away but trouble too.
2.3.0 work best
ghost commentedon Mar 11, 2018
Sorry, I meant to say 2.3.0 is the best, not 2.4.0.
reaper7 commentedon Mar 12, 2018
I have similar observations in my code with two clients, first client for ThingSpeak lib and second for pubsubclient.
Every minute I upload data to ts and to local mqtt server and this operation eats 112bytes (56bytes per client).
Does not matter LWIP 2 or 1.4, I think that some changes in wifi client in the second half of february...because before nothing bad was happening.
so, I move thingspeak client declaration from global to local (available only inside upload procedure and, I hope, is destroyed on end) and replace pubsubclient with esp-mqtt-arduino (by i-n-g-o),
these two steps solved the disappearance of memory.
But clearly something has changed...maybe client resource is not released?
Someone from specialists can say something more (56bytes per client, this is the trail)
half hour chart (with fixed thingspeak but still on pubsubclient as explained above) 1minute = -56bytes from heap:

mikekgr commentedon Mar 12, 2018
No, @reaper7 in my case this does not improve anything... unfortunately.
reaper7 commentedon Mar 12, 2018
please check this simple sketch for show difference between global and local client declaration and memory leak when global...
simply comment or uncomment #define GLOBALCLIENT (and change ssid/pass and server ip!)
if client is declared inside loop, then memory is reduced every loop by the first 17 connections
but after this stabilizes at 42424bytes and it does not decrease
but if client is declared globally at top of sketch, then memory is reduced much more every loop by the first 17 connections and from connection number 18 never stop and consumes 56bytes per loop
mikekgr commentedon Mar 12, 2018
@reaper7 Dear friend, I followed your suggestion to put inside the related function the "WiFiClient client;" so it is not global and the result is way better. In this case I loose 4*168 bytes + 424 bytes but then the leakage stopped... My project still under testing but your suggestion and analysis drove me to the right direction. Thanks a lot.
P.S. my project update values at ThingSpeak among other...
devyte commentedon Mar 12, 2018
Note: this sounds like something not being freed in WiFiClient::operator=()
igrr commentedon Mar 13, 2018
Will make a PR later today.
mikekgr commentedon Mar 14, 2018
Dear @igrr
when we will expect this PR? Please give us a time frame if possible.
Many thanks for your nice work.
s0170071 commentedon Mar 14, 2018
hm... 56 bytes. This may be related to #4487 and #4488 then as I was also losing 56 bytes at a time.
WiFiClient: clean up ClientContext before connecting
igrr commentedon Mar 14, 2018
Created #4516 with a fix.
Workaround for 2.4.1:
client = WiFiClient(); // add this line for 2.4.1 client.connect(host, port);
43 remaining items
devyte commentedon Jul 9, 2018
@dccharacter I invite to look at the labels for this issue, where you will notice that this issue is already staged for release, which means it is already fixed. I invite you to then look at the milestones, where you will notice that this issue is targeted for release 2.4.2. If you investigate the milestones for 10 seconds, you can find out that release 2.4.2 is targeted for 1/Aug, which means it hasn't happened yet.
dccharacter commentedon Jul 9, 2018
Thanks for explanation! I just read through the thread and that wasn't clear.
Gosh, it pains me that I wasted a few months for swapping modules and troubleshooting my code, than gave up and just by accident discovered today that I might have a memory leak. Thanks to google found this thread. If not for this luck, would probably totally abandon the project :-(
Commenting out workarounds for Arduino bug...
Commenting out workarounds for Arduino bug...