Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest
The attribute macros from esp_attr.h move a function or variable out of its default place, and the place decides what is kept across a software restart or deep sleep. From ESP-IDF v5.5.5 esp_attr.h and the Memory Types guide, checked on an ESP32-S3.
In short
- By default code runs from flash and const data is read from flash;
IRAM_ATTRandDRAM_ATTRmove a function or a constant into internal RAM.1 RTC_DATA_ATTRkeeps its value through deep sleep, but is initialized again after any other reset, includingesp_restart().1RTC_NOINIT_ATTRkeeps its value throughesp_restart()and deep sleep; __NOINIT_ATTR only through a software restart.1EXT_RAM_BSS_ATTRdoes nothing unlessCONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORYis enabled; PROGMEM is always empty on ESP32.1
Macros
The macros are defined in esp_attr.h. Restart here means a software restart such as esp_restart(); deep sleep means waking from it.
| Macro | Places | Kept after restart | Kept in deep sleep | Notes |
|---|---|---|---|---|
IRAM_ATTR | Function → internal IRAM instead of flash | — | — | ISRs registered with ESP_INTR_FLAG_IRAM; timing-critical code1 |
DRAM_ATTR | Data → internal DRAM instead of flash | — | — | Constants read by IRAM code (otherwise they may stay in flash)1 |
DMA_ATTR | DRAM, 4-byte aligned | — | — | DMA buffers1 |
WORD_ALIGNED_ATTR | Alignment only: 4 bytes | — | — | __attribute__((aligned(4)))1 |
RTC_DATA_ATTR | RTC memory (.rtc.data) | No: initialized again | Yes | Initialized on every reset except a deep-sleep wakeup1 |
RTC_NOINIT_ATTR | RTC memory (.rtc_noinit), not initialized | Yes | Yes | Garbage after power-on: check a magic value1 |
RTC_FAST_ATTR / RTC_SLOW_ATTR | RTC FAST / RTC SLOW memory explicitly | No: initialized again | Yes | Chips with RTC FAST memory only (e.g. ESP32-C3) place both there1 |
__NOINIT_ATTR | Internal RAM (.noinit), not initialized | Yes | No: RAM is powered off | Garbage after power-on2 |
EXT_RAM_BSS_ATTR | Zero-initialized data → PSRAM | No: zeroed | No: zeroed | Needs CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY, otherwise expands to nothing1 |
EXT_RAM_NOINIT_ATTR | PSRAM (.ext_ram_noinit), not initialized | Yes, per esp_attr.h (not measured: the test board ran without PSRAM) | — | Needs CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY, otherwise same as __NOINIT_ATTR1 |
PROGMEM | Nothing: the macro is empty | — | — | const data is in flash anyway; pgm_read_*() are plain reads3 |
Checked on a board
Each variable was set, then the chip did esp_restart(), then a deep sleep. Addresses as printed by the sketch.
| Variable | Address | After esp_restart() | After deep sleep |
|---|---|---|---|
RTC_DATA_ATTR | 0x50000200 | reset to its initializer | kept |
RTC_NOINIT_ATTR | 0x5000020c | kept | kept |
__NOINIT_ATTR | 0x3fc98ee8 | kept | lost (random value) |
plain global | 0x3fc98f00 | reset to its initializer | reset to its initializer |
EXT_RAM_BSS_ATTR | 0x3fc98ef0 (internal RAM) | — | — |
IRAM_ATTR function | 0x403783c0 | — | — |
PROGMEM const | 0x3c03a062 (flash, data bus) | — | — |
Example
#include <esp_attr.h>
#include <esp_sleep.h>
RTC_DATA_ATTR uint32_t wakeCount = 0; // kept in deep sleep, back to 0 after any other reset
RTC_NOINIT_ATTR uint32_t rtcMagic; // never initialized: check a magic value
RTC_NOINIT_ATTR uint32_t resets; // kept across esp_restart() and deep sleep
__NOINIT_ATTR uint32_t lastError; // kept across esp_restart(), lost in deep sleep
static const DRAM_ATTR uint8_t steps[4] = {1, 2, 4, 8}; // in RAM, like the ISR below
volatile uint32_t pulses = 0;
void IRAM_ATTR onPulse() {
pulses += steps[pulses & 3];
}
void setup() {
Serial.begin(115200);
if (rtcMagic != 0xC0FFEE) { // first boot after power-on
rtcMagic = 0xC0FFEE;
resets = 0;
lastError = 0;
}
if (esp_sleep_get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED) {
lastError = 0; // woke from deep sleep: internal RAM was off, the old value is garbage
}
resets++;
wakeCount++;
Serial.printf("resets=%lu wakeCount=%lu lastError=%lu\n",
(unsigned long)resets, (unsigned long)wakeCount, (unsigned long)lastError);
attachInterrupt(digitalPinToInterrupt(4), onPulse, FALLING);
}
void loop() {
delay(5000);
esp_sleep_enable_timer_wakeup(10ULL * 1000000); // 10 s
esp_deep_sleep_start();
}Pitfalls
RTC_DATA_ATTR is not a reset counter
It is initialized again on esp_restart(), a panic or a watchdog reset. To count those, use RTC_NOINIT_ATTR with a magic value.4
No RTC memory, empty macros
Where neither SOC_RTC_FAST_MEM_SUPPORTED nor SOC_RTC_SLOW_MEM_SUPPORTED is set, RTC_DATA_ATTR, RTC_NOINIT_ATTR, RTC_FAST_ATTR and RTC_SLOW_ATTR expand to nothing and the variable is ordinary RAM. In ESP-IDF v5.5.5 this is the case for ESP32-C2 and ESP32-C61.1
EXT_RAM_BSS_ATTR in Arduino
The option is fixed when the Arduino libraries are built. In the arduino-esp32 3.3.12 ESP32-S3 libraries it is not set, so the variable stays in internal RAM. Check with #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY before relying on it.1
Constants inside an IRAM function
A const table or string used inside an IRAM_ATTR function can still end up in flash. Mark it DRAM_ATTR (strings: DRAM_STR()).2
See also
- Where did 512 KB go? The ESP32-S3 memory map
- C/C++ type sizes on ESP32
- Compile-time checks for chip, core and ESP-IDF version
- ESP32 chip families: compare and choose
Sources
Each number links here; open “What it confirms” for the exact quotes. Checked 2026-11-28.
- ESP-IDF v5.5.5: components/esp_common/include/esp_attr.h primary
What it confirms (15)
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: in short “// Places code into IRAM instead of flash … // Forces data into DRAM instead of flash … By default, constant data is placed by the linker into a region mapped to the MMU flash cache.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: in short “will keep its value // during a deep sleep / wake cycle. … Data in RTC memory is initialized whenever the SoC restarts, except when waking from Deep-sleep.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: in short “will keep its value // after restart or during a deep sleep / wake cycle. … should keep its value after software restart … In Deep-sleep mode, the CPUs, most of the RAM, and all digital peripherals that are clocked from APB_CLK are powered off.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: in short “#else #define EXT_RAM_BSS_ATTR #endif … #define PROGMEM ”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: IRAM_ATTR “// Places code into IRAM instead of flash … #define IRAM_ATTR _SECTION_ATTR_IMPL(".iram1", __COUNTER__) … Interrupt handlers must be placed into IRAM if ESP_INTR_FLAG_IRAM is used when registering the interrupt handler.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: DRAM_ATTR “// Forces data into DRAM instead of flash … Strings or constants inside an IRAM_ATTR function may not be placed in RAM automatically. It is possible to use DRAM_ATTR attributes to mark these”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: DMA_ATTR “#define DMA_ATTR WORD_ALIGNED_ATTR DRAM_ATTR … Use macro DMA_ATTR to declare global/local static variables”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: WORD_ALIGNED_ATTR “// Forces data to be 4 bytes aligned … #define WORD_ALIGNED_ATTR __attribute__((aligned(4)))”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: RTC_DATA_ATTR “#define RTC_DATA_ATTR _SECTION_ATTR_IMPL(".rtc.data", __COUNTER__) … // Any variable marked with this attribute will keep its value // during a deep sleep / wake cycle. … Data in RTC memory is initialized whenever the SoC restarts, except when waking from Deep-sleep.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: RTC_NOINIT_ATTR “// Forces data into RTC memory of .noinit section. … // after restart or during a deep sleep / wake cycle. … #define RTC_NOINIT_ATTR _SECTION_ATTR_IMPL(".rtc_noinit", __COUNTER__)”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: RTC_FAST_ATTR / RTC_SLOW_ATTR “// Allows to place data into RTC_FAST memory. … // Allows to place data into RTC_SLOW memory. … Data in RTC memory is initialized whenever the SoC restarts, except when waking from Deep-sleep. … In ESP32-C3, there is only RTC FAST memory, so if some variables in the program are marked by RTC_DATA_ATTR, RTC_SLOW_ATTR or RTC_FAST_ATTR attributes, all of them go to RTC FAST memory.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: EXT_RAM_BSS_ATTR “#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY // Forces bss variable into external memory. " #define EXT_RAM_BSS_ATTR _SECTION_ATTR_IMPL(".ext_ram.bss", __COUNTER__) #else #define EXT_RAM_BSS_ATTR #endif … By applying the EXT_RAM_BSS_ATTR macro, zero-initialized data can also be placed into external RAM. To use this macro, the CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY needs to be enabled.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: EXT_RAM_NOINIT_ATTR “// Forces data into external memory noinit section to avoid initialization after restart. … If the CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY is not enabled, EXT_RAM_NOINIT_ATTR will behave just as __NOINIT_ATTR, it will make data to be placed into .noinit segment in internal RAM.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: pitfall: No RTC memory, empty macros “#if CONFIG_SOC_RTC_FAST_MEM_SUPPORTED || CONFIG_SOC_RTC_SLOW_MEM_SUPPORTED … #define RTC_DATA_ATTR #define RTC_NOINIT_ATTR #define RTC_RODATA_ATTR”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: pitfall: EXT_RAM_BSS_ATTR in Arduino “#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY”
- ESP-IDF v5.5.5 docs (esp32s3): Memory Types primary
What it confirms (2)
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: __NOINIT_ATTR “The macro __NOINIT_ATTR can be used as attribute to place data into .noinit section. The values placed into this section will not be initialized at startup and should keep its value after software restart. … In Deep-sleep mode, the CPUs, most of the RAM, and all digital peripherals that are clocked from APB_CLK are powered off.”
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: pitfall: Constants inside an IRAM function “Strings or constants inside an IRAM_ATTR function may not be placed in RAM automatically. … const static char *MSG = DRAM_STR("I am a string stored in RAM");”
- arduino-esp32 3.3.12: cores/esp32/pgmspace.h primary
What it confirms (1)
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: PROGMEM “#define PROGMEM … #define pgm_read_byte(addr) (*(const unsigned char *)(addr)) … By default, constant data is placed by the linker into a region mapped to the MMU flash cache.”
- ESP-IDF v5.5.5 docs (esp32s3): Deep-sleep Wake Stubs primary
What it confirms (1)
- Memory attributes: IRAM_ATTR, RTC_DATA_ATTR and the rest: pitfall: RTC_DATA_ATTR is not a reset counter “Data in RTC memory is initialized whenever the SoC restarts, except when waking from Deep-sleep. … // after restart or during a deep sleep / wake cycle.”