0

I've been searching the WWW about how much can we store inside a Arduino String string; or inside String v[0];. But not even Arduino Reference page has the answer. Every search ends with somebody saying that you shouldn't use String (capital S) on an Arduino because it doesn't have enough memory to handle the big, bad String. I don't use Arduino Uno, so I don't mind if String uses more memory than std::string.

Do you guys have any idea what is the maximum length a String object can store ?

2
  • 4
    Possible duplicate of Are there limits on string length in Arduino? Commented Mar 25, 2019 at 16:23
  • Thank you for the minus, whoever it was, but this comment doesn't answer my question ! Nobody in that thread actually states how much can a String object store, because they are too concerted with the fact that the Arduino has little memory to spare and that String is way too big to use ! Also, I am not using an Arduino Uno, so I don't have any issues with the memory. You guys are way to quick with the minus ! It's like you're getting prizes if you're flagging a question "bad". But it's bad from your subjective point of view ! Commented Mar 25, 2019 at 19:06

1 Answer 1

1

The maximum length of a String depends on how much memory is free when you try to store something in it.

The software underlying the Arduino Core maintains an area of RAM called the "heap" which software can allocate from at runtime. String calls a function called realloc() (a standard C library function which helps manage the heap) when it needs to increase the storage it's using for its String.

https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/WString.cpp#L170

The maximum size String may be much smaller than the amount of free heap, though. Depending on how you're software has been allocating and freeing memory, the heap may have a lot of small chunks of memory available with allocated memory between them ("heap fragmentation"). So the biggest free piece may be much smaller than the total amount of free heap.

This is why people advise against using String. It allocates memory frequently, especially if you modify String objects, and can easily lead to the heap being fragmented so that you can't allocate a large piece of memory. This effect is worse on processors with small amounts of memory, like Arduinos. The ESP8266 is a bit better as it has more memory. The ESP32 is much better because it has much more memory.

This is made worse by the fact that String fails silently when it can't allocate memory. In that case your program will just malfunction with no warning.

That said there are plenty of cases where I think String is completely fine to use. On an ESP32 I would just avoid using it in programs that are meant to run indefinitely or in commercially shipping software.

The ESP32 Arduino Core has a few functions that can help if you're trying to understand how big a String you can have:

  • ESP.getHeapSize() returns the total size of the heap, including allocated and free memory. This will be greater than the potential maximum String you can make.
  • ESP.getFreeHeap() returns the total free space in the heap. This will also be greater than the potential maximum String you can make.
  • ESP.getMaxAllocHeap() returns the size of the largest fragment of the heap which you can allocate. This will approximate the largest String you can allocate. It may be much smaller than ESP.getFreeHeap().
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer ! I found String to be easier to use because I've just finished a Java Course. All my ESP32 projects use String. It will take some time and work to change them to std::string, but that way I will understand strings even better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.