Memory bloat for ESP32

I noticed that the memory usage for an ESP32-S3 project I'm working on seemed really high. So as an experiment I created just a "Hello World" example and it clocks in at:

Sketch uses 332889 bytes (23%) of program storage space. Maximum is 1441792 bytes.

The code is below and is as dirt simple as I could make it, yet something is taking up almost a quarter of the enormous memory of the ESP32, presumably extra cruft of included libraries, etc. Is there any way to trim down the fat and keep code that I don't need from getting compiled/linked in?

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("Hello World!");
}

void loop() {
  // put your main code here, to run repeatedly:
}

The ESP32 is a complex chip with a lot of runtime support. The extra code is the manufacturers run time support. If you look at the compile listing you can see all the core modules included.

1 Like

if you run short of program storage space (e.g. if using WiFi and BLE in same program) you can under the Tools menu change the Partition Scheme to Hugh App

That's kind of my point (and honestly a complaint I've had with the Arduino ecosystem for a while). If a feature/module/peripheral isn't being referenced in the active code or needed for general support then there is no good reason to include it in (and lots of good reasons other than space to exclude it from) the binary. I tried the experiment again with nothing but an empty setup() and an empty loop() function and it still clocks in at 332,745 bytes (23%) of program storage space. That's orders of magnitude more memory than most of the microcontrollers I've used over the past two decades for what essentially boils down to a for(;;); loop. Even being generous and allowing for initialization code for things like clocking and disabling hardware that powers on by default, I struggle to see how that would take more than a few hundred instructions.

The Arduino runtime package for ESP32 is supplied by the Espressif Corporation. The only thing you can do about that is start from scratch and write your own support package.

However, if your application fits into memory and does what you want, why should you care?

3 Likes

Then contact espresiff and suggest they tidy up their core.

1 Like

Let's be fair. This is not unique to Espressif. In the case of many boards which claim Arduino support, the Arduino abstraction layer is just an encrustation on a whole stack of other abstraction layers, many layers away from the real hardware. This is all thrown in when you compile even the simplest sketch.

Just tried an empty sketch with ESP32S3 Dev Module ("esp32" v3.0.7) and got

Sketch uses 265657 bytes (20%) of program storage space. Maximum is 1310720 bytes.
Global variables use 17888 bytes (5%) of dynamic memory, leaving 309792 bytes for local variables. Maximum is 327680 bytes.

Still a lot more than a few hundred instructions, but a good chunk less than 332,745. Almost 18KB in global variables....

The ESP32 core is built on top of FreeRTOS. The main loop runs as a separate task.

1 Like

Top large functions (everything over 768 bytes) in an ESP32 build of Blink.ino:

4009169c 0000030b T esp_sha_dma
40028dec 00000313 T rtc_clk_cal_internal
40086228 00000313 T uart_set_pin
4002e074 00000318 T tlsf_free
4008c258 0000032c T nvs::Page::mLoadEntryTable()
40025594 0000032e t rmt_tx_default_isr
40090bdc 0000033e T esp_mmu_map
40093908 00000386 t search_object
40094050 000003aa T _fseeko_r
400907a8 000003b3 T esp_mmu_map_init
40089a00 000003b4 T esp_intr_alloc_intrstatus
4008f298 000003ec T esp_core_dump_get_task_regs_dump
4002e694 000003f7 T tlsf_realloc
40084a7c 0000041b T rmt_new_rx_channel
40082a44 00000441 T printBeforeSetupInfo()
3f0046a8 000004d0 D rtc_io_desc
40094af8 000004d2 t get_arg$constprop$0
40098044 000004d2 t get_arg$constprop$0
4009d204 000004d2 t get_arg$constprop$0
4009f540 000004d2 t get_arg$constprop$0
40085378 00000530 T rmt_new_tx_channel
4008a398 00000579 T rtc_init
4008e924 000005e3 t esp_core_dump_do_write_elf_pass
4008f6f0 00000694 t diag_log_add
3f007d18 000006f0 d esp_err_msg_table
3ffc2c2c 00000764 b s_coredump_stack
3ffc09c0 00000830 D port_IntStack
4009ba9c 00000d40 T _dtoa_r
4009d7e8 00001d56 T _svfiprintf_r
4009fa38 00001ee4 T _vfiprintf_r
40094fcc 00002fe6 T _svfprintf_r
40098518 00003126 T _vfprintf_r

43k of printf() implementation(s) !

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.