As of May 31, 2023, we have updated our Code of Conduct.
17 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
  • 15.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
  • 104k
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
  • 104k
11 votes

Store String using F() Macro

The Arduino core macro F() takes a string literal and forces the compiler to put it in program memory. This reduces the amount of SRAM needed as the string is not copied to a data memory buffer. ...
Mikael Patel's user avatar
  • 7,929
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
  • 40.2k
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
  • 40.2k
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
  • 40.2k
9 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,437
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
  • 104k
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
  • 104k
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
  • 104k
7 votes
Accepted

What is the memory expense of creating a String from a char array?

What would be the best-practice in this kind of situation? To not use String. I also want to allow the end user to use the String functions (like indexOf) to search for particular content in emails. ...
slash-dev's user avatar
  • 2,019
7 votes
Accepted

Why does this code execute?

If you add a debugging print you will see what is happening: void setup() { Serial.begin (115200); Serial.println (); pinMode(13, OUTPUT); int len = 5000; byte *data = (byte *)malloc(len *...
Nick Gammon's user avatar
  • 36.4k
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
  • 40.2k
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
  • 40.2k
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

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
  • 104k
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
  • 40.2k
6 votes

Overview of compiled code size

One approach is to look at the assembly listing of the program. After compiling the program (via the Verify or Download button) locate the directory where the .cpp, .hex, and .elf files for the ...
James Waldby - jwpat7's user avatar
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
  • 40.2k
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
  • 104k
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
  • 104k
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,592
6 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
  • 40.2k
6 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
  • 4,729
6 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
  • 4,729
5 votes

Measure SRAM usage

Expanding on my previous comment... The RAM memory map of an AVR-based Arduino looks like this: ┌──────┬─────┬──────┬──────────┬───────┐ │ data │ bss │ heap │→ free ←│ stack │ └──────┴─────┴──────┴─...
Edgar Bonet's user avatar
  • 40.2k
5 votes
Accepted

How to correctly pack an unsigned long from 3 unsigned chars?

You can try (explict unsigned long values): unsigned long fromValues = (0x00UL << 16) | (0x01 << 8) | 0xF3; Serial.println(fromValues); or: value = (((unsigned long) c1) << 16) | ...
Mikael Patel's user avatar
  • 7,929
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
  • 104k

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