简体   繁体   中英

How many bytes can you store inside a Arduino String?

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 ?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM