I'm inspecting where the memory is used in an application which runs on Arduino. To my surprise, there are several locations where 100 to 200 bytes are consumed by simply calling a function. Example:
void Dispatcher::processCommand() {
...
displayMemory();
this->process(this->instruction);
}
void Dispatcher::process(const Instruction &instruction) {
displayMemory();
if (instruction.length > 0) {
...
return;
}
ErrorResponse::componentNotFound(instruction).output(this->serial);
TerminationResponse(instruction.commandId, 0xFF).output(this->serial);
}
The first call to displayMemory()
will report that there is 351 bytes left. The second one will tell that only 159 bytes remain (that is, 192 bytes less).
If I alter the body of the process
function, the difference between the two memory reports vary. For instance, if I remove the two last lines, the difference drops from 192 bytes to 12 bytes.
I don't understand why this happens. I was thinking that between the moment I'm ready to call a function and a moment when the function starts, the memory should be used exclusively by the additional element in the call stack (the aforementioned 12 bytes maybe?) and the copying of the parameters passed by value (none here). Why is this not the case? What really happens under the hood?