Memory leakage: esp_partition_write
Posted: Thu Jan 19, 2023 3:05 am
Hi,
I'm sending data over MQTT to my server and I found a memory leakage after saving data in flash memory. I don't know if I'm missing something when writting data in flash.
Here is what I get in console:
WiFi crashes (Beacon timeout) when heap memory goes to low. So I started checking every time I'm sending data and realized that right before sending data I save it as backup in flash and if I comment that piece of code, minimum heap memory never goes down.
Here is how I save data in flash memory:
Any piece of advise is welcome.
Best regards,
P
I'm sending data over MQTT to my server and I found a memory leakage after saving data in flash memory. I don't know if I'm missing something when writting data in flash.
Here is what I get in console:
Code: Select all
I (19:23:47.354) mqtt: mqtt_publish on topic /telemetry, payload {"ts":1673033027000,"values":{"anguloX":0,"anguloY":0,"anguloZ":0,"VelAnguloX":0.0079987,"VelAnguloY":0.00451767,"VelAnguloZ":0.00185478}}
I (19:23:47.410) mqtt: MQTT_EVENT_PUBLISHED, msg_id=40740
I (19:23:47.621) main:
Free heap: 38448
Minimum heap: 26456
...
(This happens multiple times)
...
I (19:24:02.069) mqtt: mqtt_publish on topic /telemetry, payload {"ts":1673033041000,"values":{"anguloX":0,"anguloY":0,"anguloZ":0,"VelAnguloX":0.023266,"VelAnguloY":-0.0164746,"VelAnguloZ":-0.00577879}}
I (19:24:02.123) mqtt: MQTT_EVENT_PUBLISHED, msg_id=39102
I (19:24:02.671) main:
Free heap: 30812
Minimum heap: 26456
I (19:24:03.132) mqtt: mqtt_publish on topic /telemetry, payload {"ts":1673033043000,"values":{"anguloX":0,"anguloY":0,"anguloZ":0,"VelAnguloX":0.0289911,"VelAnguloY":0.000700951,"VelAnguloZ":0.0247555}}
I (19:24:03.193) mqtt: MQTT_EVENT_PUBLISHED, msg_id=60714
I (19:24:03.671) main:
Free heap: 30272
Minimum heap: 25956
...
(Minimum heap keeps going down until..)
...
I (19:24:56.166) mqtt: mqtt_publish on topic /telemetry, payload {"ts":1673033096000,"values":{"anguloX":0,"anguloY":0,"anguloZ":0,"VelAnguloX":0.000365198,"VelAnguloY":0.0102429,"VelAnguloZ":0.00948834}}
I (19:24:56.761) main:
Free heap: 1956
Minimum heap: 712
I (19:24:57.762) main:
Free heap: 2064
Minimum heap: 712
I (19:24:58.762) main:
Free heap: 2064
Minimum heap: 712
I (19:24:59.762) main:
Free heap: 2064
Minimum heap: 712
I (19:25:00.761) main:
Free heap: 2064
Minimum heap: 712
I (19:25:01.762) main:
Free heap: 2064
Minimum heap: 712
I (88169) wifi:bcn_timout,ap_probe_send_start
I (19:25:02.119) wifi: wifi_tcp_event_handler
I (19:25:02.120) wifi: BASE: WIFI_EVENT | ID: 21
W (19:25:02.120) wifi: WIFI_EVENT_STA_BEACON_TIMEOUT
I (19:25:02.126) wifi: wifi_update_status: status 1
I (88179) wifi:state: run -> init (0)
I (88189) wifi:pm stop, total sleep time: 41487982 us / 83411741 us
W (88199) wifi:<ba-del>idx
W (88199) wifi:<ba-del>idx
I (88199) wifi:new:<5,0>, old:<5,1>, ap:<5,1>, sta:<5,1>, prof:1
I (19:25:02.151) wifi: ESP_OK [0]: succeed
I (19:25:02.762) main:
Free heap: 2780
Minimum heap: 712
Here is how I save data in flash memory:
Code: Select all
void write_value_to_flash(uint32_t index, value_t* to_save_value)
{
uint32_t address_to_write = index * sizeof(value_t);
// semaphore take to avoid writting when other task is writting
if (address_to_write % SPI_FLASH_SEC_SIZE == 0)
esp_partition_erase_range(_log_partition, address_to_write, SPI_FLASH_SEC_SIZE);
esp_partition_write(_log_partition, address_to_write, to_save_value, sizeof(value_t)); //if this line is commented, no memory leakage happens
// semaphore give so writting is back available to other tasks
}
Best regards,
P