Arduino IDE says 306 KB for local variables. Your loop gets 8.

After every ESP32 build, Arduino IDE prints two lines about memory. Both are misleading. The “maximum” for globals is a constant from a text file, the space “for local variables” is not where loop() keeps its locals, and the sketch maximum is a partition setting, not the size of your flash.

In short

  • “Maximum is 327680 bytes” is upload.maximum_data_size from the core's boards.txt. 394 of the 409 boards in the ESP32 package use this same number. The IDE subtracts your globals and calls the rest “for local variables”.
  • loop() runs as a FreeRTOS task with an 8,192-byte stack. A 10,000-byte local array crashed the board on every pass (Stack canary watchpoint triggered (loopTask)), while the IDE reported 306,088 bytes left. With SET_LOOP_TASK_STACK_SIZE(16 * 1024) the same sketch runs.
  • Globals hit the real limit earlier: the linker stopped at 296,192 bytes (dram0_0_seg overflowed) while the IDE showed 90%. The limit depends on how much code is placed in IRAM.
  • “Maximum is 1310720 bytes” for the sketch is the app0 partition of the default Flash Size: 4MB setting. On a 16 MB board, with 16MB and 16M Flash (3MB APP/9.9MB FATFS), the same line says 3145728.
  • The 8 MB of PSRAM is disabled by default and is not counted in either line.

306 KB for locals, and a crash

Here is a sketch with one 10,000-byte local array in loop():

void loop() {
  uint8_t buf[10000];
  memset(buf, 0x55, sizeof buf);
  Serial.printf("buf ok, sum=%u\n", (unsigned)(buf[0] + buf[9999]));
  delay(1000);
}

The build output for it, with the default settings of the ESP32S3 Dev Module board:

Sketch uses 273721 bytes (20%) of program storage space. Maximum is 1310720 bytes.
Global variables use 21592 bytes (6%) of dynamic memory, leaving 306088 bytes for local variables. Maximum is 327680 bytes.

306,088 bytes “for local variables” suggests that 10,000 bytes is nothing. On the board, the sketch never prints a single buf ok:

Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception).
Debug exception reason: Stack canary watchpoint triggered (loopTask)
...
Rebooting...
rst:0xc (RTC_SW_CPU_RST),boot:0x8 (SPI_FAST_FLASH_BOOT)

Then it boots, panics and reboots again. In three runs of 20 seconds each, the board panicked 27–28 times per run.

Where the numbers come from

Both maximums are written in the core's board definition file. In boards.txt of arduino-esp32 3.3.12 (lines 1184–1185):

esp32s3.upload.maximum_size=1310720
esp32s3.upload.maximum_data_size=327680

The Arduino platform specification describes {upload.maximum_data_size} as “available dynamic memory for global variables”. Nothing here is calculated from the chip.

The messages themselves come from arduino-cli, which Arduino IDE uses to build. The templates in arduino-cli 1.4.1 (the version bundled with Arduino IDE 2.3.8) match the source of internal/arduino/builder/sizer.go:

Sketch uses %[1]s bytes (%[3]s%%) of program storage space. Maximum is %[2]s bytes.
Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables. Maximum is %[2]s bytes.

The “leaving” value is just maxDataSize - dataSize. The size of “global variables” is taken from the ELF sections listed in the core's recipe.size.regex.data (platform.txt, line 206): .dram0.data, .dram0.bss, .dram1.data, .dram1.bss and .noinit.

The same number for almost every board

Out of 409 boards in boards.txt 3.3.12 (not counting menu options), 394 have maximum_data_size=327680:

maximum_data_sizeBoards
327680394
2949125
45219846
24248322
87162881

Why the wording comes from AVR

On an Arduino Uno (arduino:avr:uno) the same message says Maximum is 2048 bytes. There, static data, the heap and the stack share one 2 KB SRAM, so “what is left after globals is for local variables” roughly makes sense. On the ESP32 the stack of loop() is a separate, much smaller area.

loop() gets an 8 KB stack

On the ESP32, the core runs loop() inside a FreeRTOS task. It is created in cores/esp32/main.cpp (arduino-esp32 3.3.12):

#define ARDUINO_LOOP_STACK_SIZE 8192
// ...
xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), ...);

In ESP-IDF FreeRTOS the stack size is given in bytes, not words: “in IDF FreeRTOS, the task stack sizes are specified in bytes”. So loopTask has 8,192 bytes, and every local variable of loop() and of the functions it calls lives there. On the board, getArduinoLoopTaskStackSize() returned 8192.

The Arduino-ESP32 documentation does not mention this size; it is only visible in the source.

Not all 8,192 bytes are free for your array. Before the first pass of loop(), the high water mark of the task (uxTaskGetStackHighWaterMark) was 7,032 bytes. A 10,000-byte array does not fit, and the stack overflow check stops the chip with Stack canary watchpoint triggered (loopTask): the name of the task is right in the message.

