Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Memory jumping
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Memory jumping

by max246 on Fri Mar 20, 2020 7:03 am

I have a simple script in Arduino and I am trying to make as compact and fast, but once I try to print out the buffer, the memory goes from 9% to 61%.

I am not sure how this can be possible, this only happens as soon as I copy a BYTE from the struct into the buffer and it jumps up.

Is there something in the compiling issue? Am I doing something wrong that I can keep the memory usage to minimal?

So the whole issue happens in the function send_message

Code: Select all | TOGGLE FULL SIZE

typedef struct {
   byte s1;
   byte s2;
   byte id;
   byte length;
   byte message[50];
} message;
message queue[20];

int index_next = 0;

void setup() {
   Serial.begin(9600);
}

void loop() {
next_queue();
}


void send_message(message &msg) {
  byte buff[msg.length+4];
   buff[0] = msg.s1;
   buff[1] = msg.s2;
   buff[2] = msg.id;
   buff[3] = msg.length;
   buff[4] = msg.message[0];
   buff[5] = msg.message[1];
   Serial.write(buff, msg.length+4);
}

void next_queue() {
    if (index_next < 20) {
        send_message(queue[index_next]);
        index_next++;
    }
}

max246
 
Posts: 43
Joined: Sat Mar 17, 2012 4:46 pm

Re: Memory jumping

by adafruit_support_bill on Fri Mar 20, 2020 7:14 am

Not sure what processor you are using or how you are measuring memory. But you have 54 byte buffer as a local variable in your send_message function. This will be allocated on the stack when you call the function.

adafruit_support_bill
 
Posts: 79031
Joined: Sat Feb 07, 2009 10:11 am

Re: Memory jumping

by max246 on Fri Mar 20, 2020 7:51 am

I am compiling with Arduino Uno but you can try to compile the same sketch and..

With Serial.write it goes Global variables use 1266 bytes (61%) of dynamic memory
Without Serial.write it goes Global variables use 186 bytes (9%) of dynamic memory,

I am not sure why, because if I make that buffer with fixed number such as

buff[0] = 0x01
buff[1] = 0x0A
etc...
the dynamic memory stay at 9%

max246
 
Posts: 43
Joined: Sat Mar 17, 2012 4:46 pm

Re: Memory jumping

by adafruit_support_bill on Fri Mar 20, 2020 8:14 am

Interesting. Seems that passing Serial.write() is allocating more buffer space than seems necessary. It is not clear why.
This code allocates 61% of global memory:
Code: Select all | TOGGLE FULL SIZE
   byte x = msg.s1;
   Serial.write(x);

while this gives you just 9%:
Code: Select all | TOGGLE FULL SIZE
   byte x = 0;
   Serial.write(x);

Looks like a question for the Arduino team. You should post your question over on the Arduino forums: https://forum.arduino.cc/

adafruit_support_bill
 
Posts: 79031
Joined: Sat Feb 07, 2009 10:11 am

Re: Memory jumping

by max246 on Fri Mar 20, 2020 8:23 am

adafruit_support_bill wrote:Interesting. Seems that passing Serial.write() is allocating more buffer space than seems necessary. It is not clear why.
This code allocates 61% of global memory:
Code: Select all | TOGGLE FULL SIZE
   byte x = msg.s1;
   Serial.write(x);

while this gives you just 9%:
Code: Select all | TOGGLE FULL SIZE
   byte x = 0;
   Serial.write(x);

Looks like a question for the Arduino team. You should post your question over on the Arduino forums: https://forum.arduino.cc/


I did herehttps://forum.arduino.cc/index.php?topic=671913.new#new wonder if it is the correct place :/

max246
 
Posts: 43
Joined: Sat Mar 17, 2012 4:46 pm

Re: Memory jumping

by adafruit_support_bill on Fri Mar 20, 2020 8:42 am

Actually, the answer on the Arduino forum does make sense. Your queue is 1080 bytes which is more than half of the UNO's 2K of SRAM. I'm just surprised at how 'clever' the compiler is in optimizing that out if it is not used.

adafruit_support_bill
 
Posts: 79031
Joined: Sat Feb 07, 2009 10:11 am

Re: Memory jumping

by max246 on Fri Mar 20, 2020 8:47 am

adafruit_support_bill wrote:Actually, the answer on the Arduino forum does make sense. Your queue is 1080 bytes which is more than half of the UNO's 2K of SRAM. I'm just surprised at how 'clever' the compiler is in optimizing that out if it is not used.


Sorry I am being too thick but what would be the optimisation in this case?

max246
 
Posts: 43
Joined: Sat Mar 17, 2012 4:46 pm

Re: Memory jumping

by adafruit_support_bill on Fri Mar 20, 2020 9:05 am

If the queue is not used, then no space is allocated for it.

adafruit_support_bill
 
Posts: 79031
Joined: Sat Feb 07, 2009 10:11 am

Re: Memory jumping

by max246 on Fri Mar 20, 2020 5:05 pm

adafruit_support_bill wrote:If the queue is not used, then no space is allocated for it.

But that doesnt explain the jump of dynamic memory, if it is not used in this sketch how come the dynamic memory is increased ?

max246
 
Posts: 43
Joined: Sat Mar 17, 2012 4:46 pm

Re: Memory jumping

by adafruit_support_bill on Fri Mar 20, 2020 6:24 pm

You 'use' it as soon as you try to print values from it,

adafruit_support_bill
 
Posts: 79031
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.