📬❄️📦Adafruit Holiday Shipping Deadlines 2019: Please place all UPS 3 Day orders by 11am ET Friday December 13 📬❄️📦
0

available memory
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

available memory

by gr33nhorn on Wed Oct 22, 2008 9:04 am

i did some test regarding the available memory. here's my code below

Code: Select all | TOGGLE FULL SIZE
#include <math.h>
void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps
 
  int b;

   
   b=availableMemory();
   Serial.println(b,DEC);
}

void loop()
{

}
// this function will return the number of bytes currently free in RAM

int availableMemory()
{
  int size = 1024;
  byte *buf;

  while ((buf = (byte *) malloc(--size)) == NULL)
    ;

  free(buf);

  return size;
}


output is
Code: Select all | TOGGLE FULL SIZE
 828


i was wondering what was used to wipe off so many bytes since there is no floating point calculation?


i have another question:let say i have a function (say it takes up 200bytes)with full of floating point calculations in it.And i need to call it quite frequently to update some other functions.will the function use up the 1k sram eventually as it was being called ?
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: available memory

by mtbf0 on Wed Oct 22, 2008 10:17 am

i was wondering what was used to wipe off so many bytes since there is no floating point calculation?


serial input buffer which is allocated when you call Serial.begin whether you do any serial input or not is 128 bytes. a couple of 16 bit pointers into the buffer are allocated as well. that's another 4 bytes.

the timer uses 8 bytes.

there's stuff allocated to keep track of which pins you're using for input and which you're using for output.

and by the time you get into your loop routine there are at least two call frames on the stack, main() and loop().

of course there's more, but that's a lot of it. and i'm not sure the space on the stack is figured into the availableMemory calculation.

you can reduce the size of the serial read buffer, by editing the file arduino-0010/hardware/cores/wiring_serial.c and reducing the number at the end if the line that begins "#define RX_BUFFER_SIZE." it appears that i have already reduced my buffer to 16 bytes. you will notice that i am using arduino version 10. if you are using a different version use the appropriate directory name.

i have another question:let say i have a function (say it takes up 200bytes)with full of floating point calculations in it.And i need to call it quite frequently to update some other functions.will the function use up the 1k sram eventually as it was being called ?


only if the function is called recursively, i.e. if it calls itself either directly or indirectly. recursion is generally a lousy idea in a memory constrained environment.
"i want to lead a dissipate existence, play scratchy records and enjoy my decline" - iggy pop, i need more
User avatar
mtbf0
 
Posts: 1645
Joined: Sat Nov 10, 2007 12:59 am
Location: oakland ca

Re: available memory

by westfw on Wed Oct 22, 2008 11:35 am

That's a clever way to figure out how much RAM is available! You should post it over in the Arduino forums as well.
I'm surprised there is that much, given the 128bytes for serial buffers. Does the AVR "malloc()" let the heap and the stack run into each other?
westfw
 
Posts: 1585
Joined: Fri Apr 27, 2007 1:01 pm
Location: SF Bay area

Re: available memory

by gr33nhorn on Fri Oct 24, 2008 12:33 pm

thanks for the replies ..i will update this thread again
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Re: available memory

by gr33nhorn on Sat Oct 25, 2008 5:47 am

i had successfully send 4 bytes of command packet to control the WCK servo and it's suppose to response by transmitting back 2 bytes back to arduino.

1)is it the right way to clear the 2 bytes by calling Serial.read() for 2 times?please refers to my code PosSend() function.

2)why i couldn't see the rx led blings or it's too fast for a naked eye?

3)how to optimize my code as the dynamic ram only left 50bytes?

here's my code
Code: Select all | TOGGLE FULL SIZE
#include <math.h>
#define HEADER 0xff

void setup()
{  char SpeedLevel,ServoID,Position;
   SpeedLevel=0x01; // hex value to represent the speedlevel
   ServoID=0x00; // hex value to represent the servoID
   Position=0x80; // any value from 0-255(byte/hex)that represents position
   Serial.begin(115200);           // set up baudrate at 9600 bps
   char x,y;
   x=PosSend( ServoID,  SpeedLevel, Position);
   y=availableMemory();
   Serial.println(y,DEC);
}

void loop()
{
}

char PosSend(char ServoID, char SpeedLevel, char Position)
{
  //char Current;
  SendOperCommand((SpeedLevel<<5)|ServoID, Position);
  Serial.read();//read first byte
  Serial.read();//read second byte
  //Current =Serial.read();
  //return Current;
}

void SendOperCommand(char Data1, char Data2)
{   
  char CheckSum;
  CheckSum = (Data1^Data2)&0x7f;
  Serial.print(HEADER,BYTE);
  Serial.print(Data1,BYTE);
  Serial.print(Data2,BYTE);
  Serial.print(CheckSum,BYTE);
}

int availableMemory()
{
  int size = 1024;
  byte *buf;
  while ((buf = (byte *) malloc(--size)) == NULL);
  free(buf);
  return size;
}
 
gr33nhorn
 
Posts: 34
Joined: Sat Sep 20, 2008 4:36 pm

Please be positive and constructive with your questions and comments.