I'm using too much RAM. How can this be measured?
You can use the functions provided AVRGCC: Monitoring Stack Usage
The function was intended to check the stack usage but what it reports is the actual RAM that has never been used (during execution). It does so by "painting" (filling) the RAM with a known value (0xC5), and then checking the RAM area counting how many bytes have still the same initial value.
The report will show the RAM that has not been used (minimum free RAM) and therefor you can calculate the max RAM that has been used (Total RAM - reported RAM).
There are two functions:
StackPaint is executed automatically during initialization and "paints" the RAM with the value 0xC5 (can be changed if needed).
StackCount can be called at any point to count the RAM that hasn't been used.
Here is an example of usage. Doesn't do much but is intended to show how to use the functions.
// -----------------------------------------------------------------------------
extern uint8_t _end;
extern uint8_t __stack;
void StackPaint(void) __attribute__ ((naked)) __attribute__ ((section (".init1")));
void StackPaint(void)
{
#if 0
uint8_t *p = &_end;
while(p <= &__stack)
{
*p = 0xc5;
p++;
}
#else
__asm volatile (" ldi r30,lo8(_end)\n"
" ldi r31,hi8(_end)\n"
" ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */
" ldi r25,hi8(__stack)\n"
" rjmp .cmp\n"
".loop:\n"
" st Z+,r24\n"
".cmp:\n"
" cpi r30,lo8(__stack)\n"
" cpc r31,r25\n"
" brlo .loop\n"
" breq .loop"::);
#endif
}
uint16_t StackCount(void)
{
const uint8_t *p = &_end;
uint16_t c = 0;
while(*p == 0xc5 && p <= &__stack)
{
p++;
c++;
}
return c;
}
// -----------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(StackCount(), DEC); // calls StackCount() to report the unused RAM
delay(1000);
}
The main issues you can have with memory usage at runtime are:
- no available memory in the heap for dynamic allocations (
malloc
ornew
) - no room left on the stack when calling a function
Both are actually the same as the AVR SRAM (2K on Arduino) is used for both (in addition to static data which size never changes during program execution).
Generally, dynamic memory allocation is seldom used on MCUs, only a few libraries typically use it (one of them is String
class, which you mentioned you don't use, and that's a good point).
The stack and the heap can be seen in the picture below (courtesy of Adafruit):
Hence, the most expected issue comes from stack overflow (i.e. when the stack grows towards the heap and overflows on it, and then -if the heap was not used at all- overflows on the static data zone of the SRAM. At that time, you have a high risk of either:
- data corruption (i.e. the stack ovewrites heap or static data), giving you ununderstandable behavior
- stack corruption (i.e. the heap or static data overwrites stack content), generally leading to a crash
In order to know the amount of memory that's left between the top of the heap and the top of the stack (actually, we might call it the bottom if we represent both the heap and the stack on the same image as depicted below), you can use the following function:
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
In the code above, __brkval
points to the top of the heap but is 0
when the heap has not been used, in which case we use &__heap_start
which points to __heap_start
, the first variable that marks the bottom of the heap; &v
points of course to the top of the stack (this is the last variable pushed on the stack), hence the formula above returns the amount of memory available for the stack (or the heap if you use it) to grow.
You can use this function in various locations of your code to try and find out where this size is getting dramatically reduced.
Of course, if ever you see this function return a negative number then it is too late: you have already overflown the stack!
When you figure out how to locate the generated .elf file in your temporary directory, you can execute the command below to dump a SRAM usage, where project.elf
is to be replaced with the generated .elf
file. The advantage of this output is the ability to inspect how your SRAM is used. Do all the variables need to be global, are they really all required?
avr-objdump -S -j .bss project.elf
project.elf: file format elf32-avr
Disassembly of section .bss:
00800060 <__bss_start>:
...
00800070 <measurementReady>:
...
00800071 <cycles>:
...
00800073 <measurement>:
800073: 00 00 00 00 ....
00800077 <measurementStart>:
800077: 00 00 00 00 ....
0080007b <timerOverflows>:
80007b: 00 00 00 00
Notice that this doesn't show stack or dynamic memory use as Ignacio Vazquez-Abrams noted in the comments below.
Additionally a avr-objdump -S -j .data project.elf
can be checked, but none of my programs output anything with that so I can't tell for sure if it is useful. It supposed to list 'initialized (non-zero) data'.