Measuring free ram on Samd21

Hello Everyone,

I discovered the other day a way to get the amount of free ram on a atmega series chip. Adafruit has a nice little writ up, and sure enough it works really well. Saved me alot of trouble diagnosing why my already tested code was breaking when I added some irrelevant code ;-).

I was wondering if anyone knew of an equivalent way of doing this for the samd21? I ported freertos to the samd21 for a laser tag project, and it is only a matter of time before I try and fill up all its ram hehehe.

I tried the same code on adafruit, and of course __heap_start and __brkval do not exist when compiling as a samd21 type board.

Anybody have any ideas on finding the samd21 equivalent variables (if they exist), or a different/better way to measure the ram currently in use?

Talk to you soon!
starwarslegokid

I ported freertos

Doesn't freertos have its own version of malloc()?
It probably even already has a function that returns the amount of free memory. (um... xPortGetFreeHeapSize() ?)

The FreeMemory library works with the SAMD core: GitHub - mpflaga/Arduino-MemoryFree: basic functions and example to use show used RAM and use less of it.

sandeepmistry:
The FreeMemory library works with the SAMD core: GitHub - mpflaga/Arduino-MemoryFree: basic functions and example to use show used RAM and use less of it.

Hello sandeepmistry, thank you for posting this, thats just what I was looking for. I used the example program and added some print statements and it looks like it works great. Samd21 starting with just over 30,000 (im assuming bytes) and after afew constant string prints, the ram trickles down at about two bytes per non flashed char printed.

[/quote]

westfw:
Doesn't freertos have its own version of malloc()?
It probably even already has a function that returns the amount of free memory. (um... xPortGetFreeHeapSize() ?)

Hello westfw,

Ahh yes you are correct, freertos does have xPortGetFreeHeapSize(), I totally forgot that! When I ported it over, I ended up setting it up using heap_3 because it was the only one that I could get working at the time. It uses wrapped malloc calls, and throws a error that can be handled if a object creation fails. Problem is heap_3 does not support the xPortGetFreeHeapSize() function since it doesn't know how to predict free memory, just knows when malloc fails.

You bring up a good point, I should probably go back and revisit that code. Now that I know how many bytes are there at startup (~30,000), I can probably figure our how to setup heap_1 which allocates a giant heap array, and can easily check how much I actually used.

Thank you both for your answers! I will use both your advice B-)
starwarslegokid

sandeepmistry:
The FreeMemory library works with the SAMD core: GitHub - mpflaga/Arduino-MemoryFree: basic functions and example to use show used RAM and use less of it.

Hi guys,

I was looking for a way to measure free RAM of my MKR1400 (SAM D21) too, so I tried this MemoryFree.h library. A played a bit around with however I am not sure if this really works.

When I use the following sketch:

#include <MemoryFree.h>;

void setup() {
  Serial.begin(9600);

  Serial.println(F("Not RAM consuming string because of F Macro")); 


  Serial.println("A RAM consuming string");
  //Serial.println("Yet another a RAM consuming string.........."); // <-- uncommented in second run
  //Serial.println("Yet another a RAM consuming string.........."); // <-- uncommented in second run
  //...


  Serial.print(F("Free RAM = "));
  Serial.println(freeMemory(), DEC);  // print how much RAM is available.
}

void loop() {
  
}

I get this output...

Not RAM consuming string because of F Macro
A RAM consuming string
Free RAM = 29907

So the FreeMemory-Lib tells me I have 29907bytes RAM left... OK! Now I expected when I add these not-F'ed print strings to the code, the displayed number will significantly decrease by the number of addtionally added chars to print. However the output is

Not RAM consuming string because of F Macro
A RAM consuming string
Yet another a RAM consuming string..........
Yet another a RAM consuming string..........
Free RAM = 29899

Which is only 8byte less than in Run No. 1?

Could somebody explain me this or can reporduce?

Thank you very much,

Oliver

Btw:

On my Arduino Uno the FreeMemeory lib seems to totally fail. It shows a free RAM of 22xx bytes which higher then the available RAM stated in the Tech specs... And here as well the Number of free RAM does not decrease with additonal print-Statements.

I think on arm, all quoted strings end up in flash....