Where did 512 KB go? The ESP32-S3 memory map

The ESP32-S3 has 512 KB of SRAM, but a fresh Arduino sketch sees about 350 KB of free heap, and the largest block it can allocate is about 300 KB. Here is where the rest goes, with addresses and numbers printed on a real board.

In short

  • In setup() of a small test sketch, ESP.getFreeHeap() returns 359,024 bytes and ESP.getMaxAllocHeap() returns 303,092, out of 524,288 bytes of SRAM.
  • The 512 KB split, to the byte: 48 KB cache (16 KB instructions + 32 KB data), 60.5 KB code in IRAM, 21.2 KB global variables, 2.9 KB kept by the ROM drivers, 379.5 KB heap. Before setup() the system has already taken 36.4 KB of that heap.
  • The heap is five separate regions, so the largest single allocation is smaller than the free total. Check getMaxAllocHeap() before a big malloc.
  • With PSRAM enabled (it is disabled by default in Arduino IDE), malloc(4096) stays in internal SRAM and malloc(4097) goes to PSRAM. new behaves the same.
  • Reading a new cache line costs 12 cycles from internal SRAM, 120 from PSRAM and 283 from flash, once the data no longer fits in the cache.

512 KB on paper, 350 KB in practice

The ESP32-S3 datasheet lists 512 KB of on-chip SRAM “for data and instructions”. Ask a fresh Arduino sketch how much of it is free, and the answer is noticeably smaller:

ESP.getFreeHeap()     = 359024
ESP.getMaxAllocHeap() = 303092

That is 350.6 KB free, about 68% of the SRAM, and the biggest block a single malloc can return is 296 KB. Both numbers were printed in setup() on an ESP32-S3 with arduino-esp32 3.3.12 and default board settings.

The rest is not lost. Part of the SRAM works as a cache for flash and PSRAM, part holds code that must not live in flash, part holds global variables, and the system takes some of the heap before your code starts. The sections below account for every byte.

Throughout this page, 1 KB = 1024 bytes, as in Espressif's documentation.

The memory map

On the chip itself there are:

Flash and PSRAM are external. The CPU does not read them directly: the datasheet describes access “through high-speed caches”, and the MMU maps the external memory into the address space. Flash and PSRAM share one 32 MB virtual address window and the same cache.

MemorySizeHow the CPU reaches it
ROM384 KBon chip
Internal SRAM (SRAM0 + SRAM1 + SRAM2)32 + 416 + 64 KBon chip
RTC FAST8 KBon chip, kept in deep sleep
RTC SLOW8 KBon chip, kept in deep sleep
Flash (16 MB on the test board)externalthrough the cache and the MMU
PSRAM (8 MB OPI on the test board)externalthrough the same cache and MMU

Where the 512 KB go

Using linker symbols, the core's precompiled sdkconfig and the list of heap regions, the 524,288 bytes of SRAM can be accounted for exactly:

PartBytesWhere the number comes from
Instruction cache (ICache)16,384CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE=0x4000; IRAM starts at 0x40374000 = 0x40370000 + 0x4000
Data cache (DCache)32,768CONFIG_ESP32S3_DATA_CACHE_SIZE=0x8000; the heap in SRAM2 is only 0x3FCF0000, 32,768 bytes, the other half is the cache
Code in IRAM (IRAM_ATTR, core, FreeRTOS, …)61,952_iram_start … _iram_end
Global variables, .data + .bss21,688_data_start … heap start
ROM driver variables2,936ROM layout table read from the chip, see below
Heap in internal SRAM388,560sum of the heap regions from heap_caps_print_heap_info
Total524,288= 512 KB

Cache: 48 KB

Part of SRAM0 can be allocated “to either CPU or ICACHE”, and part of SRAM2 “to either CPU/GDMA or DCACHE”. The arduino-esp32 build uses 16 KB for the instruction cache and 32 KB for the data cache. That memory is never visible to your sketch.

Code in IRAM: about 60 KB

