# RAM Usage / Management

**URL:** <https://forums.freertos.org/t/ram-usage-management/45>\
**Category:** Kernel\
**Created:** [May 17, 2007, 8:46pm UTC](https://forums.freertos.org/t/ram-usage-management/45 "2007-05-17T20:46:50Z")\
**Posts on this page:** 2\
**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:** [May 17, 2007, 8:46pm UTC](https://forums.freertos.org/t/ram-usage-management/45/1 "2007-05-17T20:46:50Z")

</div>

**[scottnortman](https://sourceforge.net/u/scottnortman/)** wrote on [Thursday, May 17, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/04dfffb4/#ea6f):

Hi,

How does one know how much stack space to allocate to each task?&nbsp; Also, what about the configTOTAL\_HEAP\_SIZE option?&nbsp; How is the heap size related to the memory allocation scheme?

Thanks,  
Scott Nortman

---

<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:** [May 18, 2007, 6:12am UTC](https://forums.freertos.org/t/ram-usage-management/45/2 "2007-05-18T06:12:09Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, May 18, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/04dfffb4/#ea6f/d115):

The amount of stack required by a task is completely dependent on the implementation of the task.&nbsp; The following items will increase the amount of stack the task requires:

1) High function call nesting depth.

Each time a function is called data is saved to the stack.

2) Local, non static variables.&nbsp; Each of which is allocated stack space.&nbsp; This is why you should not declare large structures or arrays as local variables (unless you make them static).

When a task is created its stack is set to 0xa5, so you can always check the high water mark by viewing the memory in a debugger.&nbsp; The location of the currently used stack can be obtained from the stack pointer of the processor you are using.&nbsp; Alternatively, using the debugger you can inspect the pxCurrentTCB pointer, which is a pointer to the TCB.&nbsp; The pxCurrentTCB-\>pxStack member points to the start of the stack used by the task.&nbsp; The pxCurrentTCB-\>pxTopOfStack points to the top of the stack as it was the last time the task entered the Running state.

Also take a look at the function usTaskCheckFreeStackSpace() in tasks.c, this can be used to return the task high water mark.&nbsp; The lower the number returned the closer the task has come to overflowing its stack.

configTOTAL\_HEAP\_SIZE defines the size of the block of memory available to the memory allocator used by either the heap\_1 or heap\_2 schemes.&nbsp; Memory is grabbed from this pool each time a task or queue is created.&nbsp; Take a look here:&nbsp; [http://www.freertos.org/a00111.html](http://www.freertos.org/a00111.html)&nbsp; if you have not already seen it.

Regards.
