cancel
Showing results for 
Search instead for 
Did you mean: 

Determine heap free space on STM32

tinkerer42
Associate III

I'm using an STM32L4 with STM32CubeIDE, bare metal, and using C++ along with Cube's C libraries. The compiler is the GCC/G++ supplied with STM32CubeIDE.

I have some dynamic memory usage in STL containers etc. I'm aware of the recommendation not to use dynamic memory on bare-metal embedded systems, but it is generally used only during initialization, minimizing the complications.

I would like to know how much of the heap I'm using, and found no way to do that. I saw discussions of __heapstats but it doesn't seem to be defined in this toolchain (?).

It doesn't have to be a programmatic way, if there is some internal data structure of newlib or whatever that I can watch while debugging that will suffice.

p.s.

I know the term "free heap space" is not very well defined, because of block sizes and fragmentation, but I suspect that in my case it's going to be one big free block.

any ideas?

16 REPLIES 16

The default GCC toolchain comes with newlib as std. C library. I think the contained heap allocator supports mallinfo() to retrieve some heap status information.

Semer CHERNI
ST Employee

Hello @tinkerer42​ 

The most generic way to detect stack/heap collisions during run-time would be to by inserting a "known pattern" inside the RAM-memory.

If this memory is completely overwritten then you will know that the heap and stack has collided...

I think there should be some previous ticket where we advised how customer how he can re-write the Reset_Handler to fill the unused RAM with a known pattern.

Have a look at this extension to startup code:

LoopFillZerobss:

ldr r3, = _ebss

cmp r2, r3

bcc FillZerobss

/* ============================ */

/* Fill space between Heap and stack with magic pattern */

ldr r2, =_ebss

mov r4, sp

movs r3, #0xa5a5a5a5 /* The magic pattern */

b LoopFillPattern

FillPattern:

str r3, [r2]

adds r2, r2, #4

LoopFillPattern:

cmp r2, r4

bcc FillPattern

/* Call static constructors */

bl __libc_init_array

Kind regards,

Semer.

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.

tinkerer42
Associate III

that's probably the most robust solution, I'll try that.

Thanks!

You specifically asked for the variable names and structures your build process was using. Perhaps that..

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

The original question, in its entirety, was originally Determine heap free space on STM32

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I don't recall stack collision, or stack size, being the question. This would be a good way of determining the low water mark of the stack, and high water mark on the heap within that same space. Not very enlightening on the current state of heap or stack, or determine used/unused sections of the heap. The heaps here are pretty simplistic linked lists.

What if the stack and heap are in entirely different memory regions? What if _sbrk() worked properly? Having it assume it's in the same memory region, and that the current base of the stack is a safe bounding condition, really not inspiring confidence at this end.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

oh, ok - I just hoped to know in advance what I'm looking for (from someone with experience with the specific compiler)