By default code goes to flash. Code that has to run from RAM, such as interrupt handlers and timing-critical parts of the system, is placed into internal SRAM instead. In this build that is 61,952 bytes.

Global variables: about 21 KB

Initialized globals (.data) and zero-initialized globals (.bss) sit in SRAM right after the IRAM code. In this build .data is 14,630 bytes and .bss 7,056 bytes.

Heap: about 380 KB

Everything that is left becomes the heap: 388,560 bytes of internal SRAM. The memory the ROM used during boot is also returned to it; ESP-IDF's memory_layout.c marks that area as “ROM reserved area, recycled by heap allocator in app_main task”.

Arduino also adds the 8 KB RTC FAST memory to the heap (CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP=y): 8,152 bytes at 0x600FE000. That is why ESP.getHeapSize() reports 396,712 = 388,560 + 8,152.

The 2,936 bytes kept by the ROM

After adding up cache, IRAM, globals and heap, 2,936 bytes were still missing. They belong to code that is built into the chip's ROM.

The ROM contains a memory layout table (ets_rom_layout_p, structure ets_rom_layout_t from esp_rom/esp32s3/include/esp32s3/rom/rom_layout.h). At start-up ESP-IDF reserves everything from dram0_rtos_reserved_start to the end of SRAM1 (components/heap/port/memory_layout_utils.c). On the test chip that is 0x3FCEEE34 … 0x3FCF0000, 4,556 bytes. The table read from the chip shows what is inside: the .data and .bss of the ROM's own drivers.

AddressesBytesWhat
0x3FCEEE34 … 0x3FCEF130764USB driver in ROM
0x3FCEF130 … 0x3FCEF17468UART in ROM
0x3FCEF174 … 0x3FCEF1A448Bluetooth in ROM, data + bss
0x3FCEF1A4 … 0x3FCEF3D4560Wi-Fi in ROM: coexist, net80211, pp
0x3FCEF3D4 … 0x3FCEF66C664PHY (radio) in ROM
0x3FCEF66C … 0x3FCEF820436not described in the table
0x3FCEF820 … 0x3FCEF958312Wi-Fi interface tables in ROM
0x3FCEF958 … 0x3FCEFFAC1,620not kept: this range is a heap region
0x3FCEFFAC … 0x3FCF000084ROM global pointers (syscall_table_ptr at 0x3FCEFFD4, ets_ops_table_ptr at 0x3FCEFFFC and others, from esp32s3.rom.ld)

764 + 68 + 48 + 560 + 664 + 436 + 312 + 84 = 2,936 bytes, exactly the amount that was missing.

The heap is five separate pieces

Two things make the usable heap smaller than 380 KB.

The system gets there first

By the time setup() runs, the system has already allocated 37,296 bytes (36.4 KB) of the heap in internal SRAM: task stacks, including the 8 KB stack of loopTask, Serial buffers and similar. The ESP-IDF heap documentation says the same in general terms: “some memory is allocated from the heap before the FreeRTOS scheduler is started”.

The arithmetic: 359,024 bytes are free, 7,760 of them in RTC FAST, so 351,264 are free in SRAM, out of 388,560.

The free memory is not contiguous

The heap is assembled from whatever was left between everything else, and “each contiguous region of memory contains its own memory heap”. On the test board there are five regions:

StartLength, BFree in setup()What it is
0x3FC986B8331,864309,112main part of SRAM1, right after .bss
0x3FCF000032,76832,020SRAM2; the other half is the data cache
0x3FCE971022,3089,828memory the ROM used during boot
0x600FE0008,1527,760RTC FAST
0x3FCEF9581,6200inside the ROM-reserved range

One malloc can only take memory from one region. The largest free block is in the main region: 303,092 bytes. So the largest single allocation is about 296 KB, not 350.

The heap documentation warns about exactly this: because of fragmentation, “it is probably not possible to allocate a single block of memory of this size”, and points to heap_caps_get_largest_free_block(). In Arduino that is what ESP.getMaxAllocHeap() returns.

Seen during testing

