# Squeeze all memory into FreeRtos heap

**URL:** <https://forums.freertos.org/t/squeeze-all-memory-into-freertos-heap/6260>\
**Category:** Kernel\
**Created:** [October 22, 2016, 1:37am UTC](https://forums.freertos.org/t/squeeze-all-memory-into-freertos-heap/6260 "2016-10-22T01:37:27Z")\
**Posts on this page:** 4\
**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:** [October 22, 2016, 1:37am UTC](https://forums.freertos.org/t/squeeze-all-memory-into-freertos-heap/6260/1 "2016-10-22T01:37:27Z")

</div>

**[joehinkle](https://sourceforge.net/u/joehinkle/)** wrote on [Saturday, October 22, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3371ccab/#4074):

I’m using heap4 which requires me to specify the amount of ram to allocate to the FreeRtos heap.

I would like to allocate all unallocated ram to the heap without playing with the configuration size.

Is there a way to use the linker to figure out what amount of ram is unallocated and assign it to my heap allocation size?

Thanks

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 22, 2016, 1:53am UTC](https://forums.freertos.org/t/squeeze-all-memory-into-freertos-heap/6260/2 "2016-10-22T01:53:30Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Saturday, October 22, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3371ccab/#54f6):

There are as many answers to that question as there are compilers.

If you are using GCC then check out the gnu linker documentation. That describes how to set linker variables, then access those variables from C code. If the linker fills your RAM from low to high, then set a linker variable to the end of the section filled by the linker, and another to the top of the RAM, then you have the limits available to the heap. Here is an example that is more complicated than you need, it has lots of linker variables defined. Look for

` __data_end__ = . ;`

[https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/CORTEX\_MPU\_Simulator\_Keil\_GCC/GCC\_Specific/sections.ld](https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/CORTEX_MPU_Simulator_Keil_GCC/GCC_Specific/sections.ld)

The linker variables are used in this C file. Search “extern uint32\_t” to see how to bring them in  
[https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/CORTEX\_MPU\_Simulator\_Keil\_GCC/main.c](https://sourceforge.net/p/freertos/code/HEAD/tree/trunk/FreeRTOS/Demo/CORTEX_MPU_Simulator_Keil_GCC/main.c)

Then best to use heap\_5.c because even with configAPPLICATION\_ALLOCATED\_HEAP ([http://www.freertos.org/a00110.html#configAPPLICATION\_ALLOCATED\_HEAP](http://www.freertos.org/a00110.html#configAPPLICATION_ALLOCATED_HEAP)) GCC makes it hard to place the ucHeap array at the address you want. heap\_5.c makes that easy.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 22, 2016, 9:18pm UTC](https://forums.freertos.org/t/squeeze-all-memory-into-freertos-heap/6260/3 "2016-10-22T21:18:55Z")

</div>

**[joehinkle](https://sourceforge.net/u/joehinkle/)** wrote on [Saturday, October 22, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3371ccab/#d122):

Thanks.

I defined my heap this way using heap4 and GCC compiler.

byte ucHeap[configTOTAL\_HEAP\_SIZE] **attribute** ((section (".heap")));

Would I gain anything by using heap5 if this is working fine?

---

<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:** [October 22, 2016, 9:46pm UTC](https://forums.freertos.org/t/squeeze-all-memory-into-freertos-heap/6260/4 "2016-10-22T21:46:56Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Saturday, October 22, 2016](https://sourceforge.net/p/freertos/discussion/382005/thread/3371ccab/#d122/f353):

The problem with this is the definition of configTOTAL\_HEAP\_SIZE has to  
be a fixed constant at compile time. If you use heap\_5 then you can set  
the heap region size using a linker variable, so it automatically  
adjusts as the memory map changes.

Make sure you understand your linker file. Some cruder linker files  
will set the stack at the top and the heap at the bottom of any RAM that  
is left over, but not dimension either of them, so they will eventually  
crash in the middle if too much RAM is used.
