Memory Usage Issue

I’m fairly new to this so could be ignorance but I seem to be running out of memory on the esp device: esp8266ex 4M (see below).

I can get code to run by commenting a switch, un-comment and fails with dump (all zeros).

The device is reporting 4Mbytes, but the file size I’m generating seems way less than that. I’ve trimmed code, removed the web sever stuff but still get issue.

Any ideas, am I missing ob?

--------- Arduino Mem tool reports:
Flash real id:   0016405E
Flash real size: 4194304 bytes

Flash ide  size: 4194304 bytes
Flash ide speed: 40000000 Hz
Flash ide mode:  DOUT
Flash Chip configuration ok.

---------- EspHome Compile:
RAM:   [=====     ]  49.6% (used 40652 bytes from 81920 bytes)
Flash: [=====     ]  46.3% (used 483792 bytes from 1044464 bytes)
Building /data/moisture_sensor_prototype/.pioenvs/moisture_sensor_prototype/firmware.bin
Creating BIN file "/data/moisture_sensor_prototype/.pioenvs/moisture_sensor_prototype/firmware.bin" using "/data/moisture_sensor_prototype/.pioenvs/moisture_sensor_prototype/firmware.elf"

--------- Upload Info
Files size:
Size:    476 KB (487,952 bytes)
On Disk: 480 KB (491,520 bytes)

Using 'COM7' as serial port.
Connecting....
Detecting chip type... ESP8266
Connecting....

Chip Info:
 - Chip Family: ESP8266
 - Chip Model: ESP8266EX
 - Chip ID: 00D163B3
 - MAC Address: F4:CF:A2:D1:63:B3
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
 - Flash Size: 4MB
 - Flash Mode: dout
 - Flash Frequency: 40MHz
Erasing flash (this may take a while)..

Using 'COM7' as serial port.
)Writing at 0x0004c000... (100 %)Wrote 487952 bytes (326162 compressed) at 0x00000000 in 7.3 seconds (effective 534.4 kbit/s)...
Hash of data verified.

What does this mean?

To clarify, I’m not asking for help in debugging compilation issue but rather best practices for reducing the file size.

Simply removing the restart switch was enough to have it work, so I figured removing 2 charsets would free up a ton, but results were same. Then removed the web server component, same again, so not sure where it’s all going.

Just to clarify, you can’t be sure if your files are too big by referring to

But the web server is often the biggest saving. In fact the documentation warns against that.

The source and all the libs that went into compiling are all left behind I think. Maybe take a look in the build directory at sizes?

From the upload output it looks like it’s only sending over 0.4MB, nowhere near the 4MB allowed, unless of course I’m totally reading the log/values incorrectly.

Also I noticed that even tho the web server is not enabled it seemed to compile some ws files (that may be normal).
Compiling /data/moisture_sensor_prototype/.pioenvs/moisture_sensor_prototype/src/esphome/components/web_server_base/web_server_base.cpp.o

This is my repo, if interested:

I don’t think that size firmware is a problem at all. In fact it appears that the firmware was uploaded OK.

If you think there are bit of an old dependency left in there is might pay to do a clean build, it is an option in the esphome gui.

Yea, tried clean build. Since adding/ removing large chunks didn’t seem to make much diff I’m wondering if it’s got more to do with which memory space is being used. Eg am i using to many globals, or due to logging in lambda am i pulling in c string libs,

I’m going to do some pruning along those lines

Found this interesting doc on esp mem management. I use a lot of strings!

Printing these values when the program runs gives (in DEC):

[14:40:44]getFreeHeap:           19184
[14:40:44]getHeapFragmentation:  11%
[14:40:44]getMaxFreeBlockSize:   17208
[14:40:44]getSketchSize:         485824
[14:40:44]getFreeSketchSpace:    3686400
[14:40:44]getFreeSketchSpace:    3686400

These look ok I think, Must be my code somewhere is crapping on memory. Probably one of lambda calls!

I’m fairly sure (ha!) I was having memory issue before introducing code below, regardless this is definitely causing my crash/reboot issue. It seems to behave differently as I commented/un-commented code leading to thinking memory was being corrupted, or running out.

The snippet below causes either reboot loop, or what looks like core dump, depending other code enabled. I think it’s either both the ‘on_value’ and ‘on_raw_value’ competing for the led, or race condition??

Either way removing the on_raw_value and having extra ‘else’ fixes it. So marking as Solved.!

on_value:
  then:
    - lambda: |-
        // ESP_LOGD("main", "VA2 val (x): %f", x );

        // Only check for values if the sensor is showing as connected
        if ( id(adc_va2_connected) )
        {
          if (x < id(danger_threshold) )
          {
            auto call = id(va2_status_led).turn_on();
            call.set_brightness(id(led_max_brightness));
            call.set_effect("Danger Will Robinson");
            call.perform();  
          } else
          {
            auto call = id(va2_status_led).turn_on();
            call.set_brightness(id(led_max_brightness));
            call.set_effect("none");
            call.set_rgb( (100.0-x)/100.0, x/100.0, 0.0 );
            call.perform();  
          }
        }
on_raw_value:
  then:
    lambda: |-
      // If the sensor has been disconnected then turn off the led and set flag.
      if ( x < id(ads1115_nc_value) ){
        id(adc_va2_connected) = false;
        auto call = id(va2_status_led).turn_off();
        call.perform();  
      } else {
        id(adc_va2_connected) = true;
      }