Memory leakage in IDF function

ukrsms
Posts: 13
Joined: Tue Aug 20, 2019 11:30 am

Memory leakage in IDF function

Postby ukrsms » Wed Jan 29, 2020 2:26 pm

During debugging of my own code I found two functions that don't deallocate all the memory they used.
Memory leaked only after the first used of the function
here are the fubctions:
memory leak 20 Bytes

Code: Select all

ledc_fade_func_install(0);	
ledc_fade_func_uninstall();
and

memory leak 84 Bytes:

Code: Select all

esp_adc_cal_characteristics_t characters;
u8 calType=esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_12, 1100, &characters);
u32 voltage;
esp_adc_cal_get_voltage((adc_channel_t)1, &characters, &voltage);
Please, tell me whether I did something wrong, or it is a bug in IDF, or it is a normal operation of the functions?
Tested with IDF ver.4.1

ukrsms
Posts: 13
Joined: Tue Aug 20, 2019 11:30 am

Re: Memory leakage in IDF function

Postby ukrsms » Fri Feb 14, 2020 4:59 pm

after recent test I found one more function stealing memory:
esp_efuse_mac_get_default()
it steals 84 bytes after first calling

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

Re: Memory leakage in IDF function

Postby ESP_Sprite » Sat Feb 15, 2020 10:04 am

Thanks for the report, however, you're probably better off creating a Github issue for this, as it will automatically get tracked into the system.

ESP_Angus
Posts: 2110
Joined: Sun May 08, 2016 4:11 am

Re: Memory leakage in IDF function

Postby ESP_Angus » Tue Feb 18, 2020 4:34 am

ukrsms wrote:
Wed Jan 29, 2020 2:26 pm
Memory leaked only after the first used of the function
These may not be memory leaks as some types of data structures in IDF are allocated on first access ("lazy initialization") but are not deinitialized.

If you repeat the same init/deinit sequence multiple times, or call the same function multiple times, and the memory use goes up each time then this is definitely a memory leak.

If you need some tools to better analyze where allocations are happening, the heap tracing feature can help:
https://docs.espressif.com/projects/esp ... ap-tracing

Angus

ukrsms
Posts: 13
Joined: Tue Aug 20, 2019 11:30 am

Re: Memory leakage in IDF function

Postby ukrsms » Thu Mar 05, 2020 10:09 am

Thank you, Angus

that are "lazy initialization", actually. However, it seems it is wrong behavior of the functions reported as IDF provided us with deinitialization function, however, not all memory is freed.

ukrsms
Posts: 13
Joined: Tue Aug 20, 2019 11:30 am

Re: Memory leakage in IDF function

Postby ukrsms » Fri Mar 06, 2020 4:49 pm

One more "Lazy initialization" found. After executing the following sequence for the first time 204 bytes disappears

Code: Select all

	TEST_ASSERT(nvs_flash_init_partition(PARTITION_LABEL_FACTORYDATA)==0);
	nvs_handle_t handle;
	TEST_ASSERT(nvs_open_from_partition(PARTITION_LABEL_FACTORYDATA, STORE_NAMESPACE_FACTORY, (nvs_open_mode_t)NVS_READWRITE, &handle)==0);
	nvs_close(handle);
	nvs_fl
ash_deinit_partition(PARTITION_LABEL_FACTORYDATA);

ESP_Angus
Posts: 2110
Joined: Sun May 08, 2016 4:11 am

Re: Memory leakage in IDF function

Postby ESP_Angus » Wed Mar 11, 2020 12:31 am

ukrsms wrote:
Thu Mar 05, 2020 10:09 am
that are "lazy initialization", actually. However, it seems it is wrong behavior of the functions reported as IDF provided us with deinitialization function, however, not all memory is freed.
There is sometimes global intialization associated with the first time some functions is called. ledc_fade_func_install() registers an ISR internally, which involves some global initialization for the first time an ISR is allocated. Uninstalling and re-installing the same function (or a different ISR in another driver) shouldn't allocate this memory a second time.

Are these one-off small allocations causing a problem in your app?

ukrsms
Posts: 13
Joined: Tue Aug 20, 2019 11:30 am

Re: Memory leakage in IDF function

Postby ukrsms » Thu Apr 16, 2020 7:28 am

No, it doesn't cause problems for the program. It just made me do spend time in order to decide whether I have memory leak in my program or not.
It would be easier if that information was documented (about "lazy initialization")

Who is online

Users browsing this forum: ESP_Roland, maximekli and 14 guests