# Stack and Heap size?

**URL:** <https://forums.freertos.org/t/stack-and-heap-size/679>\
**Category:** Kernel\
**Created:** [September 8, 2011, 4:20pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679 "2011-09-08T16:20:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2011, 4:20pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679/1 "2011-09-08T16:20:35Z")

</div>

**[gavin00](https://sourceforge.net/u/gavin00/)** wrote on [Thursday, September 08, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#ea52):

I don’t know how to assign the stack and heap size in FreeRTOSConfig.h  
It seems I have to give a very huge heap size, but I don’t dynamically allocate any memory. Is the OS copy my code to HEAP? my code is already in RAM, why do the copy?  
The Stack size should be task control block size, am I right? if I am right , then it should be just a couple of KB.

What’s the relation of FreeRTOS HEAP&Stack comparing to the C heap&stack?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2011, 4:26pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679/2 "2011-09-08T16:26:29Z")

</div>

**[gavin00](https://sourceforge.net/u/gavin00/)** wrote on [Thursday, September 08, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#a02b):

Sorry, my previous post is wrong.  
This is the correct one:  
I don’t know how to assign the TASK size and heap size in FreeRTOSConfig.h  
It seems I have to give a very huge heap size, but I don’t dynamically allocate any memory. Is the OS copy my code to HEAP? my code is already in RAM, why do the copy?  
The TASK size should be task control block size, am I right? if I am right , then it should be just a couple of KB.

What’s the relation of FreeRTOS HEAP&Stack comparing to the C heap&stack?

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [September 8, 2011, 4:29pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679/3 "2011-09-08T16:29:29Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, September 08, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#31d4):

[http://www.freertos.org/a00111.html](http://www.freertos.org/a00111.html)

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [September 8, 2011, 7:13pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679/4 "2011-09-08T19:13:28Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Thursday, September 08, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#2d1c):

Your PROGRAMS stack and heap size are normally set with compiler/linker options. Since you didn’t say which one you are using, it is hard to help you more on where to set those. If your program is using heap1.c or heap2.c, then size of the memory block that FreeRTOS uses for its heap is configured with a parameter in FreeRTOSConfig.h, this will NOT affect how much memory is allocated to the C library heap. Anything the linker report calls “heap” will be the C library heap, not the FreeRTOS heap which will look just like a chunk of statically declared memory usage (which is how it is implemented)

Your programs starting stack, is normally only used for your startup code, and in some ports, for the interrupt stack. It is NOT used by any of the tasks.

Task stacks are allocated from the FreeRTOS heap, which if you use heap3.c is the C library heap, otherwise it is the separate chunk of memory described previously.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 15, 2012, 9:56pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679/5 "2012-08-15T21:56:29Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Wednesday, August 15, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#1b2b):

Hi richard,

i have here a question:  
in code composer is the stack of the linker used for the startup code, i.e the main function? and is it used for the interrupts?  
i am using code composer on msp430.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 16, 2012, 7:54am UTC](https://forums.freertos.org/t/stack-and-heap-size/679/6 "2012-08-16T07:54:49Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Thursday, August 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#110c):

> in code composer is the stack of the linker used for the startup code

yes.

> and is it used for the interrupts

No. The port does not use interrupt nesting so there is little to gain from that.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 16, 2012, 10:18pm UTC](https://forums.freertos.org/t/stack-and-heap-size/679/7 "2012-08-16T22:18:22Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Thursday, August 16, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#256c):

thank you very much.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [August 29, 2012, 3:22am UTC](https://forums.freertos.org/t/stack-and-heap-size/679/8 "2012-08-29T03:22:04Z")

</div>

**[modiallen](https://sourceforge.net/u/modiallen/)** wrote on [Wednesday, August 29, 2012](https://sourceforge.net/p/freertos/discussion/382005/thread/1eef806d/#7bec):

Hi again,  
i read in the dokumentation in another RTOS, that the msp430 dont have a stack for the ISR.  
so the stack of every task will be used if the task will be interrupted with an ISR.  
because of this, they recommended the user to define the stack of every task a little more bigger than the estimated space for every task.  
i think this recommendation will be usefull for freertos-users too.

Best Regards.
