Page 1 of 1

How can I terminate FreeRTOS tasks and get the memory back?

Posted: Tue Mar 28, 2023 1:38 am
by vritzka
Hello,

I am running 2 tasks on my ESP32-S3

- Graphics Task
- Sensor Task

But when I start WIFI and add an Over-the-air update Task, the system runs out of memory.

I get strange errors like:

Code: Select all

I (13590) esp_https_ota: Starting OTA...
I (13590) esp_https_ota: Writing to partition subtype 16 at offset 0x210000
W (13650) wifi:m f null

E (60430) transport_base: poll_read select error 113, errno = Software caused connection abort, fd = 54
E (60430) HTTP_CLIENT: transport_read: error - 57347 | ERROR
E (60430) esp_https_ota: data read -1, errno 0
E (60440) ota.c: Firmware upgrade failed

When I change my firmware and NOT run the sensor task, the OTA update works fine.


I tried deleting the sensor task before I start the OTA update with vTaskDelete( x_Sensor_Reader_Task_Handle );, but it doesn't help. (I also add vTaskDelay(pdMS_TO_TICKS(1000) to run the idle task and release memory).

Could you please give me some advice on how I could make enough memory available to run my update task?

Here are some more memory infos.


Total sizes:
Used static IRAM: 93282 bytes ( 268958 remain, 25.8% used)
.text size: 92255 bytes
.vectors size: 1027 bytes
Used stat D/IRAM: 38352 bytes ( 307504 remain, 11.1% used)
.data size: 18560 bytes
.bss size: 19792 bytes
Used Flash size : 1867417 bytes
.text : 868835 bytes
.rodata : 998326 bytes
Total image size: 1979259 bytes (.bin may be padded larger)


Smallest app partition is 0x200000 bytes. 0x1eb80 bytes (6%) free.


My partitions.csv:

# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x4000
otadata, data, ota, 0xd000, 0x2000
phy_init, data, phy, 0xf000, 0x1000
factory, app, factory, 0x10000, 2M
ota_0, app, ota_0, , 2M
ota_1, app, ota_1, , 2M
nvs_key, data, nvs_keys, , 0x1000



Thank you

Re: How can I terminate FreeRTOS tasks and get the memory back?

Posted: Tue Mar 28, 2023 7:59 am
by vanBassum
If Im not mistaken, deleting task don't free the memory. You should check the FreeRTOS docs to verify this, since Im not entirely sure. But if that is the case, your only option is to create tasks using statically allocated memory and share the memory between the tasks. Ensuring they never run at the same time of course.

Re: How can I terminate FreeRTOS tasks and get the memory back?

Posted: Tue Mar 28, 2023 11:54 pm
by ESP_Sprite
To clarify, deleting a task does free some memory, but only the memory allocated for the task control block and the stack; if you allocated any memory within the task code, it stays allocated.

Re: How can I terminate FreeRTOS tasks and get the memory back?

Posted: Wed Mar 29, 2023 6:18 am
by vanBassum
That seems reasonable, since I got interested I took a look at this page: https://www.freertos.org/a00126.html

Something on that page stood out to me:
NOTE: The idle task is responsible for freeing the RTOS kernel allocated memory from tasks that have been deleted. It is therefore important that the idle task is not starved of microcontroller processing time if your application makes any calls to vTaskDelete (). Memory allocated by the task code is not automatically freed, and should be freed before the task is deleted.

Re: How can I terminate FreeRTOS tasks and get the memory back?

Posted: Sat Apr 01, 2023 11:37 pm
by MicroController
A task should not delete any task except itself.
If a task is no longer needed, it should always perform its own cleanup, including releasing any locks, memory and other resources it holds, and then "delete" itself.
This means that vTaskDelete should always be called as vTaskDelete( NULL ).