Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

What are the traditional ways to optimize program memory usage?

While doing big projects using Arduino boards (Uno, Atmega328P MCU). I used to get warnings like this

Sketch uses 13764 bytes (44%) of program storage space. Maximum is 30720 bytes.
Global variables use 1681 bytes (82%) of dynamic memory, leaving 367 bytes for local variables. Maximum is 2048 bytes.
Low memory available, stability problems may occur.
  • What are the generally practiced methods for the optimization program memory usage?
  • Is there any difference in memory usage if the variable is declared globally or locally.
  • Will it matter what the control statement/selection statements are (like if, switch )
  • Usage of Serial monitor. Serial.print()
  • ......

Low memory available, stability problems may occur.

How bad are these warnings?

Before marking it duplicate, I have referred the following. But it wasn't satisfactory
Most memory efficient way to program
What are safe memory usage limits?

Answer*

Draft saved
Draft discarded
Cancel
3
  • 2
    1. Note that your first 5 bullet points are about saving flash rather than saving RAM. The OP was not short on flash. The tables will actually cost RAM, unless you put them in PROGMEM. 2. Re "There is no real difference compared to declaring xxx globally": that is, unless setup() is memory-hungry. 3. The serial buffers are not allocated if you don't use Serial. Commented Jul 2, 2020 at 7:37
  • Thanks @EdgarBonet 1. Yes, I was thinking of PROGMEM, sorry. 2. Could you explain this? Isn't setup()'s stack-allocated memory freed after it runs? 3. Thanks I have edited that. Commented Jul 2, 2020 at 10:10
  • Re explain point 2: setup() and loop() do not usually run at the same time, so their stack usages do not add up. If you make xxx global, it will be allocated even while setup() is running. This should be no concern unless setup() is itself memory-hungry. Commented Jul 2, 2020 at 10:28

default

AltStyle によって変換されたページ (->オリジナル) /