Solving bugs related to Arduino SRAM

Modified on Wed, 04 Jan 2023 at 10:01 PM

Refer to section What is SRAM? to learn more about SRAM.



Are you running out of SRAM memory?

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…


If you meet one of these situations, thus you should suspect that your Arduino is running out of SRAM. This typically happens after one of the following actions :

  • 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)




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.


Measurement probes

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.


Example

Refer to this example : Checking your Free RAM.

 



How to save SRAM memory?

If you are running out of SRAM, we are presenting here a list of the major actions that you can do to optimize it, starting with what we believe to be the most efficient one. For more details about how to optimize your memory, check out this article from Adafruit.



Put literal strings in the F() macro

This action moves literal strings to Program (Flash) Memory. It only works if the literal string is passed as a function parameter to a function. 


Example

Replace :

Serial.print("my string");

With:

Serial.print(F("my string"));



Move constant variables to Program (Flash) Memory

This method is more powerful than the previous one as it lets you move any constant variable to Program Memory. Moreover, it also works for constant string variables, arrays of string literals, and more generally, any array holding constant data.


However, this method is more complex since it requires declaring your variables using the const ... PROGMEM keyword, which is not easy to handle. Refer to this article from the Arduino Reference Manual to learn how to use it.



Don’t oversize your variables

Use the smallest data type you need for your variables.



Remove unused variables

Go hunting all your unused variables. And remove them all!



Prefer local variables

When a Global and Static variable is defined, the corresponding allocated memory is never released. On the contrary, when a Local variable is defined inside a function, memory is allocated for this variable only for the time this function is being executed and is released when this function exits. So, prefer using local variables instead of global variables.



Be careful with dynamically allocated memory

If you are using the new keyword or the malloc() function, it means you are dynamically allocating memory. The thing is this memory is not automatically deallocated, it is up to you to do it. So, don’t forget to deallocate this memory when you are done with it.



Use an Arduino board with more SRAM

After all these actions, if you are still short on SRAM, your last option is to use an Arduino board with more embedded SRAM.


Refer to section Arduino board SRAM comparison for choosing a new board.



 



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article