Question about String

John09
Posts: 4
Joined: Mon Jan 15, 2024 9:17 pm

Question about String

Postby John09 » Mon Mar 11, 2024 11:06 pm

Hello, i am using arduino ide to program an esp8266 wemos d1 mini. I know using String is generaly not adviced due to memory leakage but i have a simple question about it regardless.

Let's say i have globably declared an array of strings:

Code: Select all

String payload2D[40];
Then in a loop i first init this array of Strings to "0":

Code: Select all

for(int i=0;i<40;i++) payload2D[i] = "0";
After that, my program will try to read text from external server and if it connects, it will fill that array with text, such as:

Code: Select all

payload2D[0] = "DHT22_BOTTOM_1_Temperature:      27.3C"
My question is whether this is allowed, meaning i first init the string array, then write to it only "0" and then writing a long text into it. Is the size of String's in the array being increased beforehand to accommodate bigger string i am writing into it, or am i writting to memory outside of that array ?

User avatar
Inq720
Posts: 37
Joined: Fri Dec 22, 2023 1:36 pm
Location: North Carolina, USA
Contact:

Re: Question about String

Postby Inq720 » Tue Mar 12, 2024 12:13 pm

I liked the way you worded it... "is it allowed?" :lol:

With programming in general and C/C++ specifically, they will allow you to do anything. Even shoot yourself in the foot. So yes... if it compiles... it is allowed.

Using String is a convenience piece and used correctly will work continuously. To my knowledge, it does not have memory leak issues. It does, however, cause fragmentation of memory. If you are not really tight on using the full 1MB of program address space and not doing other allocations (using malloc/new and delete/free) all over the place, you will also be fine. And even if you do, and it finally fails to allocate, the software/hardware watchdog will reboot it and clean everything up anew.

Now... if you really want to be a purist, hardcore developer or you absolutely want your program to run forever and never rely on the watchdog as a pacifier, I can understand that. In that case... you absolutely don't want to use String and certainly not the way you describe. I have not looked into the class for String on an Arduino/ESP8266 as I don't use it. But... in general... String class typically allocates memory on the heap. As you're using the String's features, it typically has to re-allocate memory as the string is added to. As there is no way ahead of time for the String to know what you are going to assign it later. This is where fragmentation occurs. Some implementations will allocate a larger block initially, but being a microcontroller needing to use memory sparingly, this probably does not happen on an ESP8266. Likely... when you set it to "0" it is doing the equivalent of a malloc(2) to hold the 0 and the NULL. And later when you pass it the long strings... it either deletes or reallocs for the larger space. Either way a 2 byte (fragmentation) can occur.

IF you know the maximum size of the incoming strings... you'd be far better off just using character arrays. Something like...

char payload2D[40][80];

This way fragmentation can't occur. It is also the fastest as the allocation only happens once. But if you are relying on other features of the String class for manipulation or the string can be vary greatly in length this might not be possible.

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Question about String

Postby ESP_Sprite » Wed Mar 13, 2024 2:23 am

To answer your question: the String object will manage its own memory (request and release it from the heap) and this is normally entirely transparent to you, the user. So you don't have to worry about the size of the array as the actual string data is not stored there.

John09
Posts: 4
Joined: Mon Jan 15, 2024 9:17 pm

Re: Question about String

Postby John09 » Wed Mar 13, 2024 10:26 pm

I am using String on all my systems though i would like to transition to safer option in time, but it will take quite a lot of code changes and testing. I have 2 systems running 24/7 and 1 of the systems will often have 7-15day runtime before restarting, while the other one often restarts after just a day. The only difference between the two i can think of is a lot more String data. It would make me feel better if both had uptime of 20-30days.

When something goes wrong with String, can i always count on system restarting ? Because that is not bad for me, apart from the fact that seeing high uptime is nice. But if restart got stuck and the board wouldnt properly restart, that would be very bad since its controling temperature and humidity.

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Question about String

Postby ESP_Sprite » Thu Mar 14, 2024 1:47 am

It'd be better to figure out why your system is crashing in the first place, honestly. String objects themselves aren't inherently bad, but they can make you run out of memory if you're not careful.

John09
Posts: 4
Joined: Mon Jan 15, 2024 9:17 pm

Re: Question about String

Postby John09 » Thu Mar 14, 2024 9:29 pm

Would a good start be maybe to print some memory/cache/stack statistics on the server along with my usual text data that i print ? And then check how it changes over time ?

It's a simple control system reading data from 6 sensors, controling a heater and a fan and running a server where it posts all relevant information in plain text. It stores all sensor and other data in global/local variables and at the end of each loop constructs all that data into a single String that is about 1650 letters long and post's it on it's server

ESP_Sprite
Posts: 9757
Joined: Thu Nov 26, 2015 4:08 am

Re: Question about String

Postby ESP_Sprite » Fri Mar 15, 2024 2:49 am

That would certainly help. It might be as simple as a memory leak (e.g. doing a 'String *bla=new String' somewhere without delete'ing it afterwards).

Who is online

Users browsing this forum: No registered users and 32 guests