Setting the size of a Task's allocated memory

how do you set the size of a task’s allocated memory?

For example if you have a task that only blinks and LED, surely you would need only very few bytes contrary to other much more complex tasks?

What dictates the size? How do you choose it?

Are there tools and/or techniques to estimate the exact amount or at least a SAFE value close to the minimum that allows the task to always work?

Thank you

Are you talking about the size of the stack allocated to the task? If so, then the demo for each port contains a definition of configMINIMAL_STACK_SIZE, which is only used by the kernel to dimension the size of the stack allocated to the idle task on the assumption the idle task is not doing anything. That can be used as a reference for the least amount of stack you should ever allocate to a task. If you add code into the idle task using the idle hook facility then you may need to increase it.

Over and above that there are a few things you can do:

  1. Calculate the stack required by the task - which just as per any other program is dependent on the function call nesting depth, how many variables you place on the stack, etc. You also need a few bytes extra as the context of the task is also saved to its stack when it is not running. There are some tools that will do this for you - including some features in GCC - but honestly I normally don’t bother doing it this way and prefer a more pragmatic trial and error approach as per the following…

  2. Make sure you have configCHECK_FOR_STACK_OVERFLOW set to 2 so you know if the stack is too small. If so, you can increase its size until the stack overflow hook is no longer hit.

  3. Set the stack to be larger than you think it needs to be. Then check how much stack it is actually using and adjust as necessary. You can check the amount of stack being used using a kernel aware IDE plug-in, or just using the FreeRTOS API to check. You can also check the stacks of all tasks at once.

Thank you Richard, very useful!