In one test sketch, after a 256 KB malloc from internal memory, a further 128 KB and even a 64 KB malloc failed, although about 100 KB was free in total: the free space was split across regions. malloc returned NULL, and the following memcpy into NULL crashed with StoreProhibited.

if (ESP.getMaxAllocHeap() < size) { /* won't fit in one piece */ }

One memory, two addresses

SRAM1, the 416 KB block that holds most of the memory, is “accessed by CPU's DBUS, IBUS and GDMA at the same time”: it is wired both to the instruction bus and to the data bus, at different addresses. The same byte has two addresses.

On the test board, the IRAM code and the global variables meet in the middle:

IRAM code ends   0x40383200   (instruction bus)
.data starts     0x3FC93200   (data bus)

The last digits match. The difference, 0x6F0000, is exactly the offset between the two buses for SRAM1: 0x40378000 − 0x3FC88000 in memory.ld. It is the same spot in the same physical SRAM, seen through two buses. ESP-IDF's memory_layout.c relies on this: “the part used by code is reserved … directly shift IRAM address to get reserved DRAM address”.

The boundary moves with the amount of IRAM code. With PSRAM: OPI PSRAM the same sketch has 65,280 bytes of IRAM code, IRAM ends at 0x40383F00, and .data starts at 0x3FC93F00: the boundary moved along with it.

Consequence

“Any internal SRAM which is not used for Instruction RAM will be made available as DRAM.” The reverse is also true: every function you mark with IRAM_ATTR takes RAM away from your data and your heap.

Where your sketch lives

The test sketch prints the address of typical objects and checks which region each one falls into. The short version:

void blink() {...}                    // 0x4201FB8C  flash
void IRAM_ATTR onPulse() {...}        // 0x40377B20  IRAM
const uint32_t table[256] = {...};    // 0x3C0391EC  flash
"hello"                               // 0x3C030487  flash
uint32_t counter;                     // 0x3FC96B28  SRAM
void setup() { uint32_t x; ... }      // 0x3FCEBD1C  SRAM, loopTask stack
RTC_DATA_ATTR uint32_t bootCount;     // 0x50000200  RTC

One detail from the ROM layout table: the ROM runs on two 8 KB boot stacks, 0x3FCE9710 … 0x3FCEB710 (core 0) and 0x3FCEB710 … 0x3FCED710 (core 1). After start-up that memory becomes the heap region at 0x3FCE9710, and the loopTask stack was allocated there. The local variable x in setup(), at 0x3FCEBD1C, sits where the ROM kept the boot stack of the second core.

The full list, with PSRAM disabled (default) and enabled:

ObjectPSRAM offPSRAM on (OPI)Region
normal function0x4201FB8C0x4201FFACflash, through the cache
IRAM_ATTR function0x40377B200x40377C58internal SRAM, instruction bus
const uint32_t table[]0x3C0391EC0x3C03931Cflash, through the cache
string literal0x3C0304870x3C03048Aflash, through the cache
global with initializer0x3FC932040x3FC93F04SRAM, .data
global without initializer0x3FC96B280x3FC979B8SRAM, .bss
static inside a function0x3FC932000x3FC93F00SRAM, .data
local in setup()0x3FCEBD1C0x3FCEBD1CSRAM, loopTask stack (in the heap)
RTC_DATA_ATTR0x500002000x50000200RTC SLOW
RTC_NOINIT_ATTR0x500002040x50000204RTC SLOW
malloc(100)0x3FCEC6A00x3FCEC180SRAM
malloc(4096)0x3FCEC7F80x3FCEC2D8SRAM
malloc(4097)0x3FCED8FC0x3C040EECSRAM / PSRAM
malloc(100 KB)0x3FC9F3C80x3C041F70SRAM / PSRAM
new uint8_t[20000]0x3FCD23D00x3C05B774SRAM / PSRAM
heap_caps_malloc(100 KB, MALLOC_CAP_INTERNAL)0x3FCB8BCC0x3FCA024CSRAM
String buffer (50 characters)0x3FCEC7140x3FCEC1F4SRAM

