How to measure free INTERNAL heap when using PSRAM
Posted: 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):
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:
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:
And I get this:
"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:
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...
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
BEFORE adding PSRAM, I logged free heap like this:
Code: Select all
ESP_LOGI(TAG, "Free Heap: %u bytes", xPortGetFreeHeapSize());
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)
);
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
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 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...