Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.
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 (...
user avatar
  • 13.7k
15 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 (...
user avatar
12 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 ...
user avatar
  • 101k
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 ...
user avatar
  • 35.5k
11 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 ...
user avatar
  • 101k
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 ...
user avatar
  • 35.5k
10 votes
Accepted

PROGMEM: do I have to copy data from flash to RAM for reading?

AVR is a modified Harvard architecture family, so code is stored in flash only, whereas data exists primarily in RAM when being manipulated. With that in mind, let's address your questions. Why on ...
user avatar
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 ...
user avatar
  • 35.5k
9 votes
Accepted

Can I write to Flash Memory using PROGMEM?

The short answer is no: PROGMEM data is read-only. Flash memory limitations The first thing to understand is that Flash memory (where program space lives) is designed for long-term fixed storage. ...
user avatar
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 ...
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 ...
user avatar
  • 101k
8 votes

PROGMEM: do I have to copy data from flash to RAM for reading?

This is how Print::print prints from program memory in the Arduino library: size_t Print::print(const __FlashStringHelper *ifsh) { const char PROGMEM *p = (const char PROGMEM *)ifsh; size_t n = 0;...
user avatar
  • 2,886
8 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. ...
user avatar
  • 7,811
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 ...
user avatar
  • 101k
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 ...
user avatar
  • 101k
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 *...
user avatar
  • 35.1k
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 ...
user avatar
  • 35.5k
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? ...
user avatar
  • 35.5k
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 ...
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 ...
user avatar
  • 101k
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, ...
user avatar
  • 35.5k
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 ...
user avatar
6 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. ...
user avatar
  • 1,974
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 ...
user avatar
  • 35.5k
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 ...
user avatar
  • 101k
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 ...
user avatar
  • 101k
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 ...
user avatar
  • 5,509
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 ...
user avatar
  • 35.5k
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 ...
user avatar
  • 3,868
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 ...
user avatar
  • 3,868

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