Where the stack runs out

The same sketch with different array sizes, default 8 KB stack:

Local array, bytesResultRuns
6,000runs; 1,768 bytes of stack free after the first pass1
7,000Stack canary watchpoint triggered (loopTask)1
7,500Guru Meditation Error: Core 1 panic'ed (Double exception)1
8,000Stack canary watchpoint triggered (loopTask)1
10,000Stack canary watchpoint triggered (loopTask), reboot loop3 of 3
10,000, 16 KB stackruns; 5,724 bytes free after the first pass, then 4,6683 of 3

The 7,500-byte case is worth noting: instead of the canary message, the chip reported a Double exception, which does not name the task or mention the stack. A stack overflow does not always produce a clear message.

What to do

A large buffer does not have to live on the loopTask stack. Three options:

1. Make it static or global

void loop() {
  static uint8_t buf[10000];   // not on the task stack
  memset(buf, 0x55, sizeof buf);
  // ...
}

A static local is stored with the globals, so it shows up in the “Global variables use …” line instead.

2. Allocate it with malloc

uint8_t *buf;

void setup() {
  buf = (uint8_t *)malloc(10000);   // from the heap
}

3. Give loop() a bigger stack

SET_LOOP_TASK_STACK_SIZE(16 * 1024);

void loop() {
  uint8_t buf[10000];
  // ...
}

SET_LOOP_TASK_STACK_SIZE is a macro from the core's Arduino.h; it is placed at file scope. This is the variant that was tested on the board: with a 16,384-byte stack the 10,000-byte sketch printed loop task stack size: 16384 bytes and then buf ok, sum=170, stack free now: 5724 bytes, with no crash in 3 runs of 3.

The limit for globals is lower

The 327,680 maximum is not a real limit for globals either. A sketch with one global uint8_t big[N] was built with different N, using a binary search for the largest array that still links:

NBuild output
100,000Global variables use 121592 bytes (37%) …, builds
274,599Global variables use 296192 bytes (90%) of dynamic memory, leaving 31488 bytes for local variables. Maximum is 327680 bytes. and Low memory available, stability problems may occur., builds
274,607 (+8)region `dram0_0_seg' overflowed by 8 bytes, linker error
300,000region `dram0_0_seg' overflowed by 25400 bytes

The limit on this build is 296,192 bytes of global variables, 90% on the IDE's scale. The IDE's own check (an error above 327,680) is never reached: the linker refuses first. An earlier version of the sketch, without the print in setup(), hit the same 296,192 limit (at N = 274,600).

Why it depends on the build

In the linker script ld/memory.ld (esp32s3-libs 3.3.12), dram0_0_seg starts at 0x3FC88000, and its length is computed as 0x403CB700 − 0x40378000 = 341,760 bytes. Code placed in IRAM uses the same internal SRAM, so the start of the data region moves up by the size of that code. ESP-IDF says this directly:

ESP-IDF: Memory Types

“The maximum statically allocated DRAM size is reduced by the IRAM size of the compiled application.”

So 296,192 is the limit for this sketch on this core version. More code in IRAM means less room for globals.

It boots, but the heap is small

The sketch with the largest array (N = 274,599) boots and runs in 3 runs of 3. It reports:

# N=274599 global array at 0x3fc96ad8; heap size 122208, free 84696, largest block 31732

For comparison, a default build without the array has 358,788 bytes of free heap, with a largest free block of 303,092 bytes. Static arrays and the heap come out of the same internal RAM.

Sketch size is a setting

The other line, Maximum is 1310720 bytes for the sketch, is a real limit, but it is set by the board menu. By default the ESP32S3 Dev Module uses esp32s3.build.flash_size=4MB and the partition scheme Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS). Its app0 partition is 0x140000 bytes, which is 1,310,720 bytes or 1.25 MiB (tools/partitions/default.csv).

The flash chip on the test board is larger. esptool flash-id reports Detected flash size: 16MB, and esp_flash_get_physical_size() returns 16,777,216. The partition table read on the board:

PartitionFlash Size: 4MB (default)16MB, 3MB APP/9.9MB FATFS
nvs20,48020,480
otadata8,1928,192
app01,310,7203,145,728
app11,310,7203,145,728
spiffs / ffat1,441,79210,354,688
coredump65,53665,536
partitions end at4,194,30416,777,216

With the default setting, the partitions end at 4 MiB, and the remaining 12 MB of the 16 MB chip are not used.

In Tools, set Flash Size to 16MB and Partition Scheme to 16M Flash (3MB APP/9.9MB FATFS) (FlashSize=16M,PartitionScheme=app3M_fat9M_16MB in arduino-cli). The same sketch then reports:

