How to measure free INTERNAL heap when using PSRAM

jcolebaker
Posts: 64
Joined: Thu Mar 18, 2021 12:23 am

How to measure free INTERNAL heap when using PSRAM

Postby jcolebaker » Mon Apr 03, 2023 9:50 pm

We've started prototyping with an ESP32 module which has PSRAM to see how we could use it (due to RAM shortages). However, I am confused by how to measure the available free RAM with PSRAM in use.

The module has 2 MB PSRAM and I enabled it in the config with these option (some other options were set but they didn't look relevant to this question):

Code: Select all

CONFIG_ESP32_SPIRAM_SUPPORT=y
CONFIG_SPIRAM_USE_CAPS_ALLOC=y
From my reading, "CONFIG_SPIRAM_USE_CAPS_ALLOC" means that the PSRAM is only used if we explicitly use "heap_caps_malloc(size, MALLOC_CAP_SPIRAM)" to allocate heap, which we are not doing yet.

BEFORE adding PSRAM, I logged free heap like this:

Code: Select all

    ESP_LOGI(TAG, "Free Heap: %u bytes", xPortGetFreeHeapSize());
And this reported about 66816 bytes free heap.

AFTER adding PSRAM, this log message reported 2113379 bytes, so it looks like it's including the PSRAM. However, I want to report the free internal RAM, NOT PSRAM, so I can measure the effects of shifting various buffers to PSRAM.

I then tried logging a number of different "free size" measurements like this:

Code: Select all

   ESP_LOGI(
        TAG,
        "Free Heap: %u bytes\n"
        "  MALLOC_CAP_8BIT      %7zu bytes\n"
        "  MALLOC_CAP_DMA       %7zu bytes\n"
        "  MALLOC_CAP_SPIRAM    %7zu bytes\n"
        "  MALLOC_CAP_INTERNAL  %7zu bytes\n"
        "  MALLOC_CAP_DEFAULT   %7zu bytes\n"
        "  MALLOC_CAP_IRAM_8BIT %7zu bytes\n"
        "  MALLOC_CAP_RETENTION %7zu bytes\n",
        xPortGetFreeHeapSize(),
        heap_caps_get_free_size(MALLOC_CAP_8BIT),
        heap_caps_get_free_size(MALLOC_CAP_DMA),
        heap_caps_get_free_size(MALLOC_CAP_SPIRAM),
        heap_caps_get_free_size(MALLOC_CAP_INTERNAL),
        heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
        heap_caps_get_free_size(MALLOC_CAP_IRAM_8BIT),
        heap_caps_get_free_size(MALLOC_CAP_RETENTION)
    );
And I get this:

Code: Select all

I (2341) DEBG: Free Heap: 2109211 bytes
  MALLOC_CAP_8BIT      2109211 bytes
  MALLOC_CAP_DMA         14360 bytes
  MALLOC_CAP_SPIRAM    2094851 bytes
  MALLOC_CAP_INTERNAL    33980 bytes
  MALLOC_CAP_DEFAULT   2109211 bytes
  MALLOC_CAP_IRAM_8BIT       0 bytes
  MALLOC_CAP_RETENTION       0 bytes
"MALLOC_CAP_8BIT", "MALLOC_CAP_SPIRAM" and "MALLOC_CAP_DEFAULT" are all including the PSRAM. Should I look at "MALLOC_CAP_DMA" or "MALLOC_CAP_INTERNAL"? Both have gone DOWN after turning on PSRAM, even though I haven't changed anything else.

If I REMOVE the PSRAM options from the config, the free heap log shows this:

Code: Select all

I (1847) DEBG: Free Heap: 68804 bytes
  MALLOC_CAP_8BIT        68804 bytes
  MALLOC_CAP_DMA         68804 bytes
  MALLOC_CAP_SPIRAM          0 bytes
  MALLOC_CAP_INTERNAL    93452 bytes
  MALLOC_CAP_DEFAULT     68804 bytes
  MALLOC_CAP_IRAM_8BIT       0 bytes
  MALLOC_CAP_RETENTION       0 bytes
So without PSRAM, "MALLOC_CAP_8BIT", "MALLOC_CAP_DMA" and "MALLOC_CAP_DEFAULT" are reporting the same value as "xPortGetFreeHeapSize", but "MALLOC_CAP_INTERNAL" reports MORE for some reason?

So how do I interpret these numbers? Which is most comparable to the old measurement of "Free heap" I was logging before? Specifically, I want to see the effect of any code changes on the available free heap in internal RAM, I.e. can we increase available internal RAM by moving some buffers to PSRAM...

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: How to measure free INTERNAL heap when using PSRAM

Postby ESP_Sprite » Tue Apr 04, 2023 2:25 am

Both have gone DOWN after turning on PSRAM, even though I haven't changed anything else.
This is correct. Psram itself needs some internal memory, and for non-ECO3 ESP32 chips we compile in some workarounds for psram bugs which also increase the internal memory by a bit.

jcolebaker
Posts: 64
Joined: Thu Mar 18, 2021 12:23 am

Re: How to measure free INTERNAL heap when using PSRAM

Postby jcolebaker » Tue Apr 04, 2023 10:32 pm

Is there any way to reduce this internal RAM usage? According to the free heap information, we've lost 59,472 bytes of internal heap by enabling the PSRAM. That makes the value of PSRAM a bit limited, since we'll have to do a lot of work just to get back to the same amount of free internal heap as we had before.

ESP_Sprite
Posts: 9746
Joined: Thu Nov 26, 2015 4:08 am

Re: How to measure free INTERNAL heap when using PSRAM

Postby ESP_Sprite » Wed Apr 05, 2023 12:42 am

The general advise is to follow this guide, but it's not psram-specific. You can also enable CONFIG_SPIRAM_USE_MALLOC (iirc that's what it's called) which allows ESP-IDF to allocate large internal structures into PSRAM automatically.

Who is online

Users browsing this forum: Bing [Bot] and 125 guests