Dynamic memory exceeds available space in Arduino UNO board

I’m making a small game recently and I want to use Arduino to do it. My code compiles without problems, but when I upload it, I get an error like the one shown in the picture, is it because I have too many variables defined?
Can someone please tell me how to fix it?

Arduino:1.8.12 (Windows 10), Development board: "Arduino Uno"

The project uses 6336 bytes, which occupies (19%) of the program storage space. The maximum is 32256 bytes. data section exceeds available space in board

Global variables used 3046 bytes, (148%) of dynamic memory, leaving -998 bytes for local variables. The maximum is 2048 bytes.
There is not enough memory; visit the following URL to follow the instructions to reduce memory usage.
http://www.arduino.cc/en/Guide/Troubleshooting#size
Error while compiling for development board Arduino Uno.

Turn on in File -> Preferences
"Show detailed output during compilation" option
This report will contain more information.

Yes. It seems like you have a lot of global variables. Your compiler will definitely compile the code without any error because the compiler is unaware of the memory usage at a given point in time. You may either buy an Arduino Mega or take the following steps to reduce the SRAM usage.

  • You can store some of the data in Flash memory (Program memory) instead of SRAM.

  • Use PROGMEM to store all the constant variables in Flash memory.

  • Use the F macro to store all the string constant to flash memory. Example: Use Serial.print (F("Hello World")) ; instead of Serial.print ("Hello world") ;

1 Like

Without replacing the board, I think the third method is the easiest way.
I tried the method using F macro and had success, the code was successfully uploaded to the board, thank you very much!

2 Likes

In that case you may mark the post as solved.

1 Like

Yes, thanks for the reminder!

1 Like