Flash constants and PSRAM both show up at 0x3C0… addresses, in the same data-bus window: both are reached through the same cache and MMU.

RTC memory and deep sleep

In deep sleep the RTC SLOW memory stays powered, and variables placed there “for example, using RTC_DATA_ATTR” are “kept powered on by default”. ESP-IDF's esp_attr.h describes RTC_DATA_ATTR as data that “will keep its value during a deep sleep / wake cycle”.

The test sketch increments three counters on every boot and goes into deep sleep for 0.5 seconds, three times:

BootReset reasonRTC_DATA_ATTRRTC_NOINIT_ATTRplain global
1power-on11033029511
2deep sleep wake-up21033029521
3deep sleep wake-up31033029531
4deep sleep wake-up41033029541

PSRAM and the 4 KB threshold

In Arduino IDE, PSRAM is disabled by default, even if the board has it: the first item of the board menu in boards.txt is esp32s3.menu.PSRAM.disabled=Disabled. With default settings, psramFound() returns 0 and ESP.getPsramSize() returns 0 on a board with 8 MB of PSRAM. Turn it on in Tools → PSRAM → OPI PSRAM (for this board).

With PSRAM enabled, malloc quietly sends large blocks out to PSRAM. The threshold is CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL: “When allocating a size less than or equal to the threshold, the allocator will try internal memory first”, and “allocations larger than this will be done from external RAM”.

malloc(4096);          // 0x3FCEC2D8  internal SRAM
malloc(4097);          // 0x3C040EEC  PSRAM
new uint8_t[20000];    // 0x3C05B774  PSRAM
SettingThreshold
ESP-IDF default (Kconfig.spiram.common)16,384 B
arduino-esp32 3.3.12 (precompiled sdkconfig)4,096 B

So in Arduino, anything above 4 KB goes to PSRAM: malloc(4097), malloc(100 KB) and new uint8_t[20000] all did. malloc(100), malloc(4096) and the buffer of a 50-character String stayed internal. heap_caps_malloc(..., MALLOC_CAP_INTERNAL) always returns internal memory (“Memory must be internal”).

getFreeHeap() does not count PSRAM

In arduino-esp32, ESP.getFreeHeap() calls heap_caps_get_free_size(MALLOC_CAP_INTERNAL), and ESP.getMaxAllocHeap() calls heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL). With PSRAM enabled:

CallBytesCounts
ESP.getFreeHeap()356,620internal only
ESP.getMaxAllocHeap()294,900internal only
ESP.getFreePsram()8,384,788PSRAM only
heap_caps_get_free_size(MALLOC_CAP_DEFAULT)8,741,220both

The internal heap is slightly smaller with PSRAM enabled: 392,848 bytes in total instead of 396,712. In that build the IRAM code is 3,328 bytes larger (65,280 vs 61,952), and the boundary between IRAM and data moved with it.

What a cache miss costs

Does it matter where a buffer ends up? While the data fits in the cache, PSRAM and flash are as fast as internal SRAM. The ESP-IDF docs put it this way: “frequently accessed variables in external RAM can be read and modified almost as quickly as in internal RAM”, but “when accessing large chunks of data (> 32 KB), the cache can be insufficient, and speeds will fall back to the access speed of the external RAM”.

To measure the cost of a miss, the test reads one word every 32 bytes, so every read touches a new cache line, over a buffer much larger than the cache:

A cache miss to PSRAM costs about 10× a read from internal SRAM, and a miss to flash about 24×. The documentation gives no specific figures; these are from the test board only.

MemorySequential read, MB/sCycles per new cache line
internal SRAM, 256 KB21212
PSRAM, 2 MB (does not fit in the cache)61120.5 (≈ 10×)
flash const, 512 KB (does not fit in the cache)27283 (≈ 24×)
PSRAM or flash, 16 KB, repeated pass (in the cache)21312, same as SRAM

