Memory use when using global variables and constants

Hello,

I am trying to optimize memory usage and learn more about the different options.
I avoid using Strings, I use c chars or c char arrays.

I notice in my installation (IDE 2.0.4) that Global use report given by the compiler is sometimes the same if I comment out a line of code that requires space,

I send a small piece of code.

//  GLOBAL VARIABLES AND CONSTANTS 
int myData [20] [40];
byte arPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,  //Pins to be set
                10, 11, 12, 13, 14, 15, 16, 17};

    char  caOwner[21]; 
const
    char  caVersion[21] = "Version 1.4.12";
    char  caVersion2[1000] = "Version 1.4.12 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
    
    unsigned int   iaParms[4];  
                                
    unsigned int   iaSchedule[12];
    unsigned int   iaCycles[12];
    unsigned int   iPeriodMinutes;
    unsigned int   iPeriodSchedule;
    unsigned int   iMinFirstServe;
    unsigned int   iMinLastServe;
    unsigned int   iPeriodUnit;
    unsigned int   iNumServe;        
    unsigned int   iFeedSeconds;     
    unsigned int   iHHMMFirstServe;  
    unsigned int   iHHMMLastServe;   
    unsigned int   iError;

//  CONFIG DATA

//  VOLTMETER
float fVolt = 0;
int   iInVoltage = 0;

//  LCD
char cOutFirstLine[21];   // output from string functions placed here
char cOutSecondLine[21];  // output from string functions placed here
char cOutThirdLine[21];   // output from string functions placed here
char saBuff[21];

//   SIGNALS FOR TRANSISTOR/OPTOCOUPLERS
const unsigned int iPerifPin     = 3; 
const unsigned int iDrivePin     = 4; 
const unsigned int iCallerPin    = 5; 
const unsigned int iShakePin     = 6; 
                                      
// For RUNNING DATA
const unsigned int iSirenSeconds    = 60;  
const unsigned int iWaitSeconds     = 60;  
const unsigned int iTimeError       = 5000;
const unsigned int iMealIndex[12][12] = {
    { 3,12,12,12,12,12,12,12,12,12,12,12}, 
    { 3, 9,12,12,12,12,12,12,12,12,12,12},
    { 0, 3, 9,12,12,12,12,12,12,12,12,12},
    { 0, 3, 6, 9,12,12,12,12,12,12,12,12},
    { 0, 1, 3, 6, 9,12,12,12,12,12,12,12},
    { 0, 1, 3, 4, 6, 9,12,12,12,12,12,12},
    { 0, 1, 3, 4, 6, 7, 9,12,12,12,12,12},
    { 0, 1, 3, 4, 6, 7, 9,10,12,12,12,12},
    { 0, 1, 2, 3, 4, 6, 7, 9,10,12,12,12},
    { 0, 1, 2, 3, 4, 5, 6, 7, 9,10,12,12},
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,12},
    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11},
};
void setup()
{
  Serial.begin(9600);
  Serial.println("Testing log to serial port");
  delay(1500);
}

void loop() {
  while(1) {
  Serial.println("Looping !!!");    
      delay(3000);
  }
}

Compiler answers:

Sketch uses 1712 bytes (5%) of program storage space. Maximum is 32256 bytes.
Global variables use 226 bytes (11%) of dynamic memory, leaving 1822 bytes for local variables. Maximum is 2048 bytes.

If I comment out from start of GLOBAL lines of code, to line 62, at the end of Globals definitions, results are still 226 bytes.

What am I missing or bad interpreting?

Thanks for you help !
Horacio

If you don't use those variables, they get optimised away

1 Like

It's compiler code optimization. The compiler will omit any code sections that it can determine, have no final effect on anything...

To control your memory usage, you have to keep that in mind, while manually examining your code for that, up front.

1 Like

Thanks for both answers, clear.
So to study real mem usage I must include all the functions I removed to shorten the code.

Have a very nice day !
Horacio

It's more involved than that, it's not just functions. Any variable not referenced in your code simply vanishes. So a string that is initialized to a value of, for example, "1234567890" might be expected to consume 11 bytes, but if it isn't not referenced, the compiler ignores it. Similarly, your variable with the long string of xxx in it will go away if you don't do something with it.

1 Like

Thanks again !
Nice day
Horacio

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