Go Down

Topic: Memory limits (Read 166 times) previous topic - next topic

gilperon

Hi,

I have very small arduino code but I use too many Serial.print("Type your name")... Serial.print("Type your age")... After increasing this serial messages my program became completely unstable. I believe it's lack of memory cause my atmega328p only has 2048KB of SRAM.

My question is this: I have no more than 10 variables of type int in my code. How am I running without memory? Are those strings in the Serial.print saved in the SRAM when arduino starts?

westfw

Quote
Are those strings in the Serial.print saved in the SRAM when arduino starts?
Yes.  It's essentially an artifact of C that since the strings are "data", they have to appear in the RAM of cpus that have separate code/data address spaces.
You can get around this by using the F construct (an arduino special feature.)  http://arduino.cc/en/Serial/Print
Quote
You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
  • Serial.print(F("Hello World"))


gilperon

Thank you, F macro is amazing! But what about this:



char x = "ABCDEFGHIJ";

Serial.println(x);

Serial.println(x);

char y = x;



Is this ABCDEFGHIJ string using only 20 bytes (eery char is 2 bytes -> 10x2 = 20)? Or will it use 80 bytes of SRAM?

LarryD

char x = "ABCDEFGHIJ";
You need an array here.
The way you have it in your schematic isn't the same as how you have it wired up!

westfw

chars are only one byte in (most?) C.
Code: [Select]

char x[] = "ABCDEFGHIJ";

Will use 11 bytes of RAM.  (don't forget the terminating null.)

gilperon

Thank, you are indeed right, it should be an array. But will it only use 11 bytes of RAM or every variable that has that value will also consume 11 bytes of RAM?

AWOL

Quote
atmega328p only has 2048KB of SRAM.
Nope.
2048 bytes.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

igendel

Thank, you are indeed right, it should be an array. But will it only use 11 bytes of RAM or every variable that has that value will also consume 11 bytes of RAM?
After compilation ("Verify"), Arduino IDE 1.5.8 shows you how much RAM is taken by global variables, so you can try and see for yourself. Char arrays do not have common references - each occupies its own space, no matter what.
My Adventures in the Making:
http://www.idogendel.com/en

westfw

Quote
Code: [Select]
char x[] = "ABCDEFGHIJ";
Serial.println(x);
Serial.println(x);


Char arrays do not have common references - each occupies its own space, no matter what.
However, "x" in the println does NOT create another copy of the string in RAM, just a "pointer" to it, so the above program segment would still only use 11 bytes of RAM.

The "occupies its own space" thing comes into play with code like:
Code: [Select]
char x[] = "ABCDEFGHIJ";
char y[] = "ABCDEFGHIJ";

which gives you 22 bytes of storage used...


gilperon

Hi @westfw thank you for helping but I think you made a mistake. In the arduino docs it says that the compiler tries to identify the same string that appears in the code and if it appears more than once it will be saved only once in the Flash and SRAM, and will use a pointer to wherever that same string is being used.

Note that if you use the macro F() it's not true, if you have the same string over and over the compiler will not "optimize" it.

Anyway, I made a lot of research today and finaly understood it. Thank you so much

AWOL

#10
Dec 05, 2014, 09:43 pm Last Edit: Dec 05, 2014, 09:43 pm by AWOL
Quote
In the arduino docs it says that the compiler tries to identify the same string that appears in the code and if it appears more than once it will be saved only once in the Flash and SRAM, and will use a pointer to wherever that same string is being used.
Code: [Select]
char a [] = "0123456";
char b [] = "0123456";

...
.... and then later
a[1] = 'B';


Now, I can see that the same initial string can exist in flash, but that's simply not going to apply to RAM.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nick Gammon

Hi @westfw thank you for helping but I think you made a mistake. In the arduino docs it says that the compiler tries to identify the same string that appears in the code and if it appears more than once it will be saved only once in the Flash and SRAM, and will use a pointer to wherever that same string is being used.
That only applies in this sort of case:

Code: [Select]

Serial.println ("foo");
...
Serial.println ("foo");
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy