0

Conventional wisdom has it that global and static data is stored in the bottom of RAM along with other stuff. Somewhere above that is the heap, then free memory and at the top of RAM, is the stack. See the code below before reading on.

When I compile this with the Arduino IDE (1.8.10) on a MEGA2560 I get the following statistics:

Sketch uses 2750 bytes (1%) of program storage space. Maximum is 253952 bytes. Global variables use 198 bytes (2%) of dynamic memory, leaving 7994 bytes for local variables. Maximum is 8192 bytes.

If I change ARRAY_SIZE from 1 to 7001, I get exactly the same numbers. I expected that dynamic memory should increase by 7000. When I do the same comparison with AtmelStudio V7, dynamic memory does indeed increase by 7000.

One more piece of information along these lines. If I do a malloc of 7000 which is pretty close to free memory, one would expect that the malloc should be successful when ARRAY_SIZE equals one and would fail when the ARRAY_SIZE equals 7001. I was dismayed to find that the malloc was successful with both the small and the large array sizes. With AtmelStudio this does not happen.

I suspect respective compiler/linker options somewhere could explain the difference (AtmelStudio - project properties and Arduino IDE - platform.txt perhaps?).

I also suspect that the Arduino IDE dynamically allocates global variables in FlashMemory.

I am not a newbie, but I am not a guru - comments anyone?

Thanks Michèle

#define ARRAY_SIZE 7001
uint8_t globalArray[ARRAY_SIZE]{1};
void setup() {
 Serial.begin(115200);
 for (int i = 0; i < ARRAY_SIZE; i++) globalArray[i] = 1;
  Serial.print(F("Just initialized globalArray, size = "));Serial.println(ARRAY_SIZE);
  uint8_t* testPointer = (uint8_t*) malloc(7000);
  Serial.print(F("Allocation of 7000 bytes "));
  if ( testPointer != (uint8_t*) 0) {
  Serial.print(F("SUCCESSFUL"));
   } else {
   Serial.print(F("NOT SUCCESSFUL"));
   }
} // setup
void loop() {} // loop
double-beep
  • 5,031
  • 17
  • 33
  • 41
Michele
  • 1
  • 1
  • 2

1 Answers1

0

I ran some more tests to figure out why AtmelStudio and the Arduino IDE are supplying vastly different RAM usage values after declaring an array. The response from juraj (thank you) was that the compiler optimized unused code away. This answer was true however I had included an array initialization loop to make sure that the compiler would include the array in the code. It turns out that AtmelStudio and the Arduino IDE have different criteria as to code what it means "code being used". The outcome is that globalArray, in the initialization line, for (int i = 0; i < ARRAY_SIZE; i++) globalArray[i] = 1; is considered by AtmelStudio as being used and by the Arduino IDE as not being used. The Arduino IDE requires that globalArray appear on the left of an assignment statement to consider it as being used thus the need for the "a+=globalArray[i];" line. The exact same code below reports:

a+=globalArray[i]; not used Atmel Studio: Data Memory Usage as being 7211 bytes Arduino IDE: Global variables use 198 bytes

a+=globalArray[i]; used Atmel Studio: Data Memory Usage as being 7211 bytes Arduino IDE: Global variables use 7199 bytes

Q.E.D. Interesting how the two IDEs do not quite mean the same thing with "being used".

Thanks - My first time on this forum got my question answered rather quickly.

Michèle

#define ARRAY_SIZE 7001
uint8_t globalArray[ARRAY_SIZE];
void setup() {
  Serial.begin(115200);
  for (int i = 0; i < ARRAY_SIZE; i++) globalArray[i] = 1;
  Serial.print(F("Just initialized globalArray, size = "));
  Serial.println(ARRAY_SIZE);
  //   uint16_t a {0};
  //   for (int i = 0; i < ARRAY_SIZE; i++) a+=globalArray[i];
  //   Serial.print(F("Value of a = ")); Serial.println(a);
  uint8_t* testPointer = (uint8_t*) malloc(7000);
  Serial.print(F("Allocation of 7000 bytes "));
  if ( testPointer != (uint8_t*) 0) Serial.print(F("SUCCESSFUL"));
  else Serial.print(F("NOT SUCCESSFUL"));
} // setup
Michele
  • 1
  • 1
  • 2
  • it is not Arduino IDE. it is gcc which compiles and links the code. Arduino setting is -Os (optimize for size) – Juraj Nov 01 '19 at 13:22