当我在 Arduino 上为 ESP8266 编译一个简单的 Blink 草图时,看起来 38% 的内存被某些东西使用了:
全局变量使用 31,576 字节 (38%) 的动态内存,为局部变量留下 50,344 字节。最大为 81,920 字节。
这些内存去哪儿了?我有一个需要大量内存的应用程序,想看看是否可以禁用/减少某些 Arduino 内置库的使用。
代码如下:
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
// Initialize the LED_BUILTIN pin as an output
}
void loop() {
digitalWrite(LED_BUILTIN, LOW);
// Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
delay(1000);
// Wait for a second
digitalWrite(LED_BUILTIN, HIGH);
// Turn the LED off by making the voltage HIGH
delay(2000);
// Wait for two seconds (to demonstrate the active low LED)
}
它由您初始化的变量和固件库使用。如果你想编写更长的代码,你将需要更多的内存。通过使用 ESP 的基本库,它已经占用了一些内存用于配置和固件设置。如果您在程序中使用较少的变量和简单的逻辑,这将大大减少您的程序大小。实际上,即使对于更大的程序,它也会占用更少的内存,因为更大的程序包含的所有库也是相同的。 但如果它真的很大,请专注于您的逻辑并减少 ESP 的压力,并将其交给大型计算机进行复杂的计算和逻辑(也有助于减少功耗和散热)。
使用空程序
void setup() {
}
void loop() {
}
当我使用 Wemos D1 mini Pro 进行编译时,我得到
. Variables and constants in RAM (global, static), used 28004 / 80192 bytes (34%)
║ SEGMENT BYTES DESCRIPTION
╠══ DATA 1496 initialized variables
╠══ RODATA 924 constants
╚══ BSS 25584 zeroed variables
. Instruction RAM (IRAM_ATTR, ICACHE_RAM_ATTR), used 59131 / 65536 bytes (90%)
║ SEGMENT BYTES DESCRIPTION
╠══ ICACHE 32768 reserved space for flash instruction cache
╚══ IRAM 26363 code in IRAM
. Code in flash (default, ICACHE_FLASH_ATTR), used 230996 / 1048576 bytes (22%)
║ SEGMENT BYTES DESCRIPTION
╚══ IROM 230996 code in flash
IRAM 已经达到 90%。这正常吗?