The practical result: the same line of code can run at very different speeds depending on where its data lives, and with PSRAM enabled, the size of the malloc decides where a buffer goes.

What to do about it

Ask for internal memory explicitly

If a buffer has to be fast, don't leave its placement to the 4 KB threshold:

uint8_t* buf = (uint8_t*)heap_caps_malloc(32768, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
if (buf == NULL) { /* not enough contiguous internal memory */ }

Check the right number before a big allocation

Print your own memory map

heap_caps_print_heap_info(MALLOC_CAP_INTERNAL);

The output lists every heap region with its address, size and free space. A fragment from the test board:

At 0x3fc986b8 len 331864 free 309112 allocated 20056 min_free 79724
    largest_free_block 303092 alloc_blocks 57 free_blocks 4 total_blocks 61
At 0x3fcf0000 len 32768 free 32020 allocated 0 min_free 32020
    largest_free_block 31732 alloc_blocks 0 free_blocks 1 total_blocks 1

Spend IRAM deliberately

Every IRAM_ATTR function moves the IRAM/DRAM boundary towards the data and takes RAM away from your globals and heap. Use it where code has to run from RAM.

What these numbers do not cover

Test setup and code

BoardESP32-S3 (QFN56) rev v0.2, 2 cores at 240 MHz, 16 MB flash, 8 MB OPI PSRAM
Corearduino-esp32 3.3.12, board esp32:esp32:esp32s3
Toolchainarduino-cli from Arduino IDE 2.3.8
Settingsdefaults (PSRAM disabled) for the memory map; PSRAM=opi for the PSRAM and speed tests
Memory maplinker symbols, heap_caps_* calls and esp_ptr_* region checks, printed in setup()
Speedread loops in IRAM, CPU cycles via esp_cpu_get_cycle_count(), three passes per buffer
ROM layoutthe ets_rom_layout_p table read directly from the chip
memmap.ino (shortened) · full file
#include <esp_heap_caps.h>
#include <esp_memory_utils.h>
#include <esp_attr.h>

extern "C" {  // section boundaries from the linker script
extern int _iram_start, _iram_end, _data_start, _heap_low_start;
}

const uint32_t table[256] = {1, 2, 3};      // flash
uint32_t global_init[16] = {1, 2, 3};       // SRAM, .data
uint32_t counter;                           // SRAM, .bss
RTC_DATA_ATTR uint32_t bootCount = 0;       // RTC SLOW

void blink() { asm volatile("nop"); }              // flash
void IRAM_ATTR onPulse() { asm volatile("nop"); }  // IRAM

const char* region(const void* p) {
  if (esp_ptr_in_rtc_slow(p)) return "RTC SLOW";
  if (esp_ptr_external_ram(p)) return "PSRAM (via cache)";
  if (esp_ptr_in_drom(p)) return "FLASH rodata (DROM, via cache)";
  if (esp_ptr_in_iram(p)) return "internal SRAM, IRAM bus";
  if (esp_ptr_in_dram(p)) return "internal SRAM, DRAM bus";
  if (esp_ptr_executable(p)) return "FLASH code (IROM, via cache)";
  return "?";
}

void show(const char* name, const void* p) {
  Serial.printf("  %-28s %p  %s\n", name, p, region(p));
}

void setup() {
  Serial.begin(115200);
  delay(3000);
  Serial.printf("getFreeHeap=%u getMaxAllocHeap=%u\n",
                ESP.getFreeHeap(), ESP.getMaxAllocHeap());
  Serial.printf("iram %p..%p, data %p..%p\n",
                &_iram_start, &_iram_end, &_data_start, &_heap_low_start);

  uint32_t x = 1;                    // loopTask stack
  void* m_4k  = malloc(4096);        // SRAM
  void* m_4k1 = malloc(4097);        // PSRAM, if enabled
  show("blink()", (void*)&blink);
  show("IRAM_ATTR onPulse()", (void*)&onPulse);
  show("const uint32_t table[]", table);
  show("\"hello\"", "hello");
  show("uint32_t counter (bss)", &counter);
  show("setup() local x", &x);
  show("RTC_DATA_ATTR bootCount", &bootCount);
  show("malloc(4096)", m_4k);
  show("malloc(4097)", m_4k1);
  free(m_4k); free(m_4k1);

  // ... more objects and heap_caps_get_*_size() by capability: see the full file
  heap_caps_print_heap_info(MALLOC_CAP_INTERNAL);
}

void loop() { delay(1000); }

Also: memspeed.ino (read speed of SRAM, PSRAM and flash), rtcmem.ino (RTC variables across deep sleep), romlayout.ino (dump of the ROM layout table).

Sources

  1. ESP32-S3 Series Datasheet v2.2, §4.1.2 “384 KB ROM: for booting and core functions” · “512 KB on-chip SRAM: for data and instructions” · “RTC FAST memory: 8 KB SRAM … It can retain data in Deep-sleep mode” · “Through high-speed caches, ESP32-S3 can support…”
  2. ESP32-S3 Technical Reference Manual v1.8, §15.3.2 and the Cache chapter “512 KB in total, including 32 KB Internal SRAM0, 416 KB Internal SRAM1, and 64 KB Internal SRAM2” · SRAM0 “can be allocated to either CPU or ICACHE” · SRAM2 “can be allocated to either CPU/GDMA or DCACHE” · SRAM1 “Accessed by CPU's DBUS, IBUS and GDMA at the same time” · “The size of ICache can be configured to 16 KB or 32 KB…”
  3. ESP-IDF v5.5: Memory Types (ESP32-S3) “If a function is not explicitly placed into IRAM … or RTC memory, it is placed into flash” · “placing a function into IRAM may reduce delays caused by a cache miss” · “By default, constant data is placed by the linker into a region mapped to the MMU flash cache” · “Any internal SRAM which is not used for Instruction RAM will be made available as DRAM”
  4. ESP-IDF v5.5: Support for External RAM (ESP32-S3) “The 32 MB virtual addresses are shared with flash instructions and rodata” · “External RAM uses the same cache region as the external flash” · “When allocating a size less than or equal to the threshold, the allocator will try internal memory first” · “when accessing large chunks of data (> 32 KB), the cache can be insufficient, and speeds will fall back to the access speed of the external RAM”
  5. ESP-IDF v5.5: Heap Memory Allocation (ESP32-S3) “some memory is allocated from the heap before the FreeRTOS scheduler is started” · “Each contiguous region of memory contains its own memory heap” · “it is probably not possible to allocate a single block of memory of this size” · MALLOC_CAP_INTERNAL: “Memory must be internal”
  6. ESP-IDF v5.5: Sleep Modes (ESP32-S3) variables placed into RTC SLOW memory “(for example, using RTC_DATA_ATTR)” are “kept powered on by default”
  7. ESP-IDF source code v5.5.6: components/heap/port/esp32s3/memory_layout.c, components/heap/port/memory_layout_utils.c, components/esp_common/include/esp_attr.h, components/esp_psram/Kconfig.spiram.common “ROM reserved area, recycled by heap allocator in app_main task” · “the part used by code is reserved … directly shift IRAM address to get reserved DRAM address” · RTC_DATA_ATTR “will keep its value during a deep sleep / wake cycle” · “allocations larger than this will be done from external RAM”, default 16384
  8. arduino-esp32 source code 3.3.12: cores/esp32/Esp.cpp, cores/esp32/main.cpp, boards.txt, precompiled tools/esp32s3-libs/3.3.12/sdkconfig getFreeHeap → heap_caps_get_free_size(MALLOC_CAP_INTERNAL) · xTaskCreateUniversal(loopTask, "loopTask", getArduinoLoopTaskStackSize(), …) · esp32s3.menu.PSRAM.disabled=Disabled · CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL=4096, CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE=0x4000, CONFIG_ESP32S3_DATA_CACHE_SIZE=0x8000, CONFIG_ARDUINO_LOOP_STACK_SIZE=8192

Video

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

Also as a YouTube Short.