Bug: Running out of memory, fix/cut things loading up into memory #22

Closed
opened 2025-11-09 10:59:04 -05:00 by sickprodigy · 3 comments
Owner

alright i got this error after trying to temp hold new settings:
Error loading page

memory allocation failed, allocating 14032 bytes

from log:

==================================================
Schedule Applied: Morning
==================================================
AC Target:      75.0��F
Heater Target:  72.0��F
==================================================

AC cooldown: 0s remaining before can turn on
DEBUG: Generating status page...
DEBUG: Sending response (13287 bytes)
DEBUG: Response sent successfully
DEBUG: Client connection closed
AC turned ON
DEBUG POST: Content-Length = 46 bytes
DEBUG POST: Already read = 19 bytes
DEBUG POST: Still need = 27 bytes
DEBUG POST: Read 27 bytes (total: 27/27)
DEBUG POST: Read additional 27 bytes (expected 27)
DEBUG POST: Final body length = 46 bytes (expected 46)
DEBUG POST: First 100 chars = heater_target=72.0&ac_target=73&hold_type=temp
AC target updated to 73.0��F
Heater target updated to 72.0��F
������ Temporary hold activated - Manual override (1 hour)
Schedule configuration reloaded
DEBUG: Saving config with 4 schedules
Settings saved to config.json
Settings persisted to disk
��� Config reloaded into memory
DEBUG: Generating status page...
Error generating page: memory allocation failed, allocating 14032 bytes
Traceback (most recent call last):
  File "scripts/web_server.py", line 1106, in _get_status_page
MemoryError: memory allocation failed, allocating 14032 bytes
DEBUG: Sending response (112 bytes)
DEBUG: Response sent successfully
DEBUG: Client connection closed


==================================================
Shutting down gracefully...
==================================================
Turning off AC...
AC minimum runtime: 7s remaining before can turn off
Turning off heater...
Turning off LED...
Shutdown complete!
==================================================
alright i got this error after trying to temp hold new settings: Error loading page memory allocation failed, allocating 14032 bytes from log: ``` ================================================== Schedule Applied: Morning ================================================== AC Target: 75.0��F Heater Target: 72.0��F ================================================== AC cooldown: 0s remaining before can turn on DEBUG: Generating status page... DEBUG: Sending response (13287 bytes) DEBUG: Response sent successfully DEBUG: Client connection closed AC turned ON DEBUG POST: Content-Length = 46 bytes DEBUG POST: Already read = 19 bytes DEBUG POST: Still need = 27 bytes DEBUG POST: Read 27 bytes (total: 27/27) DEBUG POST: Read additional 27 bytes (expected 27) DEBUG POST: Final body length = 46 bytes (expected 46) DEBUG POST: First 100 chars = heater_target=72.0&ac_target=73&hold_type=temp AC target updated to 73.0��F Heater target updated to 72.0��F ������ Temporary hold activated - Manual override (1 hour) Schedule configuration reloaded DEBUG: Saving config with 4 schedules Settings saved to config.json Settings persisted to disk ��� Config reloaded into memory DEBUG: Generating status page... Error generating page: memory allocation failed, allocating 14032 bytes Traceback (most recent call last): File "scripts/web_server.py", line 1106, in _get_status_page MemoryError: memory allocation failed, allocating 14032 bytes DEBUG: Sending response (112 bytes) DEBUG: Response sent successfully DEBUG: Client connection closed ================================================== Shutting down gracefully... ================================================== Turning off AC... AC minimum runtime: 7s remaining before can turn off Turning off heater... Turning off LED... Shutdown complete! ================================================== ```
Author
Owner

The fix for now:

    # ===== FORCE GARBAGE COLLECTION BEFORE BIG ALLOCATION =====
    import gc
    gc.collect()
    print("DEBUG: Memory freed, {} bytes available".format(gc.mem_free()))
    # ===== END GARBAGE COLLECTION =====
The fix for now: # ===== FORCE GARBAGE COLLECTION BEFORE BIG ALLOCATION ===== import gc gc.collect() print("DEBUG: Memory freed, {} bytes available".format(gc.mem_free())) # ===== END GARBAGE COLLECTION =====
Author
Owner

So i'm thinkin soon i will run out of memory/ram. It would be light weight to just code in an api and have a server/pc link to the pi.

Will need to host html files on local computer/server in order to view web page configurations. It's just too much to put on a pico with 250kb of ram. 70kb to load current full application. Not quite sure how to break it down I just think python loads into memory/ram and then runs from there. Once i figure out the way around it could be less restrictive

