Free Memory - how much is my sketch using on Dash


#1

Hi, Everyone,

I’m trying to find out how much RAM I’m using with a fairly large sketch. I’ve tried some of the standard Arduino tools like MemoryFree and the freeRam routine (from Ada). Neither will compile for the Dash (“error compiling for board Dash”).

Thanks in advance for your thoughts!

Michael


#2

Unfortunately I think those are AVR based tools, which won’t work with the ARM-based Dash. I haven’t been able to get the Arduino IDE to print the RAM usage, but there is a way to get it manually.
You need to find the Arduino tools folder and the temporary output folder for your sketch. If you turn on Verbose output during compilation in the Arduino Preferences dialog, it’ll print out these paths as part of the build commands.

On Arduino 1.8.0 for OSX (it may work with other versions too), near the end of the verbose build output, I see:

"/Users/erik/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gcc"  "-L/var/folders/8f/qcm9kx3j2g1fjmft9w1_l_3c0000gn/T/arduino_build_32933"```

Those are the 2 directories you need. Instead of `arm-none-eabi-gcc` you need to run `arm-none-eabi-size` and in the build directory, there is a `.elf` file that you need to use as input. So on my machine I run:
`/Users/erik/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-size /var/folders/8f/qcm9kx3j2g1fjmft9w1_l_3c0000gn/T/arduino_build_32933/sketch_name.ino.elf`

Substitute the paths from your machine/build and you should get an output like this:
`   text	   data	    bss	    dec	    hex	filename`
`  42836	    580	  31748	  75164	  1259c	/var/folders/8f/qcm9kx3j2g1fjmft9w1_l_3c0000gn/T/arduino_build_32933/sketch_name.ino.elf`

Text is your flash usage, Data and BSS are your RAM usage.

#3

Thanks for the speedy response, Erik!

Your walkthrough works perfectly!

Now the inevitable follow-up question and Seeking of Wisdom:

Sketch 1 is flaky. Corrupted Strings. Of course. It’s ALWAYS the Strings. Yes, I know. Don’t use Strings.

It yields:

 text	  data	    bss	    dec	    hex	filename
53220	   632	  31748	  85600	  14e60	/var/folders/1j/86sy7s_56p34rbdvxdlz3wz00000gn/T/arduino_build_857489/Flaky_Sketch.ino.elf

Sketch 2, which I “stripped down” from Sketch 1until it ran well (sophisticated methodology, yes? LOL) shows:

   text	   data	    bss	    dec	    hex	filename
  49324	    608	  31748	  81680	  13f10	/var/folders/1j/86sy7s_56p34rbdvxdlz3wz00000gn/T/arduino_build_19547/Good_Sketch.ino.elf

Even a relatively small sketch to check cellular connections gives:

text	   data	    bss	    dec	    hex	filename
  39444	    572	  31236	  71252	  11654	/var/folders/1j/86sy7s_56p34rbdvxdlz3wz00000gn/T/arduino_build_712951/Connection_Status_Test.ino.elf

Soooo… I noticed that both of my sketches show the program size I’m familiar with on uploading, but the data and BSS (including from your example) seem to pretty much be the same, with the total of data + bss JUST UNDER 32,768 (the magic number, yes?). Thoughts? Hints? How much trouble am I in?


#4

Strings are mostly on the Heap, which is pre-allocated, so that’s why your number isn’t moving much.

The problem with the Heap and String is if you exceed the Heap size in total allocation, or if a long-running sketch with lots of string creating/deletion fragments the Heap such that further allocations aren’t possible.

That is why in a lot of safety-critical embedded systems dynamic memory allocation (i.e. the Heap) is not allowed. But it’s a lot easier to use Strings. So that’s the trade-off.

And anticipating your follow-up, the Heap is 16K and the Stack is 4K. You can change those numbers, but you’d need to modify the linker file (dash.ld). Most of the rest is pre-allocated buffers for the built-in Arduino components (like Serial, etc). The Dash has 128K of RAM, so you’re nowhere close to running out.