Problem understanding memory utilisation

Hi All

Im new to the Arduino and having a problem understanding how memory is utilised so started playing with Arduino Test Suite and finally ended up with this code.

#include "WProgram.h"
#include "HardwareSerial.h"
#include <ArduinoTestSuite.h>

int startMemoryUsage;
boolean test=false;

void setup()
{
  startMemoryUsage = ATS_GetFreeMemory();
  ATS_begin("Arduino", "Skeleton Test");
  ATS_ReportMemoryUsage(startMemoryUsage);
}

void loop()
{
if (test) 
   {
   ATS_ReportMemoryUsage(startMemoryUsage);
   }
delay(5000);
}

The ATS_ReportMemoryUsage command in setup() reports …FAIL as shown below. The reason being somehow is the call to ATS_ReportMemoryUsage call in loop(). If I comment this out the report is …ok. Im at a loss to understand why a call in loop() which hasnt been executed yet could have any effect on memory in the setup() stage. You will also note that due to the fact that the variable test is never set true the second call is never called anyway. Whats happening under the hood here that I dont understand.

info.MANUFACTURER    = Arduino
info.CPU-NAME        = ATmega328P
info.GCC-Version     = 4.3.2
info.AVR-LibC-Ver    = 1.6.4
info.Compiled-date   = Nov  8 2011
info.Test-Suite-Name = Skeleton Test
info.Free-memory     = 1445 bytes
Memory Usage (lost 54 bytes)                                ... FAIL

Thanks awfully
Kenneth

Try putting startMemoryUsage = ATS_GetFreeMemory(); AFTER ATS_begin("Arduino", "Skeleton Test");

I suspect that ATS_begin() is allocating 54 bytes of memory and therefore causes the appearance of a memory leak.

I briefly played with the test suite and couldn’t get it to work for me either. I went to using MemoryFree instead. Get it off the playground and simply include it:

#include <MemoryFree.h>

and then sprinkle Serial.println(freeMemory()); around where you want to know what is going on. I found this to fit my code better and was easy to setup and use on a frequent basis. The source for this is easy to understand and change if you want to look at something specific as well.

johnwasser:
Try putting startMemoryUsage = ATS_GetFreeMemory(); AFTER ATS_begin(“Arduino”, “Skeleton Test”);

Tried that, it gives the same result. Comment the ATS_GetFreeMemory in the loop() out you get …ok. Leave it in you get …FAIL

draythomp:
I briefly played with the test suite and couldn’t get it to work for me either. I went to using MemoryFree instead. Get it off the playground and simply include it:

Outstanding… Thanks for that I will give it a try.

In the meantime a bit of expansion on how I got to this point. My origonal purpose is to have the Arduino serve up a webpage and its a big one. So I modified the webserver demo adding a procedure to serve the page as follow.

void SendWebPage(Client client)
{
 client.println("HTTP/1.1 200 OK");
 client.println("Content-Type: text/html");
 client.println();
 client.println("<html>");
 client.println("<head>");
 .............And lots lots more 
}

The problem is once the procedure exceeds a certain length all sorts of weird stuff happens which looks like a ram overrun. Whats more startling is that even if I comment the call to the procedure out, it still happens, until I comment out parts of the procedure itself. Anyway im starting to suspect that the Arduino loads the entire sketch into ram and i guess that im hoping that the 99% of you guys who probably already know that will just confirm that for me and the next bunch of noobs who read this :slight_smile:

No, it doesn't load the sketch into RAM, just all the strings.

I hit the exact same problem. That's why I hunted down the memory free stuff. I was running myself out of memory with a big web page. You have about 16x Flash memory to Sram memory and it's really easy to run out. Each of those strings you're trying to send takes up twice the space you think it does. It's stored in Flash (so it will be there after a power down) and copied to Sram just before setup() is called. So, you're half right, only globals and strings are handled this way. So, a big array can wipe you out also (I did that too).

What you need to do is look at the PROGMEM stuff. There's examples all over this board, on the web and in the playground on techniques to overcome this problem. Then, put the memory free stuff I mentioned before in to make sure you don't get too close to running out again as you work on your project. Remember to keep at least a couple of hundred bytes around for the stack and temporary variables YMMV on this, depending on your techniques.