heap and stack size

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

heap and stack size

2,504 Views
PO220
Contributor IV

Hi

 

I try to understand HEAP_SIZE  and STACK_SIZE in .ld file

 

In this file KDS find the RAM size  : 0x8000 for my KL17Z256VLH4 ; why should we specify KDS the size of stack and head ?

 

>> Why compiler and linker can not work with just the RAM size ? (to optimize its use)

 

>> Is there a risk in defining stack and heap to the maximum RAM size ? if so, what is the mistake ?

 

/*for my KL17Z256*/

HEAP_SIZE  = DEFINED(__heap_size__)  ? __heap_size__  : 0x8000; /* defaut  0x0400*/

STACK_SIZE = DEFINED(__stack_size__) ? __stack_size__ : 0x8000; /* defaut 0x0400*/

 

/* 0x8000 = RAM size */

 

I use Freertos (with en other heap specification : configTOTAL_HEAP_SIZE)

 

Thank you in advance for your explanations

Labels (1)
0 Kudos
Reply
3 Replies

1,263 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

it's necessary to allocate max heap and stack size in a project beforehand.

because:

stack is needed to allocate function parameters, local variables,etc

heap is needed if using mallo() to apply a memory.

global variables and static variables are allocate in RAM (not stack space, neither heap).

normally a project (including called system files) need to use stack and heap anyway, so it's necessary to allocate them in linker file.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply

1,263 Views
PO220
Contributor IV

Hi, thank for your answer

I understand a little better since Friday and you end make me understand (I hope !)

I'm right ? :

Heap :

1>>When using FreeRTOS (I don't use malloc apart from freertos) the heap necessary is that required by FreeRTOS only. (I can use xPortGetFreeHeapSize() to optimise this)

As freertos use malloc to allocate task stack, heap need to be large for a freertos application

2>> If not using malloc or FreeRTOS ; heap can be set to zero (no dynamic allocation)

Stack :

3>>With freertos i need just allocate stack for some variables that are outside FreeRTOS. The not stack or heap  space must be allocate for some global or static variables that are outside FreeRTOS.

4>>Without FreeRTOs. I need to allocate the largest Stack, with just enough RAM (not stack space, neither heap) for global or static variables.

5>> In any case I need to leave space in RAM for startup vector table copy.

I'm right ?

thanks

0 Kudos
Reply

1,263 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

regarding to your comments,

for Heap:

no matter using or not using rtos, I suggest you NOT set heap size, even you think your code doesn't use malloc, it potentially called some low level system library functions that could use heap.

for stack:

when use freeRTOS, the freeRTOS related codes are also part of project. the global variables and static variables  in freeRTOS are allocated in RAM; the local variables in freeRTOS are allocated in stack.

5>>

yes.


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply