Wy is my global memory so high?

These are the ONLY two variables declared outside of a class:

Servo myServo;
int myServoPin = 9;

Every other variable in the entire sketch is inside its own class. no statics. And yet compiler says:

Global variables use 1733 bytes (84%) of dynamic memory, leaving 315 bytes for local variables. Maximum is 2048 bytes.

How can two variables possibly use 1733 bytes? It seems a lot (if not all) of my variables inside a class are seen as global for some reason.

And when I remove a class from the sketch, a whole bunch of global memory comes back:

Global variables use 251 bytes (12%) of dynamic memory, leaving 1797 bytes for local variables. Maximum is 2048 bytes.

So what's the deal with scoping here? Why are non-static variable declared inside a class counting towards global memory?

Probably because someone is creating a global instance of the class. The class variables have to go somewhere.

Here's my declaration of the class (er, method, that is) using all the global var mem:

void CommandProcessor()

And I need to correct my nomenclature. This is a field, just like setup and loop, not a class, but the documentation sez fields (functions) encapsulate their variables as private... this is why I'm confused. My function here is just like loop or setup, yet it's using global mem.

OK, and to narrow it down further... I have some char arrays inside CommandProcessor. Example:

char ItemE1[numToken]; "E1";

However, if I just declare them dynamically, global memory usage is reduced:

char ItemE1[numToken]; //="E1";

So that tells me...?

... that you do not completely understand SRAM usage.

Arduino Memories | Memories of an Arduino | Adafruit Learning System

This tells you const data takes up RAM. The fix is PROGMEM. The Adafruit article also talks about using PROGMEM.

https://www.arduino.cc/reference/en/language/variables/utilities/progmem/

So by declaring my char array with an actual value, that's making it a const? OK -- I learned something there. I thought const needed actual "const" in front of it. But it makes sense now that you point it out. = "SOMETHING" means it's kind of stuck there, like a constant.

Or maybe just static in general? Hence in the stack.

It doesn't make it const. It DOES make it consume RAM.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.