# FreeRTOS HEAP and STACK Memory issue

**URL:** <https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163>\
**Category:** Kernel\
**Created:** [August 4, 2021, 5:19am UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163 "2021-08-04T05:19:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://forums.freertos.org/u/Manikandan)\
**Post date:** [August 4, 2021, 5:19am UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/1 "2021-08-04T05:19:55Z")

</div>

Hai,  
I am using One FreeRTOS task for reading ADC value. Inside the task i am using one buffer for storing the ADC value. Here my doubt is, which memory is allocated for that buffer. FreeRTOS stack or system stack. System heap is allocated for FreeRTOS. In this FreeRTOS heap, it’s having individual stack for task. My FreeRTOS task buffer is using FreeRTOS stack or system stack?

Thanks  
Manikandan D

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [August 4, 2021, 5:31am UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/2 "2021-08-04T05:31:47Z")

</div>

Do you dynamically allocate memory for the buffer? If so then where do you allocate it from (what do you call to allocate it)? If you don’t dynamically allocate the buffer then the compiler and linker determine where the buffer is located as they would for any C code. Note if this is in a task and you declare the buffer as a stack variable then each task has its own stack.

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://forums.freertos.org/u/Manikandan)\
**Post date:** [August 4, 2021, 5:46am UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/3 "2021-08-04T05:46:40Z")

</div>

xTaskCreate(vTaskAnalog, “vTaskAnalog”, 2048, 0, (tskIDLE\_PRIORITY + 1), &vTaskAnalogHandle)

what is the use of this 2048.  
I am using this function to create a task. I declare that buffer in global(int buffer[256]). But I am using that buffer inside the task.  
RAM size= 320k  
FreeRTOS heap size = 32k  
What is the use of “task STACK”. my buffer is taking FreeRTOS task STACK or system STACK.

Thanks  
Manikandan D

---

<div class="post-metadata">

**Author:** ![hs2](https://avatars.discourse-cdn.com/v4/letter/h/a4c791/32.png) [@hs2](https://forums.freertos.org/u/hs2)\
**Post date:** [August 4, 2021, 7:52am UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/4 "2021-08-04T07:52:02Z")

</div>

As the name suggests the task stack is the stack reserved for the task being created.  
When using `xTaskCreate` this stack memory is allocated internally from FreeRTOS heap as [documented](https://www.freertos.org/a00125.html) in the API docs.  
Local/stack variables used in the task code are put on this stack. You can of course also access/use global variables in a task.  
For instance Cortex-M MCUs support a main and a so called process stack. The latter is used by FreeRTOS to manage the task stacks (on context switch) and the main stack is used by ISRs.

---

<div class="post-metadata">

**Author:** ![Manikandan](https://avatars.discourse-cdn.com/v4/letter/m/3da27b/32.png) [@Manikandan](https://forums.freertos.org/u/Manikandan)\
**Post date:** [August 4, 2021, 9:36am UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/5 "2021-08-04T09:36:49Z")

</div>

Thanks for your answer @hs2

---

<div class="post-metadata">

**Author:** ![Xavier](https://avatars.discourse-cdn.com/v4/letter/x/2bfe46/32.png) [@Xavier](https://forums.freertos.org/u/Xavier)\
**Post date:** [August 10, 2021, 4:44pm UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/6 "2021-08-10T16:44:36Z")

</div>

If your buffer was declared as automatic variable to the a function, then the buffer is allocated in your task stack, which in turns was allocated either in the heap (if you used xTaskCreate()) or in the system memory (if you used xTaskCreateStatic()).

However, if the buffer was declared with dynamic memory (through a call to malloc()), then it is allocated in the system’s heap (as long as you’re using your malloc’s compiler implementation).

---

<div class="post-metadata">

**Author:** ![jackyjoy123](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@jackyjoy123](https://forums.freertos.org/u/jackyjoy123)\
**Post date:** [August 28, 2021, 12:24pm UTC](https://forums.freertos.org/t/freertos-heap-and-stack-memory-issue/13163/7 "2021-08-28T12:24:33Z")

</div>

thanks for the awesome information.
