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

Memory allocation limitations on the nRF52840?

Hi,

I am using the nRF52840, which has 256KB of RAM, (i.e. loads and load of RAM.)

I would like to allocate a buffer of 45KB.

I have tried using malloc (from the stdlib.h, in SEGGER Embedded Studio for ARM 3.40) to allocate some memory buffers, but the function returns NULL if I try and allocate anything bigger than 8188 bytes ~=8KB  (which is tiny compared to the 262144 available in the chip)

Is there an in-built limitation of the malloc function, and if so, how do I override it?

I have also tried experimenting with the mem_manager library and the nrf_malloc function, but this seems to be limited to blocks of 3444 bytes. I'm not sure why this weird block-size is the default, but it is far too small to be called an "xxlarge" block, on a chip with 256KB of RAM.

How do I work around these issues and allocate a single large contiguous RAM buffer, given that my chip has lots of memory?

Parents
  • Hi,

    You can set the heap (and stack) size under "Runtime Memory Area" in the project settings:

    The longer story is that what you select above is put in the __HEAPSIZE__ and __STACKSIZE__ variable, which are used in the section-placement file. In the SDK examples this is called flash_placement.xml and is in the same directory as the project files for all SDK examples. Relevant snippet:

        <ProgramSection alignment="4" size="__HEAPSIZE__" load="No" name=".heap" />
        <ProgramSection alignment="8" size="__STACKSIZE__" load="No" place_from_segment_end="Yes" name=".stack"  address_symbol="__StackLimit" 

     

  • Thank you.
    Yes, changing that Heap Size value to 128KB now allows me to allocate sensibly sized arrays.

    I don't understand why this Heap Size value is set in this obscure settings dialog as an arbitrary and tiny default value, instead of being automatically sized to be whatever the free memory for the chip is.

    Also, can you shed any light on the nrf_malloc function? Is there any advantage to using that over malloc?
    Why are the nrf_malloc buffer size increments based on fractions of 3444 instead of some power-of-2 number of bytes. Does this perhaps relate to some common application for this function?


     

Reply
  • Thank you.
    Yes, changing that Heap Size value to 128KB now allows me to allocate sensibly sized arrays.

    I don't understand why this Heap Size value is set in this obscure settings dialog as an arbitrary and tiny default value, instead of being automatically sized to be whatever the free memory for the chip is.

    Also, can you shed any light on the nrf_malloc function? Is there any advantage to using that over malloc?
    Why are the nrf_malloc buffer size increments based on fractions of 3444 instead of some power-of-2 number of bytes. Does this perhaps relate to some common application for this function?


     

Children
  • Hi,

    I agree that it would make sense to let any unused memory be part of the heap. You can achieve it by editing the section placement file (flash_placement.xml in the SDK projects). Replace the existing line that sets the heap (name=".heap") with the following line:

        <ProgramSection alignment="4" size="((__RAM_segment_end__ - __STACKSIZE__) - __heap_load_start__)" load="No" name=".heap" />

    The nrf_malloc function is part of the Memory Manager library, which is more complex than the standard malloc function. The fixed size blocks are used to reduce memory fragmentation. The buffer sizes are configurable in the projects sdk_config.h (e.g. MEMORY_MANAGER_XXSMALL_BLOCK_SIZE). Where do you see that the buffer size increments are based on fractions of 3444?

  • Hi,

    Thanks.

    The XXLARGE block is defined in the sdk_config.h file as 3444 bytes, which is a strange and arbitrary number. I had assumed the smaller blocks would be fractions of that.

Related