user82594: I am working on a simple project on talking to the serial monitor, and my only global variable ("int" type, named "incomingByte") is consuming a lot of memory. There is no problem on running the scketch, but I am not satisfied to have it that way.
When I load it, I receive: "Global variables use 390 bytes (19%) of dinamic memory...".
Why is it happening, and how could I fix this?
int incomingByte = 0; //for the incoming serial data
void setup() {
  // put your setup code here, to run once:
  randomSeed(analogRead(0)); //starts the pseudo-random sequence in a random point
  Serial.begin(9600);
  //waits for serial initialization to send the messages:
  //(Serial) returns false for serial off, and true for serial on. This is inverted with !.
  while(!Serial){
    ;
  }
  //sending instructions:
  Serial.println("Initializing...");
  Serial.println(" ");
  delay(2000);
  Serial.println("Please select an option:");
  Serial.println("1 - greetings");
  Serial.println("2 - random number");
  Serial.println("3 - calculator");
  Serial.println(" ");
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() > 0){
    incomingByte = Serial.read(); // Serial.read() returns the first byte of incoming data
  
    //recognizing the command received:
    if (incomingByte == '1'){
      numberOne();
      delay(1500);
      Serial.println("Waiting; choose another option");
      Serial.println(" ");
    }
    if (incomingByte == '2'){
      numberTwo();
      delay(1500);
      Serial.println("Waiting; choose another option");
      Serial.println(" ");
    }
    if (incomingByte == '3'){
      numberThree();
      delay(1500);
      Serial.println("Waiting; choose another option");
      Serial.println(" ");
    }
  }
}


void numberOne() {
  Serial.println("Hello, Vitor! Good to see you!");
}


void numberTwo() {
  //sets the random number to be picked between [0, 101[
  long randomNum = random(0, 101);
  Serial.print("Your random number is: ");
  Serial.println(randomNum, DEC);
}

void numberThree(){
  Serial.println("Tool not available yet");
}


user7508: A few things that count towards the “global variables” that you may not
think of:

all your literal strings, unless encapsulated within the F()
macro; you have quite a few of them
The Serial object, including its transmit and receive buffers (157
bytes on an Uno)
The vtable for HardwareSerial (that's the class Serial belongs
to, 18 bytes)
The time-keeping variables (9 bytes).

And then, on top of that, there are the actual global variables of your
code, and the static locals which, for the purpose of memory allocation,
are the same as globals. An int, for example, takes 2 bytes on
AVR and 4 bytes on most other hardware platforms.

Edit: answering the questions in a comment

every single time I call Serial it will increase the memory
consumption of the code?

I don't quite understand what you mean by “every time”:

Every time you add a reference to Serial to a sketch, the linker
will add the Serial object to your compiled code, and the program's
RAM consumption will grow by the size of this object.
Within a single sketch, whether Serial is used once or many times
makes no difference: the object will be instantiated only once.


why does the IDE considers everything that uses RAM as a global
variable?

When it says “global variables” it actually means “statically allocated
data”. It says “global variables” because it is targeted at beginners,
and the authors assume beginners would not understand what static
allocation means. Globals are the simplest form of static allocation.

is there a difference in consumption between globals and locals?

Yes. Globals get allocated statically, i.e. at compile time. That's why
the IDE can report how much space they will consume. Locals (at least
non-static locals) get allocated and deallocated on the stack, at run
time. Short of running the program in an emulator, it is very hard to
predict how much stack space a program will consume, as this will
usually depend on run-time events.

82594: <p>I am working on a simple project on talking to the serial monitor, and my only global variable (&quot;int&quot; type, named &quot;incomingByte&quot;) is consuming a lot of memory. There is no problem on running the scketch, but I am not satisfied to have it that way.</p>
<p>When I load it, I receive: &quot;Global variables use 390 bytes (19%) of dinamic memory...&quot;.
Why is it happening, and how could I fix this?</p>
<pre><code>int incomingByte = 0; //for the incoming serial data
void setup() {
  // put your setup code here, to run once:
  randomSeed(analogRead(0)); //starts the pseudo-random sequence in a random point
  Serial.begin(9600);
  //waits for serial initialization to send the messages:
  //(Serial) returns false for serial off, and true for serial on. This is inverted with !.
  while(!Serial){
    ;
  }
  //sending instructions:
  Serial.println(&quot;Initializing...&quot;);
  Serial.println(&quot; &quot;);
  delay(2000);
  Serial.println(&quot;Please select an option:&quot;);
  Serial.println(&quot;1 - greetings&quot;);
  Serial.println(&quot;2 - random number&quot;);
  Serial.println(&quot;3 - calculator&quot;);
  Serial.println(&quot; &quot;);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(Serial.available() &gt; 0){
    incomingByte = Serial.read(); // Serial.read() returns the first byte of incoming data
  
    //recognizing the command received:
    if (incomingByte == '1'){
      numberOne();
      delay(1500);
      Serial.println(&quot;Waiting; choose another option&quot;);
      Serial.println(&quot; &quot;);
    }
    if (incomingByte == '2'){
      numberTwo();
      delay(1500);
      Serial.println(&quot;Waiting; choose another option&quot;);
      Serial.println(&quot; &quot;);
    }
    if (incomingByte == '3'){
      numberThree();
      delay(1500);
      Serial.println(&quot;Waiting; choose another option&quot;);
      Serial.println(&quot; &quot;);
    }
  }
}


void numberOne() {
  Serial.println(&quot;Hello, Vitor! Good to see you!&quot;);
}


void numberTwo() {
  //sets the random number to be picked between [0, 101[
  long randomNum = random(0, 101);
  Serial.print(&quot;Your random number is: &quot;);
  Serial.println(randomNum, DEC);
}

void numberThree(){
  Serial.println(&quot;Tool not available yet&quot;);
}
</code></pre>
 7508: <p>A few things that count towards the “global variables” that you may not
think of:</p>
<ul>
<li>all your literal strings, unless encapsulated within <a href="https://www.arduino.cc/reference/en/language/variables/utilities/progmem/#_the_f_macro" rel="nofollow noreferrer">the <code>F()</code>
macro</a>; you have quite a few of them</li>
<li>The <code>Serial</code> object, including its transmit and receive buffers (157
bytes on an Uno)</li>
<li>The <a href="https://en.wikipedia.org/wiki/Virtual_method_table" rel="nofollow noreferrer">vtable</a> for <code>HardwareSerial</code> (that's the class <code>Serial</code> belongs
to, 18 bytes)</li>
<li>The time-keeping variables (9 bytes).</li>
</ul>
<p>And then, on top of that, there are the actual global variables of your
code, and the static locals which, for the purpose of memory allocation,
are the same as globals. An <code>int</code>, for example, takes 2 bytes on
AVR and 4 bytes on most other hardware platforms.</p>
<hr />
<p><strong>Edit</strong>: answering the questions in a comment</p>
<blockquote>
<p>every single time I call <code>Serial</code> it will increase the memory
consumption of the code?</p>
</blockquote>
<p>I don't quite understand what you mean by “every time”:</p>
<ul>
<li>Every time you add a reference to <code>Serial</code> to a sketch, the linker
will add the <code>Serial</code> object to your compiled code, and the program's
RAM consumption will grow by the size of this object.</li>
<li>Within a single sketch, whether <code>Serial</code> is used once or many times
makes no difference: the object will be instantiated only once.</li>
</ul>
<blockquote>
<p>why does the IDE considers everything that uses RAM as a global
variable?</p>
</blockquote>
<p>When it says “global variables” it actually means “statically allocated
data”. It says “global variables” because it is targeted at beginners,
and the authors assume beginners would not understand what static
allocation means. Globals are the simplest form of static allocation.</p>
<blockquote>
<p>is there a difference in consumption between globals and locals?</p>
</blockquote>
<p>Yes. Globals get allocated statically, i.e. at compile time. That's why
the IDE can report how much space they will consume. Locals (at least
non-static locals) get allocated and deallocated on the stack, at run
time. Short of running the program in an emulator, it is very hard to
predict how much stack space a program will consume, as this will
usually depend on run-time events.</p>
