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

Maximum size of array in memory

I am using NRF52840 and SDK1600. Currently, my program uses 50kB RAM memory according to Segger Embedded Studio. NRF52840 has 250kB  RAM memory, so there is plenty of space left. 

Right now, I have a task to allocate 64 kB memory for temporary data storage during data acquisition. Since I have plenty of RAM available, I want to use RAM instead of flash for the temporary data storage. 

I am wondering what is the largest size of array that I can allocate without any potential problems? I understand that we want to avoid dynamic memory allocation such as calloc/free.

For example, can I use the following size of array in application without any problem?

uint16_t data_array[32000]; 

Segger studio compiles the above line of code without issues. 

I did a quick search and did not find the answer. Thanks in advance!

  • Hi 

    There is no practical limitation on how large buffers you can have, as long as they are static or global. 

    Automatic variables (non static variables declared inside a function) will be put on the stack, and as such they are limited by the stack size, while dynamically allocated memory is limited by the heap size. 

    Best regards
    Torbjørn 

  • Thanks Torbjorn. So a safe bet is to declare the array as follows:

    static uint16_t data_array[32000]; 

    As a side note, I read that the default stack size is 8192 bytes and heap size is 8192 bytes too. Therefore, in a function maximum variable array size is limited to 8k bytes, and the dynamic memory is restricted to maximum 8192 bytes too? 

  • Hi 

    davidz said:

    So a safe bet is to declare the array as follows:

    static uint16_t data_array[32000]; 

    Yes, if you make the variable static you can safely declare it anywhere (even inside a function) and it will be OK.  

    davidz said:
    As a side note, I read that the default stack size is 8192 bytes and heap size is 8192 bytes too. Therefore, in a function maximum variable array size is limited to 8k bytes, and the dynamic memory is restricted to maximum 8192 bytes too? 

    Having a variable that is exactly the size of the stack won't work, since you will be pushing other variables to the stack also. 

    If you do some testing to verify how much stack your application is using already you can probably estimate how much larger the stack needs to be if you want to declare a large variable on the stack. 

    Generally though it is not recommended to put large variables on the stack. 

    Dynamic memory is also restricted to 8192 bytes total, that is correct, but usually there is some overhead for every allocated buffer, so you won't be able to use the full 8192 bytes for data. If you need to dynamically allocate 8192 bytes you would need to make the heap a bit larger than this. 

    Best regards
    Torbjørn

  • Thank you very much. All my questions are answered clearly. 

  • I'm happy to help. The best of luck with your project Slight smile

Related