Go Down

Topic: My RAM of Arduino MEGA LOSES memory every time I enter a function (Read 284 times) previous topic - next topic

LordEdwenWinston

Hello!
I write this topic, because after of searching a lot, i couldn't find anything.

Okay... I have got a long code, with many functions in there. So I put a code there to control where my Arduino Mega 2560 loses memory, and the result is: When I call a VOID function, and Arduino enter in the function loses around 25 bytes, this memory I never recover, so, the Arduino is always losing RAM.
I use around a 40% of the RAM.

This is the caller:

Code: [Select]
void loop(){
 lcd.clear();
//------------------RAM COMPROBATION---------------------------------------
  Serial.print("\n\n  SRAM FREE LOOP: "); Serial.println(freeRam());
//------------------RAM COMPROBATION---------------------------------------


[...]


if((pulsacion2 != 0)||(pulsacion != 0)){
    lcd.clear();
    into = true;
    Serial.print("\n\n  SRAM FREE LOOP TYPE: "); Serial.println(freeRam());
    MenuPrincipal();
  }



And the called function is this:

Code: [Select]

void MenuPrincipal(){
  //------------------RAM COMPROBATION---------------------------------------
  Serial.print("\n\n  SRAM FREE IN MAIN MENU [INITIAL]: "); Serial.println(freeRam());
  //------------------RAM COMPROBATION---------------------------------------
  lcd.clear();
  int level = 1;
  int sel = 7;
  bool banana = false;
 
  DateTime now = rtc.now();
  tiempo1 = 60*now.hour() + now.minute() + 5;
 
  // Se imprime la pantalla de inicio:
 
  lcd.setCursor(0, 0); lcd.print("---MENU PRINCIPAL---");
  lcd.setCursor(2, 1); lcd.print("1. Cosas varias");//lcd.write(byte(5));
  lcd.setCursor(2, 2); lcd.print("2. Otras cosas ");//lcd.write(byte(5));
  lcd.setCursor(2, 3); lcd.print("3. Salir         ");lcd.write(byte(4));
  //------------------RAM COMPROBATION---------------------------------------
  Serial.print("\n\n  SRAM FREE IN MAIN MENU [INITIAL 2]: "); Serial.println(freeRam());
  //------------------RAM COMPROBATION---------------------------------------
  do{

[...]



As I said before, when I call this function I loss memory that I never recover, and there are the results of the Serial:

Code: [Select]


  SRAM FREE INITIALLY: 4919


  SRAM FREE LOOP: 4898


  SRAM FREE LOOP TYPE: 4898


  SRAM SRAM FREE IN MAIN MENU [INITIAL]: 4874


  SRAM SRAM FREE IN MAIN MENU [INITIAL 2]: 4874




As you can see, I loses 20 bytes, or 25 bytes when i get into the woid Function, and i don't know how to fix it.


I hope your help!

Thanks for all:)

marco_c

All of these variables
Code: [Select]
  int level = 1;
  int sel = 7;
  bool banana = false;
 
  DateTime now = rtc.now();

are using RAM. These are only allocated on the stack once you enter the function and freed once you exit.
Put another print statement after you return from MenuPrincipal and you should see it the same as before the call.
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

LordEdwenWinston

Hi Marcus! Thanks for answering my question, I can go to the MenuPrincipal, but when I get again I lose memory, I never recover it, is like I am running some program in the background, but arduino can't do this.

sterretje

How about posting complete code? Or a small example that demonstrates the problem?

In your output example, where is the output of e.g.
Code: [Select]
Serial.print("\n\n  SRAM FREE IN MAIN MENU [INITIAL]: "); Serial.println(freeRam());?

Do you loose memory everytime that you call MenuPrincipal()? Or only the first time.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up