JAndrassy / ArduinoOTA

Arduino library to upload sketch over network to Arduino board with WiFi or Ethernet libraries
GNU Lesser General Public License v2.1
435 stars 89 forks source link

client.get(buff) causes freeMemory to drop significantly, .stop() doesn't release #228

Closed JEG8 closed 9 months ago

JEG8 commented 9 months ago

Hi,

I am facing an issue with the library (in conjunction with and ) via your OTASketchDownloadWifi example, where I make a very simple call of client.get(buff), where buff is defined as the path for my server. My freeMemory (between heap and stack) drops by 1.5K when this call happens. I am assuming the memory is being claimed by something in the ArduinoHttpClient library. When I call client.stop(), the memory is not released. This makes it very difficult to use in a system where memory is critical.

Is there any reason why this would happen? Is there a recommended method to prevent this drop or does your library provide a way to fix this?

Thanks for any help in advance.

JAndrassy commented 9 months ago

is it not some glitch in the code for the free memory calculation?

JEG8 commented 9 months ago

No, it is directly from MemoryFree library. https://github.com/mpflaga/Arduino-MemoryFree/blob/master/MemoryFree.cpp

JAndrassy commented 9 months ago

I used that too so I know it is OK. Once I had some other and it didn't work good.

Is the buff string terminated by 0?

JEG8 commented 9 months ago

Yes, the buff string is being formulated using the code below. snprintf properly adds the null terminator to the string. I printed out the hex contents and did in fact see that the null terminator.

char buff[32];
  snprintf(buff, sizeof(buff), PATH);

Also, if I comment out the client.get() call, the memory usage does not drop. It doesn't seem like its in the formation of the object, but instead the .get() call itself.

JAndrassy commented 9 months ago

The WiFiNINA library allocates 1500 bytes for buffers at once, but only once.

https://github.com/arduino-libraries/WiFiNINA/blob/12c33f10cc3eddad245a2263fe0476fee60320a2/src/utility/WiFiSocketBuffer.cpp#L60

JEG8 commented 9 months ago

Great find! Thank you, this explains what I am seeing.

Do you know of any way I can save memory with this implementation?

JAndrassy commented 9 months ago

you can change the WIFI_SOCKET_BUFFER_SIZE at the beginning of WiFiSocketBuffer.cpp

JEG8 commented 9 months ago

Thank you for your help! I will mark this issue as closed.