ESP32 IRAM and DRAM

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

ESP32 IRAM and DRAM

Postby zazas321 » Fri Feb 18, 2022 12:51 pm

Hello. I would like to understand more what exactly is IRAM and DRAM. After building my project, I get the following:

Code: Select all

Total sizes:
Used static DRAM:   58608 bytes (  65972 remain, 47.0% used)
      .data size:   22632 bytes
      .bss  size:   35976 bytes
Used static IRAM:  114003 bytes (  17069 remain, 87.0% used)
      .text size:  112976 bytes
   .vectors size:    1027 bytes
Used stat D/IRAM:  172611 bytes (  83041 remain, 67.5% used)
      .data size:   22632 bytes
      .bss  size:   35976 bytes
      .text size:  112976 bytes
   .vectors size:    1027 bytes
Used Flash size : 1199003 bytes
      .text     :  971051 bytes
      .rodata   :  227696 bytes
Total image size: 1472273 bytes (.bin may be padded larger)
I am slightly worried about 87% if IRAM used. It says that 17069 bytes are remaining.

1.In my program, there are various FreeRTOS tasks running doing their thing. If it says that 17069 bytes of static IRAM is remaining, does that mean that if by any chance, my program tries to occupy more than 17069 bytes the program will crash?

What will happen if in my main.c I will allocate a buffer of lets say 18000 bytes for example:
uint8_t test_buffer[18000]={0};

2. What exactly is DRAM compared to IRAM?

3. When I declare a local variable in some function, I assume the memory is allocated in the IRAM. Is that correct?

4. What happens when I declare a global variable? Where is it placed in memory?

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32 IRAM and DRAM

Postby WiFive » Fri Feb 18, 2022 3:17 pm

1. Static memory usage is determined at compile time and won't change.
2. IRAM is for code (instructions), DRAM is for data.
3. A local variable goes onto the task's stack. Stack was allocated from the heap. These are not part of static memory calculations.
4. It is placed in static dram so it would be part of the calculations.

Do some research on static memory vs dynamic memory, stack vs heap and local variables vs static variables.

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: ESP32 IRAM and DRAM

Postby zazas321 » Wed Feb 23, 2022 6:23 am

Thanks for the reply. Would you be able to suggest a good reading material on this kind of topic because I havent been able to find a good source.

1. It is still not clear for me what do you refer to as data memory (DRAM) and instruction memory (IRAM). Could you show me an example of something that goes into DRAM and something that goes into IRAM?

2. I have tried to declare a global buffer of size 65535 in my main.c

Code: Select all

uint8_t test_buffer[65535];

void app_main(void)
{
    

    esp_err_t ret;
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

...
...
..
...


After building the program, it does not seem that it is placed in in the dram:

Code: Select all

Total sizes:
Used static DRAM:   58584 bytes (  65996 remain, 47.0% used)
      .data size:   22624 bytes
      .bss  size:   35960 bytes
Used static IRAM:  113971 bytes (  17101 remain, 87.0% used)
      .text size:  112944 bytes
   .vectors size:    1027 bytes
Used stat D/IRAM:  172555 bytes (  83097 remain, 67.5% used)
      .data size:   22624 bytes
      .bss  size:   35960 bytes
      .text size:  112944 bytes
   .vectors size:    1027 bytes
Used Flash size : 1197147 bytes
      .text     :  969063 bytes
      .rodata   :  227828 bytes
Total image size: 1470337 bytes (.bin may be padded larger)
Can you help me understand what happens? I was expecting it to show up

I have found out that if I Initialise this buffer then I can finally see the total size update:

Code: Select all

uint8_t test_buffer[65535];
void app_main(void)
{
    esp_err_t ret;
    ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
        ESP_ERROR_CHECK(nvs_flash_erase());
        ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);

    for(int i = 0; i <65535;i++){
        test_buffer[i] = i;
    }
   

Code: Select all

Total sizes:
Used static DRAM:  124120 bytes (    460 remain, 99.6% used)
      .data size:   22624 bytes
      .bss  size:  101496 bytes
Used static IRAM:  113971 bytes (  17101 remain, 87.0% used)
      .text size:  112944 bytes
   .vectors size:    1027 bytes
Used stat D/IRAM:  238091 bytes (  17561 remain, 93.1% used)
      .data size:   22624 bytes
      .bss  size:  101496 bytes
      .text size:  112944 bytes
   .vectors size:    1027 bytes
Used Flash size : 1197175 bytes
      .text     :  969091 bytes
      .rodata   :  227828 bytes
Total image size: 1470365 bytes (.bin may be padded larger)

anhnq2349
Posts: 2
Joined: Wed Feb 23, 2022 9:15 am

Re: ESP32 IRAM and DRAM

Postby anhnq2349 » Wed Feb 23, 2022 9:19 am

How can i get info IDRAM and SRAM like you, guy? thx

zazas321
Posts: 231
Joined: Mon Feb 01, 2021 9:41 am

Re: ESP32 IRAM and DRAM

Postby zazas321 » Wed Feb 23, 2022 12:12 pm

You should try:

Code: Select all

idf.py size-components 

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32 IRAM and DRAM

Postby WiFive » Wed Feb 23, 2022 3:40 pm

1. You already saw your global variable goes to dram. Iram is for code aka functions but only functions that need to be in iram for fast execution instead of flash. For example interrupt handlers. The code is placed in iram based on linker script or manual marking of functions with iram attribute. There are many menuconfig settings related to iram function placement.
2. Compiler is smart, if you don't use a variable it will throw it away.

anhnq2349
Posts: 2
Joined: Wed Feb 23, 2022 9:15 am

Re: ESP32 IRAM and DRAM

Postby anhnq2349 » Wed Feb 23, 2022 3:44 pm

zazas321 wrote:
Wed Feb 23, 2022 12:12 pm
You should try:

Code: Select all

idf.py size-components 
tysm

boarchuz
Posts: 606
Joined: Tue Aug 21, 2018 5:28 am

Re: ESP32 IRAM and DRAM

Postby boarchuz » Wed Feb 23, 2022 4:48 pm

zazas321 wrote:
Wed Feb 23, 2022 6:23 am
Would you be able to suggest a good reading material on this kind of topic because I havent been able to find a good source.
https://blog.espressif.com/esp32-progra ... 9444d89387

Who is online

Users browsing this forum: No registered users and 81 guests