Skip to main content
18 votes
Accepted

What are the traditional ways to optimize program memory usage?

What are the generally practiced methods for the optimization program memory usage? First, note you are searching for ways to lower SRAM memory. This contains global (variable) memory and heap space (...
Michel Keijzers's user avatar
17 votes
Accepted

Why is it considered bad practice to use the 'new' keyword in Arduino?

Most Arduinos (like the Uno or Nano) have very few RAM, thus you first need to make sure that you never allocate too much memory. Dynamically allocating memory can also lead to heap fragmentation (...
chrisl's user avatar
  • 16.3k
13 votes
Accepted

int VS uint8_t VS uint16_t

You can decipher most of them yourself. A u prefix means unsigned. The number is the number of bits used. There's 8 bits to the byte. The _t means it's a typedef. So a uint8_t is an unsigned 8 bit ...
Majenko's user avatar
  • 105k
12 votes
Accepted

When to use PROGMEM?

Say you have a string of 1000 characters in Flash, and you want to print it to Serial. Yes, you need to load it into RAM in order to pass that data around the place for printing. But, do you need to ...
Majenko's user avatar
  • 105k
11 votes
Accepted

memory usage "dos and don'ts"

As you have noticed yourself, this can be somewhat platform-dependent. Since you are working on a Nano, my answer will be for the AVR architecture, when compiled with gcc, the avr-libc, and the ...
Edgar Bonet's user avatar
  • 43.3k
11 votes

Request for help condensing code and saving memory

You have a huge switch...case here to decide the name of the file that should be played. This costs a lot of code space, but also data space, because every single literal string in this portion of ...
Edgar Bonet's user avatar
  • 43.3k
10 votes
Accepted

Detailed analyse of memory usage

Since you seem interested about RAM, rather than flash usage, I suggest you look at the symbol table of your compiled program, which will be more telling than the disassembly. On my Linux system, I ...
Edgar Bonet's user avatar
  • 43.3k
10 votes
Accepted

Can you access the EEPROM of an ATtiny with Arduino Code?

The Arduino EEPROM library is compatible with the ATTiny range of AVR microcontrollers as the library itself is built on the standard Atmel AVR avr/eeprom.h 'library' so it is compatible with all the ...
RSM's user avatar
  • 1,457
9 votes

What is using up all the RAM in my program?

There is a lot you are missing. Such as: SoftwareSerial is using RAM. The system stack is using RAM. The Arduino core software is using RAM. There's plenty more going on than just your sketch. For ...
Majenko's user avatar
  • 105k
8 votes
Accepted

Most memory efficient way to program

That is an incredibly hard question to give a definitive answer to. In general using OO consumes more memory, but that is the price you pay for greater flexibility and ease of use. The difference is ...
Majenko's user avatar
  • 105k
8 votes

Memory usage: #define vs. static const for uint8_t

You'll find no noticeable difference memory-wise between the two. The only real difference is that the const method also imposes a type to the value, which can be useful for function overloading or ...
Majenko's user avatar
  • 105k
7 votes

Lowest memory consumption for arrays

If I dare suggest an unorthodox solution... I suggest you store the times as “float11” floating point numbers. Michel Keijzers' solution is nice, but there is an issue with the choice of the time ...
Edgar Bonet's user avatar
  • 43.3k
7 votes

How can a function/method determine if a const array passed in is PROGMEM (flash) or not (RAM)?

I am afraid there is no good solution to this problem. One option I do like is to use the __flash qualifier instead of PROGMEM: const uint8_t ram_array[] = { 1, 2, 3, 4 }; __flash const uint8_t ...
Edgar Bonet's user avatar
  • 43.3k
7 votes

Serial printing from flash memory (F() macro, PROGMEM, sprintf_P, SPTR)

First, the freeMemory() function that you used from Adafruit was originated from a GitHub repository. I don't know about the __arm__ implementation, but for the AVR one, it is incomplete for handling ...
hcheung's user avatar
  • 1,416
7 votes

What are the traditional ways to optimize program memory usage?

I just want to add a single bullet to Michel Keijzers’ excellent answer: think about every single item you are storing in memory and ask yourself the question: do I really need to keep this in RAM? ...
Edgar Bonet's user avatar
  • 43.3k
7 votes

Why is it considered bad practice to use the 'new' keyword in Arduino?

Dynamic allocation is generally discouraged in embedded applications because you cannot guarantee that you do not exceed (attempt to allocate more than) the available memory. Static allocation will ...
crasic's user avatar
  • 226
7 votes
Accepted

ATTiny13 Interrupt Issue

You would want your bool LED_STATE = false; to be volatile qualified like volatile bool LED_STATE = false; otherwise the compiler's optimizer may assume that the value cannot be changing between loads ...
timemage's user avatar
  • 5,324
7 votes
Accepted

Memory allocation on Arduino Due never returns NULL

Why This is a partial answer, for now, mostly with regard to: Why does malloc() never return NULL? So, the Due appears to use Newlib as its libc implementation; that is the systems C (standard and ...
timemage's user avatar
  • 5,324
7 votes
Accepted

1 global variable consuming 19% of memory

A few things that count towards the “global variables” that you may not think of: all your literal strings, unless encapsulated within the F() macro; you have quite a few of them The Serial object, ...
Edgar Bonet's user avatar
  • 43.3k
6 votes
Accepted

Storing an array of function pointers

This all gets very much trickier than just an array of function pointers. Your functions are methods in unknown objects. As such you can't have one single type that stores pointers to the functions ...
Majenko's user avatar
  • 105k
6 votes
Accepted

Is the a low memory alternative to SoftwareSerial?

Simplest thing would probably be to edit your copy of SoftwareSerial.h and replace #define _SS_MAX_RX_BUFF 64 on line 42 by something smaller. If your loop() runs fast enough (no delays, no blocking ...
Edgar Bonet's user avatar
  • 43.3k
6 votes
Accepted

Arduino constant vs Microchip PIC constant

const means different things in different contexts as far as storage goes. For a simple numeric value the compiler will generally replace the constant with the literal value. Any mathematics using ...
Majenko's user avatar
  • 105k
6 votes

Memory usage: #define vs. static const for uint8_t

A #define is a preprocessor macro. As Gerben says in his comment, it's just an automated find-and-replace. If you use it to hold things like C string constants, e.g. #define ERROR_STRING "You messed ...
Duncan C's user avatar
  • 5,702
6 votes
Accepted

Does the compiled binary of a sketch include uncalled functions from a library?

In general only the functions actually used by your code are included in the final linkage of the binary. However it's not always possible for the compiler to know what is used and what isn't. In the ...
Majenko's user avatar
  • 105k
5 votes
Accepted

EEPROM - Storing Static Data

Some things I noticed in your code: You are writing everything to the same address (is that intentional?) You are only writing the first 6 six bytes of your arrays (I am pretty sure that is ...
DarthRubik's user avatar
5 votes
Accepted

What is the best way to get more RAM for a sound buffer?

A simple SPI SRAM chip. Available up to 128KB, such as the 23LC1024: http://www.microchip.com/wwwproducts/en/23LC1024
Majenko's user avatar
  • 105k
5 votes

Is there any way I can connect the RAM of a computer to Arduino?

No, you can't connect your normal desktop computer memory to your Arduino. For a number of reasons: Computer memory (at least for the past 20 years) is DDR SDRAM. That's Synchronous Dynamic RAM. It ...
Majenko's user avatar
  • 105k
5 votes

Storing an array of function pointers

This is not a direct answer to your question, as I have no such answer, and I believe the answer you are seeking would be of no use to you. The problem with your question is that your method of ...
Edgar Bonet's user avatar
  • 43.3k
5 votes
Accepted

How to read main code inside main code in Arduino?

Interesting. If you want hex you have to tell it to print in hex. If I change your display line to: Serial.println(displayInt, HEX); Then I get: 940C 3594 35 C00 940C Compare this to what the ...
Nick Gammon's user avatar
  • 38.4k

Only top scored, non community-wiki answers of a minimum length are eligible