Problem about memory management in FreeRTOS

huka wrote on Friday, March 30, 2018:

Hi ,I am confused about the problem that when i delete the task,the memory i get with the pvPortMalloc( ) founction in this task will be automatically freed ? if not, how can i free the memory when i delete the task?
I used the heap_4.c for memory managemnet


richard_damon wrote on Friday, March 30, 2018:

When you delete a task, the memory allocated to create the TCB and stack will be free’d, but any other memory the task itself allocated via calls to pvPortMalloc will NOT be free, and in fact couldn’t, because FreeRTOS doesn’t track those sorts of allocations and it would be quite possible for one task to allocate memory and then give it to another, so freeing it would actually be wrong.

Tasks are NOT like processes on a big OS which have all the resources they used get free’d when they terminate. You, as the programmer need to keep track of your resource usage and release those resourse that you aquired.

huka wrote on Friday, March 30, 2018:

I got it, thanks a lot!

huka wrote on Sunday, April 01, 2018:

Hi, I need an array size of 200 in the task,if I just define it as a local variable like char array[200 ] instead to apply for the momory by pvPortMalloc(200). When I delete the task, the momory of the array will be free automatically?And some other local variables as int,char variables int the task will be free?

rtel wrote on Sunday, April 01, 2018:

If the variables are declared as local variables with function scope they will be allocated on the task’s stack, and the task’s stack memory is automatically freed when the task is deleted.

huka wrote on Sunday, April 01, 2018:

I understand. Thank you!