cancel
Showing results for 
Search instead for 
Did you mean: 

How to reduce the heap size, or remove completely the heap?

adaniel
Associate III
 
1 ACCEPTED SOLUTION

Accepted Solutions
KnarfB
Principal III

Heap and Stack are at the top end of the RAM (unless you're using an RTOS).

from generated sysmem.c:

* ############################################################################
 * #  .data  #  .bss  #       newlib heap       #          MSP stack          #
 * #         #        #                         # Reserved by _Min_Stack_Size #
 * ############################################################################
 * ^-- RAM start      ^-- _end                             _estack, RAM end --^

So, the heap does not waste space except you're using it at runtime.

The .ld linker file reserves some space for the heap at link time using the symbol _Min_Heap_Size. Linking fails if less than _Min_Heap_Size + _Min_Stack_Size are free in RAM. You can adjust these linker values in the .ioc file's Project Manager tab. But, if linking succeeds you can gain nothing from changing these values.

Keep in mind that some C runtime functions may use the heap internally, so you might set a breakpoint or do some debug print in sysmem.c function _sbrk to find out whats happening in your system at runtime or study the sources.

View solution in original post

3 REPLIES 3
KnarfB
Principal III

Heap and Stack are at the top end of the RAM (unless you're using an RTOS).

from generated sysmem.c:

* ############################################################################
 * #  .data  #  .bss  #       newlib heap       #          MSP stack          #
 * #         #        #                         # Reserved by _Min_Stack_Size #
 * ############################################################################
 * ^-- RAM start      ^-- _end                             _estack, RAM end --^

So, the heap does not waste space except you're using it at runtime.

The .ld linker file reserves some space for the heap at link time using the symbol _Min_Heap_Size. Linking fails if less than _Min_Heap_Size + _Min_Stack_Size are free in RAM. You can adjust these linker values in the .ioc file's Project Manager tab. But, if linking succeeds you can gain nothing from changing these values.

Keep in mind that some C runtime functions may use the heap internally, so you might set a breakpoint or do some debug print in sysmem.c function _sbrk to find out whats happening in your system at runtime or study the sources.

adaniel
Associate III

Thanks, just setting _Min_Heap_Size = 0, solved my problem

Peter BENSCH
ST Employee

You can e.g. reduce the heap size while creating the project in STM32CubeIDE and editing the IOC file:

Project Manager > Project > Linker Settings

This will modify the .ld file and set the entry _Min_Heap_Size accordingly.

Regards

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.