Fix "Sketch too big" on Arduino and ESP32 (flash memory full)
Sketch too big; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing it.
- Sketch uses 34560 bytes (107%) of program storage space. Maximum is 32256 bytes.
- text section exceeds available space in board
- Not enough memory; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing your footprint.
- Global variables use 2150 bytes (104%) of dynamic memory, leaving -102 bytes for local variables. Maximum is 2048 bytes.
- Sketch uses 1442345 bytes (110%) of program storage space. Maximum is 1310720 bytes.
The compiled program does not fit in the board's available flash memory, so the IDE refuses to upload it. Remove debug code and heavy libraries, swap the biggest parts for lighter ones, and on ESP32 pick a partition scheme with a larger app partition.
What this error means
After compiling, the IDE compares the binary size ("program storage space", the flash) with the maximum defined for the board. On an Uno, that is 32,256 bytes: 32 KB minus the bootloader. On an ESP32, the limit is not the chip size but the app partition size, 1,310,720 bytes (1.25 MB) with the default 4 MB scheme. The "Not enough memory" variant is about RAM ("dynamic memory"): global variables and strings copied to RAM. Both show up in the "Sketch uses" and "Global variables use" lines.
Common causes
- Heavy libraries: display libraries with fonts (Adafruit_GFX, U8g2 in graphics mode),
String,printfwithfloat, Wi-Fi and Bluetooth stacks on ESP32. - Lots of text and constant data:
Serial.printdebug messages, HTML pages, images, lookup tables. - App partition too small on ESP32: the default scheme reserves space for OTA and the file system, and the app is limited to 1.25 MB even on a 4 MB chip.
- RAM full ("Not enough memory" variant): large global arrays and string literals that the AVR copies to RAM at startup.
- Wrong board selected: a Nano "ATmega328P (Old Bootloader)" only offers 30,720 bytes, an ATmega168 only 14,336.
Step-by-step fix
-
Read both size lines
Check which one goes over 100%: "program storage space" (flash, the real "Sketch too big") or "dynamic memory" (RAM, "Not enough memory"). The fixes are not the same.
Sketch uses 34560 bytes (107%) of program storage space. Maximum is 32256 bytes. Global variables use 1520 bytes (74%) of dynamic memory, leaving 528 bytes for local variables. Maximum is 2048 bytes. Sketch too big; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing it. -
Find what takes up space
List the biggest symbols in the
.elffile (in the build folder, path shown with verbose compile output). With ESP-IDF,idf.py size-componentsgives the size per component.# AVR (Uno, Nano, Mega) avr-nm -C -S --size-sort -r build/MySketch.ino.elf | head -n 20 # ESP32 xtensa-esp32-elf-nm -C -S --size-sort -r build/MySketch.ino.elf | head -n 20 # ESP-IDF idf.py size-components -
Remove debug and heavy code
Wrap debug messages in a macro so they are removed at compile time. Avoid
Stringandfloatformatting on AVR, and prefer lighter libraries (for example U8x8 instead of U8g2 graphics mode for plain text).#define DEBUG 0 #if DEBUG #define DBG(x) Serial.println(x) #else #define DBG(x) #endif void setup() { DBG(F("starting")); // removed entirely when DEBUG is 0 } void loop() {} -
If it is RAM: F() and PROGMEM
On AVR, every string literal is copied to RAM at startup.
F()keepsSerial.printtexts in flash, andPROGMEMdoes the same for constant arrays, read withpgm_read_byte(). Note: this frees RAM but does not shrink the flash size. On ESP32,constdata already stays in flash.#include <avr/pgmspace.h> const uint8_t sineTable[] PROGMEM = {128, 153, 177, 199, 218, 234, 245, 253, 255}; void setup() { Serial.begin(9600); Serial.println(F("This text stays in flash")); uint8_t v = pgm_read_byte(&sineTable[3]); Serial.println(v); } void loop() {} -
ESP32: change the partition scheme
In the Arduino IDE, Tools > Partition Scheme > "Huge APP (3MB No OTA/1MB SPIFFS)" or "Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)". Note: "Huge APP" removes OTA updates, and changing partitions wipes the file system content (SPIFFS or LittleFS) on the next flash. Back up your files first.
# Arduino CLI arduino-cli compile --fqbn esp32:esp32:esp32:PartitionScheme=huge_app MySketch # PlatformIO (platformio.ini) # board_build.partitions = huge_app.csv # ESP-IDF: Partition Table > "Single factory app (large), no OTA" idf.py menuconfig -
Move to a bigger board
If the project really needs all that code, switch boards: a Mega 2560 offers 253,952 bytes of flash and 8 KB of RAM, an ESP32-S3 with 8 or 16 MB of flash leaves much more room. Do not pick a Nano's new bootloader option just to gain 2 KB if the board has the old one: the upload will fail.
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Watch the percentages shown on every compile and react from 80%: you also need free RAM for the stack.
- Get used to
F()for every fixed text on AVR from the start of the project. - Choose the ESP32 partition scheme at the start of the project, before you have data in the file system.
Frequently asked questions
Does the F() macro reduce sketch size?
No. It saves RAM by keeping strings in flash, but the flash size stays about the same. For "Sketch too big", you must remove or slim down code.
What is the maximum sketch size on an Arduino Uno?
32,256 bytes of flash (32 KB minus the 512-byte bootloader) and 2,048 bytes of RAM.
Why does my 4 MB ESP32 reject a 1.4 MB sketch?
The default scheme splits the flash into two 1.25 MB app partitions (for OTA) plus a file system. Pick a scheme with a larger app partition.
Useful tools for this error
More Electronics & Arduino errors
- Fix "avrdude: stk500_getsync(): not in sync" on Arduino
- Fix "Failed to connect to ESP32" (Wrong boot mode, No serial data received)
- Fix "Brownout detector was triggered" on ESP32
- Fix "can't open device /dev/ttyUSB0: Permission denied" (Arduino, ESP32)
- Fix "Guru Meditation Error: Core 1 panic'ed (LoadProhibited)" on ESP32
- Fix "error: 'x' was not declared in this scope" (Arduino, C++)
- Fix "OSError: [Errno 121] Remote I/O error" with I2C on Raspberry Pi