简体   繁体   English

你可以在Arduino字符串中存储多少字节?

[英]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; 我一直在搜索WWW,我们可以在Arduino String string;存储多少内容String string; or inside String v[0]; 或者在String v[0]; . But not even Arduino Reference page has the answer. 但即使是Arduino Reference页面也没有答案。 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. 每个搜索都以某人说你不应该在Arduino上使用String(大写字母S),因为它没有足够的内存来处理大而坏的字符串。 I don't use Arduino Uno, so I don't mind if String uses more memory than std::string. 我不使用Arduino Uno,所以我不介意String使用的内存比std :: string多。

Do you guys have any idea what is the maximum length a String object can store ? 你们有什么想法String对象可以存储的最大长度是多少?

The maximum length of a String depends on how much memory is free when you try to store something in it. String的最大长度取决于您尝试在其中存储内容时可用的内存量。

The software underlying the Arduino Core maintains an area of RAM called the "heap" which software can allocate from at runtime. Arduino Core底层的软件维护着一个称为“堆”的RAM区域,软件可以在运行时分配该区域。 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. 当需要增加其用于String的存储时, String调用一个名为realloc()的函数(一个有助于管理堆的标准C库函数)。

https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/WString.cpp#L170 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. 但是, String的最大大小可能远小于可用堆的数量。 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 . 这就是为什么人们建议不要使用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. 它经常分配内存,特别是如果您修改String对象,并且很容易导致堆碎片化,因此您无法分配大块内存。 This effect is worse on processors with small amounts of memory, like Arduinos. 对于具有少量内存的处理器(如Arduinos),此效果更差。 The ESP8266 is a bit better as it has more memory. ESP8266有点好,因为它有更多的内存。 The ESP32 is much better because it has much more memory. ESP32更好,因为它有更多的内存。

This is made worse by the fact that String fails silently when it can't allocate memory. 由于String无法在无法分配内存时无声地失败,因此情况更糟。 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. 这就是说有很多情况我认为String完全可以使用。 On an ESP32 I would just avoid using it in programs that are meant to run indefinitely or in commercially shipping software. 在ESP32上,我会避免在无限期运行的程序或商业运输软件中使用它。

The ESP32 Arduino Core has a few functions that can help if you're trying to understand how big a String you can have: ESP32 Arduino Core有一些功能可以帮助你,如果你试图了解你可以有多大的String

  • ESP.getHeapSize() returns the total size of the heap, including allocated and free memory. ESP.getHeapSize()返回堆的总大小,包括已分配和可用内存。 This will be greater than the potential maximum String you can make. 这将大于您可以制作的潜在最大String
  • ESP.getFreeHeap() returns the total free space in the heap. ESP.getFreeHeap()返回堆中的总可用空间。 This will also be greater than the potential maximum String you can make. 这也将大于您可以制作的潜在最大String
  • ESP.getMaxAllocHeap() returns the size of the largest fragment of the heap which you can allocate. ESP.getMaxAllocHeap()返回可以分配的堆的最大片段的大小。 This will approximate the largest String you can allocate. 这将近似您可以分配的最大String It may be much smaller than ESP.getFreeHeap() . 它可能比ESP.getFreeHeap()小得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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