Sketch uses 275373 bytes (8%) of program storage space. Maximum is 3145728 bytes.

3,145,728 bytes is 3 MiB. The megabytes in the menu names are binary: 1.25 MiB = 1,310,720 bytes, 3 MiB = 3,145,728 bytes.

PSRAM is not in either line

The test board has 8 MB of PSRAM (esptool: Embedded PSRAM 8MB). With the default settings it is off: the first, default option of the PSRAM menu in boards.txt is esp32s3.menu.PSRAM.disabled=Disabled, and on the board the sketch printed psram: 0 bytes (psramFound=0).

Neither line counts it: the maximums are constants from boards.txt, and recipe.size.regex.data only counts the .dram* and .noinit sections.

Summary

Line in Arduino IDEWhat it really is
Maximum is 1310720 bytes (sketch)the app partition; change it with Tools → Flash Size and Partition Scheme
Maximum is 327680 bytes (globals)a constant from boards.txt; the real limit is lower and depends on the build (296,192 here)
leaving … bytes for local variablesthe constant minus your globals; locals in loop() get an 8 KB stack
(not shown)PSRAM: disabled by default, not counted

Test setup and code

BoardESP32-S3 (QFN56) rev v0.2, 16 MB flash, 8 MB embedded PSRAM, connected through a CH343 USB-serial bridge
Corearduino-esp32 3.3.12, board esp32:esp32:esp32s3 (ESP32S3 Dev Module), all settings default unless stated
Toolchainarduino-cli 1.4.1 from Arduino IDE 2.3.8; build messages are quoted verbatim
Flash checkesptool 5.3.1 flash-id, esp_flash_get_physical_size(), partition table read on the board
Methodarray size passed with -DN=… through compiler.cpp.extra_flags; serial log captured after each upload (20 s for the crashing 10,000-byte build); 3 runs for the main cases, 1 run for the threshold sizes

The numbers apply to this ESP32-S3 board and core version. Other ESP32 chips and other builds will give different limits for globals.

stack.ino · full file
// A local array in loop() lives on the loopTask stack (8192 bytes by default).
// N is passed with -D. STACK (optional) -> SET_LOOP_TASK_STACK_SIZE.
#ifndef N
#define N 10000
#endif
#ifdef STACK
SET_LOOP_TASK_STACK_SIZE(STACK);
#endif

void setup() {
  Serial.begin(115200);
  delay(300);
  Serial.printf("# N=%d loop stack free before loop (high water mark): %u bytes\n", N,
                (unsigned)uxTaskGetStackHighWaterMark(NULL));
  Serial.printf("# loop task stack size: %u bytes\n", (unsigned)getArduinoLoopTaskStackSize());
}

void loop() {
  uint8_t buf[N];
  memset(buf, 0x55, sizeof buf);
  Serial.printf("buf ok, sum=%u, stack free now: %u bytes\n", (unsigned)(buf[0] + buf[N - 1]),
                (unsigned)uxTaskGetStackHighWaterMark(NULL));
  delay(1000);
}

Also: globals.ino, the largest global array and the heap left after it; info.ino, physical flash size, partition table, PSRAM and heap.

Sources

  1. arduino-esp32 3.3.12: boards.txt esp32s3.upload.maximum_size=1310720 · esp32s3.upload.maximum_data_size=327680 · esp32s3.build.flash_size=4MB · esp32s3.menu.PSRAM.disabled=Disabled
  2. Arduino platform specification “{upload.maximum_data_size}: available dynamic memory for global variables”
  3. arduino-cli: internal/arduino/builder/sizer.go “Global variables use %[1]s bytes (%[3]s%%) of dynamic memory, leaving %[4]s bytes for local variables…” · maxDataSize-dataSize
  4. arduino-esp32 3.3.12: cores/esp32/main.cpp #define ARDUINO_LOOP_STACK_SIZE 8192 · xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), …)
  5. ESP-IDF: FreeRTOS (IDF) “in IDF FreeRTOS, the task stack sizes are specified in bytes”
  6. arduino-esp32 3.3.12: cores/esp32/Arduino.h #define SET_LOOP_TASK_STACK_SIZE(sz) …
  7. ESP-IDF: Memory Types (ESP32-S3) “The maximum statically allocated DRAM size is reduced by the IRAM size of the compiled application.”
  8. arduino-esp32 3.3.12: tools/partitions/default.csv app0, app, ota_0, 0x10000, 0x140000
  9. arduino-esp32 3.3.12: platform.txt recipe.size.regex.data: .dram0.data, .dram0.bss, .dram1.data, .dram1.bss, .noinit

Video

A short video on the same topic is on the ESPSSS YouTube channel (2:53, 2026-11-25). This page goes into more detail.

Also as a YouTube Short.