Is there a way to dispose an Arduino String-Object, so the memory it uses gets freed up, when the String is not needed anymore?
I didn't find a function in the reference for this, and when I call free(&str), an Exception is thrown.
Thanks in advance for your help. Simon
Edit: I code with the Arduino-IDE, like this
void setup() {
// a few setup things
}
void loop() {
String str1 = "some json String";
String str2 = str1.substring(10);
String str3 = jsonRemoveWhiteSpace(str2);
// so at this point I'd like to dispose of str1 and str2 to free up the memory,
// because the strings are also quite long json strings.
}
You can use curly braces to make explicit scopes. The C++ standard requires that objects declared in a scope are destroyed when they go out of scope. For objects that manage dynamic memory (such as
String
) that means the dynamic memory will be released to the system.If it doesn't affect the variable naming too much, you can consider assigning the result of
substring
andjsonRemoveWhiteSpace
to theString
you're operating on. The assignment operator forString
is well behaved and releases memory as expected.or