1

I am developing for the Arduino Due which has 96k SRAM and 512k flash memory for code. If I have a program that will compile to, say, 50k, when I run the code, how much sram will I use? will I use 50k immediately, or only the memory used by the functions I call? Is there a way to measure this memory usage before I upload the sketch to the arduino?

user3243135
  • 800
  • 1
  • 7
  • 28

3 Answers3

2

You can run

arm-none-eabi-size bin.elf

Where:

  • bin.elf is the generated binary (look it up in the compile log)
  • arm-none-eabi-size is a tool included with Arduino for arm which lets you know the memory distribution of your binary. This program can be found inside the Arduino directory. In my mac, this is /Applications/Arduino.app/Contents/Resources/Java/hardware/tools/g++_arm_none_eabi/bin

This command will output:

text       data     bss     dec     hex filename
9648          0    1188   10836    2a54 /var/folders/jz/ylfb9j0s76xb57xrkb605djm0000gn/T/build2004175178561973401.tmp/sketch_oct24a.cpp.elf

data + bss is RAM, text is program memory.

Very important: This doesn't account for dynamic memory (created in stack), this is only RAM memory for static and global variables. There are other techniques to check the RAM usage dynamically, like this one, but it will depend on the linker capabilities of the compiler suite you are using.

Community
  • 1
  • 1
jlhonora
  • 10,179
  • 10
  • 46
  • 70
  • 1
    heap is a wrong word here right? because both data and bss segments are used to store the global variables. heap memory is the area from which dynamic allocations take place(e.g. in C malloc), which is again solely dependent upon runtime behaviour of the program. – sraok Oct 24 '13 at 23:05
0

Your whole program is loaded into arduino, so atleast 50K flash memory will be used. Then on running the code, you will allocate some variables, some on stack, some global which will take some memory too but on SRAM.

I am not sure if there is a way to exactly measure the memory required but you can get a rough estimation based on the number and types of variables being allocated in the code. Remember, the global variables will take the space during the entire time the code is running on arduino, the local variables( the ones that are declared within a pair of {..}) remain in the memory till the '}' brace also known as the scope of the variables. Also remember, the compiled 50K code which you are mentioning is just the code portion, it does not include your variables, not even the global ones. The code is store in Flash memory and the variables are stored in the SRAM. The variables start taking memory only during runtime.

Also I curious to know how you are calculating that your code uses 50K memory?

sraok
  • 595
  • 6
  • 23
  • I got the "50k" number from the matrixssl homepage, which proudly claims their "footprint" is less than 50kb. I was wondering how that translates to actual SRAM usage, before I tried compiling it for the arduino. – user3243135 Oct 24 '13 at 23:56
0

Here is a little library to output the avalaible RAM memory. I used it a lot when my program was crashing with no bug in the code. It turned out that I was running out of RAM. So it's very handy!

Avalaible Memory Library

Hope it helps! :)

ladislas
  • 3,037
  • 19
  • 27