Pico W Memory Specs:
RAM (SRAM):
Total: 264 KB (264,192 bytes)
Available for MicroPython: ~190-220 KB (rest used by system)
Your current usage: Generating 14KB HTML pages + all your code/objects
Flash Storage:
Total: 2 MB
Used for: MicroPython firmware (~1.4 MB) + your scripts + config files
Available: ~600 KB for your code
Why You're Running Out of RAM:

  1. Large HTML Strings (~14KB each)
    When generating a page, Python must:

Load the entire HTML template
Format it with variables
Convert to bytes
Send chunks over socket
Problem: The string exists in memory 2-3 times during this process!

  1. Multiple Objects in Memory:
  2. Garbage Collection Not Aggressive Enough:
    MicroPython's GC doesn't run automatically as often as CPython. You need to manually call gc.collect() frequently.

Memory Saving Solutions:
Immediate Fixes (Already Done):
Added gc.collect() before page generation
Minified HTML (removed whitespace)
Additional Optimizations Needed:
Web Server Optimization - Stream HTML Instead of Building Full String:
Reduce Config Storage:
Remove extra whitespace - saves a few bytes when loaded into RAM.

Quick Memory Check Command:
Add this to see what's using memory:

Bottom Line:
Pico W has ~220KB usable RAM, and you're:

Using ~100KB for code/objects
Using ~14KB for each HTML page generation
Leaving ~100KB free (barely enough)
Solutions in order of priority:

Call gc.collect() more often (every 5 seconds)
Consider splitting settings into smaller pages (less HTML per page)
Use chunked transfer encoding for HTML (advanced)
Remove auto-refresh or increase to 60 seconds (less frequent page loads)
For now, the gc.collect() fix should hold, but if you add more features, you'll need chunked HTML streaming.

So i'm thinkin soon i will run out of memory/ram. It would be light weight to just code in an api and have a server/pc link to the pi. Will need to host html files on local computer/server in order to view web page configurations. It's just too much to put on a pico with 250kb of ram. 70kb to load current full application. Not quite sure how to break it down I just think python loads into memory/ram and then runs from there. Once i figure out the way around it could be less restrictive Pico W Memory Specs: RAM (SRAM): Total: 264 KB (264,192 bytes) Available for MicroPython: ~190-220 KB (rest used by system) Your current usage: Generating 14KB HTML pages + all your code/objects Flash Storage: Total: 2 MB Used for: MicroPython firmware (~1.4 MB) + your scripts + config files Available: ~600 KB for your code Why You're Running Out of RAM: 1. Large HTML Strings (~14KB each) When generating a page, Python must: Load the entire HTML template Format it with variables Convert to bytes Send chunks over socket Problem: The string exists in memory 2-3 times during this process! 2. Multiple Objects in Memory: 3. Garbage Collection Not Aggressive Enough: MicroPython's GC doesn't run automatically as often as CPython. You need to manually call gc.collect() frequently. Memory Saving Solutions: Immediate Fixes (Already Done): ✅ Added gc.collect() before page generation ✅ Minified HTML (removed whitespace) Additional Optimizations Needed: Web Server Optimization - Stream HTML Instead of Building Full String: Reduce Config Storage: Remove extra whitespace - saves a few bytes when loaded into RAM. Quick Memory Check Command: Add this to see what's using memory: Bottom Line: Pico W has ~220KB usable RAM, and you're: Using ~100KB for code/objects Using ~14KB for each HTML page generation Leaving ~100KB free (barely enough) Solutions in order of priority: ✅ Call gc.collect() more often (every 5 seconds) Consider splitting settings into smaller pages (less HTML per page) Use chunked transfer encoding for HTML (advanced) Remove auto-refresh or increase to 60 seconds (less frequent page loads) For now, the gc.collect() fix should hold, but if you add more features, you'll need chunked HTML streaming.
Author
Owner

So we are loading a lot at the top of main.py

from machine import Pin, RTC  # type: ignore
import time  # type: ignore
import network  # type: ignore
import json
import gc  # type: ignore
import sys
# REMOVE THESE - only used once during startup:
# import socket  # type: ignore  
# import struct  # type: ignore
# import ntptime  # type: ignore
So we are loading a lot at the top of main.py ``` from machine import Pin, RTC # type: ignore import time # type: ignore import network # type: ignore import json import gc # type: ignore import sys # REMOVE THESE - only used once during startup: # import socket # type: ignore # import struct # type: ignore # import ntptime # type: ignore ```
sickprodigy changed title from Feature: move web page to server and code in api to Bug: Running out of memory, fix/cut things loading up into memory 2025-11-09 11:24:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: sickprodigy/Auto-Garden#22
No description provided.