1) Are you running out of SRAM memory?
Refer to section What is SRAM? to learn more about SRAM.
Your Arduino has a limited amount of SRAM memory (Static Random-Access Memory). This memory is used by your Arduino to store program variables and to manage interrupts and function calls while your code is running. The bigger your code is, the more memory it uses.
However, running out of this memory entails bugs that are particularly hard to interpret. Indeed, even if your code is perfectly correct, even if it compiles and loads successfully, the program may not run. Or it may run then crash unexpectedly. Or part of your program may behave irrationally…
Running out of SRAM typically happens after :
- Increasing your code size (new library added, new function added…)
- Integrating components which requires a lot of memory (graphical display, SD card…)
- Dynamic instantiation of objects (new object instantiated with the “new” keyword)
2) Measuring your Free RAM
If you suspect a SRAM shortage, the first thing to do is to measure the amount of Free RAM remaining. It should never become too low, that is, avoid having less than 150 bytes left. Indeed, SRAM used fluctuates while your program is running, depending on the stacking of called function, the number of local variables and the number of objects instantiated dynamically.
As SRAM used fluctuates while your program is running, measured values will differ depending on where the measurement is performed inside your program and at which moment. Thus, it is often required to measure Free RAM values at different place inside the code to find out what are the smallest values and which code snippets are the biggest SRAM consumers.
For this purpose, our SRAM library supplies 3 probes which can be put at 3 different places inside your program to perform SRAM measurement. Monitoring of these probes is easily performed using either the SRAM Panel or the Chart.
Refer to this example : Checking your Free RAM.