how to not exceed arduino ram ?

Hello everyone.
I have a project where i have to use the Arduino Uno in sufficient manner ( not exceeding the Arduino ram ).
i started gathering information about variable length and so on,however, I have some questions and any help will be appreciated

1) why the new sketch that have setup() , loop() functions says global variables uses 9 bytes of dynamic memory at the time that there isn’t any global variables !

**2)**does these declaration will exceed the ram limit ? and does ar[2048] , val a global variables ?

byte ar[2048];
int val;

void setup()

void loop()

does these declaration will exceed the ram limit ? and do ar[2048] , val a global variables ?

Yes this exceeds the limit but the compiler is smart enough to know that you never use the array and so in the optimisation part of compiling it removes it for you.

I suspect that the 9 bytes are reserved for the function return stack from the hidden “main” function.

The Uno has only got 2k of SRAM so there is not space for an array of 2048 bytes,

Be aware that there needs to be sufficient unused SRAM for local variables (defined within functions) and for on-the-fly calculations as the program runs. I don't think there is any simple way to figure out how much spare SRAM is needed as it will vary with different programs.

Use as little memory as you can.


Those 9 bytes are these variables from cores/arduino/wiring.c:

volatile unsigned long timer0_overflow_count = 0;
volatile unsigned long timer0_millis = 0;
static unsigned char timer0_fract = 0;

this replies are very straight-forward , thank you everyone :slight_smile:

Just another point. The compiler does not know about how deep the functions will be called and so can not report on the exact amount of ram the code uses. Also some array definitions are hidden from the compiler and do not show up at compile time. This is especially true of the Neopixel driving libraries.