# Flash, SRAM, EEPROM, PROGMEM and F() — Arduino & Embedded C

Source: https://www.geekswithgeeks.com/en/arduino-embedded-c/memory-types

> Three memories, three jobs.

## Where data lives

The ATmega328P on the Uno R3 has **32 KB flash** (program code and constants; part is used by the bootloader), **2 KB SRAM** (variables, the stack and the heap) and **1 KB EEPROM** (non-volatile data that survives power-off, such as settings). SRAM is the scarce resource: string literals are copied from flash into SRAM at start-up on AVR, so many `Serial.print("long text")` calls quietly eat RAM. The **`F()` macro** keeps a literal in flash for print functions, and **`PROGMEM`** stores larger constant tables in flash, read back with functions like `pgm_read_byte`. EEPROM cells have a limited number of write cycles (the datasheet specifies around 100,000), so write only when a value changes. Avoid heavy `String` and `malloc` use on tiny-RAM boards, because heap fragmentation can cause hard-to-find crashes. These details are AVR-specific; other chips handle constants differently.

## Saving RAM and storing settings

Arduino C++ on AVR (Uno R3).

```cpp
#include <EEPROM.h>
#include <avr/pgmspace.h>

const uint8_t gammaTable[8] PROGMEM = {0, 2, 8, 20, 40, 70, 120, 255};
const int ADDR_BRIGHTNESS = 0;

void setup() {
  Serial.begin(115200);
  Serial.println(F("This text stays in flash, not SRAM"));

  uint8_t level = pgm_read_byte(&gammaTable[3]);  // read from flash
  Serial.println(level);

  uint8_t saved = EEPROM.read(ADDR_BRIGHTNESS);
  EEPROM.update(ADDR_BRIGHTNESS, 128);  // writes only if the value differs
  Serial.println(saved);
}

void loop() {}
```

## Bookshelf, desk and notebook

Flash is a bookshelf of reference books you rarely change, SRAM is a small desk you work on, and EEPROM is a notebook that keeps notes after you leave but wears out if you rewrite the same page too often.

**Quiz:** What does Serial.println(F("Hello")) achieve on an AVR board?

- [ ] Stores the text in EEPROM permanently
- [ ] Prints the text faster over USB
- [x] Keeps the string literal in flash instead of copying it into SRAM
- [ ] Formats the text as floating point

*Answer:* Keeps the string literal in flash instead of copying it into SRAM. F() saves scarce SRAM on AVR boards.
