Page 1 of 1

how to calculate free ram?

Posted: Mon Oct 04, 2021 4:58 pm
by alphago1000
I would just know the available ram in the execution of programm as for example:

Code: Select all

int freeRam()
{
    extern int __heap_start, *__brkval;
    int v;
    return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
but for esp32 wrover B

ps: i'm french

thanks for help

Re: how to calculate free ram?

Posted: Sat Oct 09, 2021 8:53 am
by ESP_Dazz
Does heap_caps_get_free_size() fulfil your requirements?

Re: how to calculate free ram?

Posted: Sat Oct 09, 2021 4:20 pm
by Craige Hales
Or maybe one of these
  • esp_get_free_heap_size()
  • esp_get_free_internal_heap_size()
  • esp_get_minimum_free_heap_size()
I found the last one (minimum free heap) most useful. I think it tracks over time the smallest available heap; I've seen it drop to 50KB if I use the web server aggressively but it is usually >100KB, maybe 150KB right after boot. I think if you have a memory leak it will probably go to zero after a while... I'm not sure what the difference between the first two is. I believe they report the current value, always >= minimum value.

Code: Select all

chip_model	ESP32
app_desc_idf_ver	v4.3.1
chip_features	50
cpu_frequency	160
crystal_frequency	40
chip_revision	1
chip_cores	2
chip_features_embedded_flash	0
chip_features_wifi_bgn	1
chip_features_bluetooth_le	1
chip_features_bluetooth_classic	1
available_heap	143936
available_internal_heap	143936
minimum_heap	131028

Re: how to calculate free ram?

Posted: Sat Oct 09, 2021 5:08 pm
by chegewara
1) esp_get_minimum_free_heap_size() - it is like stackwatermark just to report minimum free heap ever, not task stack,
2) there is few options which will report different functional free heap, ie "esp_get_free_internal_heap_size" will report current free heap in internal RAM only
3) personally i prefer to use this piece of code:

Code: Select all

heap_caps_get_info(&info, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); // internal RAM, memory capable to store data or to create new task
info.total_free_bytes;   // total currently free in all non-continues blocks
info.minimum_free_bytes;  // minimum free ever
info.largest_free_block;   // largest continues block to allocate big array

Re: how to calculate free ram?

Posted: Sat Oct 09, 2021 8:50 pm
by Craige Hales
Thanks! The largest free block seems more useful to report than what I'm currently doing.