This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Circumstances that trigger Heap usage

Hello,

I am developing in NRF51 with gazelle communication. I specify in project setting (in IRAM) to use full RAM of my nRF51, and use MicroLib. My last working application does not use dynamic memory allocation at all, and I verify in produced memory map that arm_startup_nrf51 uses (default size) 2048 stack, no heap at all.

Then, I modify the code, still without dynamic mem allocation, and suddenly I got "no space in execution regions" error, even though the variable and method does not change much. Since it doesn't produce memory map to be checked, I set the IRAM to exceed full RAM of my nRF51 to get successful compilation and memory map.

In the memory map, everything is almost same, except arm_startup_nrf51 uses 4096 now, with heap. I notice this additional usage is heap as it changes the size accordingly every time I change Heap_size in arm_startup_nrf51.s to different size. (for example, I change Heap_size into 1024, now arm_startup_nrf51 use 3072 in memory map).

Is there any circumstance other than dynamic memory allocation that will trigger this heap usage?

*I use default arm compiler of Keil in c99 mode. I understand that I can set Heap_size to 0. However, what confuses me is, why is the heap still set to 2048 (or used at all), even though I do not change the code to use dynamic memory allocation. What I understand is, heap is automatically set to 0 in compile time if I do not use dynamic mem allocation or use MicroLib, as what has been applied in my code before modification. Or do I understand it wrong?

Thank you!

  • I have had the same problem when I was using a variable sized array inside a function.

    int array[variable];
    

    In old C this way of declaring arrays is not even allowed, but in C99 it is allowed. Nevertheless I expect the compiler chooses how to implement it, uses malloc() or place it on the stack (guessing now, but it would be nice if somebody could confirm this). When I replaced in my code the variable with a fixed length which was large enough, space for the heap wasn't automaticly reserved. With automaticly reserved I mean that when you are not using any form of dynamic memory allocation, the arm_startup_nrf51 file won't reserve space for the heap after compiling, which you could verify in the linker file.

    it would be nice if somebody could give some more information about how the variable sized array is handled by the compiler.

  • "I expect the compiler chooses how to implement it,

    you expect wrong then. A local variable like that would always be placed on the stack.

  • Turns out, this variable-length array is also the one that triggers the heap usage in my code. @RK : I thought the same until I encounter this problem. The odd thing is, I also used this variable-length array in my previous code that does not trigger heap usage at all. So, I am not sure if this variable-length is really the main source of the trigger, maybe the trigger is a product of some combination (with this array). *I use default arm compiler of Keil in c99 mode

Related