how to calculate free ram?

alphago1000
Posts: 1
Joined: Mon Oct 04, 2021 4:49 pm

how to calculate free ram?

Postby alphago1000 » Mon Oct 04, 2021 4:58 pm

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

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: how to calculate free ram?

Postby ESP_Dazz » Sat Oct 09, 2021 8:53 am

Does heap_caps_get_free_size() fulfil your requirements?

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: how to calculate free ram?

Postby Craige Hales » Sat Oct 09, 2021 4:20 pm

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
Craige

chegewara
Posts: 2364
Joined: Wed Jun 14, 2017 9:00 pm

Re: how to calculate free ram?

Postby chegewara » Sat Oct 09, 2021 5:08 pm

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

Craige Hales
Posts: 94
Joined: Tue Sep 07, 2021 12:07 pm

Re: how to calculate free ram?

Postby Craige Hales » Sat Oct 09, 2021 8:50 pm

Thanks! The largest free block seems more useful to report than what I'm currently doing.
Craige

Who is online

Users browsing this forum: No registered users and 93 guests