how to allocate heap size

sudhakarrtos wrote on Wednesday, September 09, 2015:

heap size depends on stack_size and semophore,queue and so on., suppose if i am not use any queue or mutex
means can i allocate same size what i used for STACK_SIZE. its enough? or i want to give more

heinbali01 wrote on Wednesday, September 09, 2015:

Hi Sudhakar,

All FreeRTOS demo project have a define configMINIMAL_STACK_SIZE. That should give enough space on the stack for a minimal task like blinking a LED (and some more).

Just start playing with it and see how much stack and heap is being used.

For instance, uxTaskGetSystemState() gives detailed information about each task, including a field called usStackHighWaterMark: the minimum amount of stack available.
While playing: make sure that the tasks have more than enough stack space. A shortage of stack often results in a crash which makes debugging extremely difficult.

Managing the HEAP is a bit easier. The modules heap_4.c and heap_5.c have a function xPortGetMinimumEverFreeHeapSize() which returns the lowest amount of HEAP available during a run.

Personally, when I have enough RAM, I’d rather assign more RAM to the stack than allocate items statically or on the heap.

Using stack has two advantages:

  • the allocation and deallocation are very fast, a decrement of SP (Stack Pointer register)
  • the same RAM space can be reused for many different purposes

Regards.

tlafleur wrote on Wednesday, September 09, 2015:

Give it a lot more that you think you need and use FreeRTOS tool to see how much you use during the development. Start big and cut down as you need ram for other things.

~~ _/) _/) _/) ``` _/) ~~

On Sep 9, 2015, at 3:54 PM, sudhakar sudhakarrtos@users.sf.net wrote:

heap size depends on stack_size and semophore,queue and so on., suppose if i am not use any queue or mutex
means can i allocate same size what i used for STACK_SIZE. its enough? or i want to give more

how to allocate heap size

Sent from sourceforge.net because you indicated interest in https://sourceforge.net/p/freertos/discussion/382005/

To unsubscribe from further messages, please visit https://sourceforge.net/auth/subscriptions/

sudhakarrtos wrote on Thursday, September 10, 2015:

hi,
i am using atmega644 so RAM size was less.only 4k available. so i want to know how to allocate heap size. if i increase heap size my code RAM size was increasing. suppose stack_size 128 mean can i allocate same size to heap also. is it k? if not ok means how much size i want to increase for 128 byte stack_size. inside my task am not using any queue or semaphore.

thanks and regards,
sudhakar

heinbali01 wrote on Thursday, September 10, 2015:

Hi Sudhakar,

What compiler are you using?
Which module for memory allocation are you planning to use (portable\MemMang\heap_x.c) ?

You might want to read this recent post:

https://sourceforge.net/p/freertos/discussion/382005/thread/14fb7b28/?limit=25#b406

It is about the various heap allocation functions.

A stack size of N means that N registers can be pushed. In an ATmega644, a register is 16-bits wide, so a stack_size of 128 will cost 256 bytes.

Before you start the scheduler (vTaskStartScheduler()), you already have a stack. Have a look at where it is located and how big it is: you may want to reuse that SRAM space later on, once the scheduler is running.

Regards.

sudhakarrtos wrote on Thursday, September 10, 2015:

hi,
i am using heap_1.c module and AVR studio 5.2.
am confused to allocate heap please give some suggestion
regards,
sudhakar

heinbali01 wrote on Thursday, September 10, 2015:

You will have to configure configTOTAL_HEAP_SIZE in your FreeRTOSConfig.h.

How large should it be? Only you can tell. And if you don’t know how much heap you’ll need, just start playing with it.

Normally the heap gets “what is left over”. Your program will use the 4 KB of SRAM and 64 KB of flash in the following ways:

  section             size       addr
  .data               0xd4   0x800100 <= SRAM
  .text             0x7afa        0x0 <= flash
  .bss               0x520   0x8001d4 <= SRAM

Here you find a clear explanation about the above sections:

http://mcuoneclipse.com/2013/04/14/text-data-and-bss-code-and-data-size-explained

And further: the stack grows downwards, it is often declared at the top of the RAM memory. The heap is located right after the .data and .bss sections, just below the stack.
Once you have started the scheduler, the stacks will (normally) be located in the heap and the original stack space will be vacant.

So how big will you make configTOTAL_HEAP_SIZE ?

Sum the size of .data, .bss and your boot stack (128 x 2 = 256 bytes). Subtract this amount from the available 4KB and that is how much heap you can declare.

Regards.

Correction: the location of the default heap is between the .data / .bss segments and the stack.
When you use a FreeRTOS heap module (like heap_1.c), your heap space will be located in the .bss section.

1 Like