Memory problems while serving web pages

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
janol
Posts: 14
Joined: Wed Oct 02, 2019 7:52 am

Memory problems while serving web pages

Post by janol » Mon Mar 08, 2021 11:26 am

My project is to build a general purpose relay controller based on input from various sensors. I want to see the current values of the sensors and be able to enter "on" and "off" values for the various sensors via web server. All of it runs on an esp32. And it now works -- reasonably well (with the current limitation that I can only activate one sensor at a time). My problem is that I get "MemoryError: memory allocation failed, allocating 7103 bytes" after some time of playing around with the webinterface. I have around 67000 of free memory and it seems rather stable until it fails. When I get the error it invariably happens when I build the html string to reload the full the web page. The page is very simple -- basically a table with sensor values and on off values -- but because of <style> instructions and javascripts the page is around 8kB to load.

As I have no real experience in how to do these things I'm just inquiring about possible best practice. From posts on the forum I understand that my problem probably is due to fragmentation of memory so that there is no 8kB of consecutive memory available.
  • To reduce my html string, I was thinking that I should put the java scripts and style sheet in separate files that gets called from my html file (like I would on an apache server) but I have not found how to do this with my websocket in micropython.
  • As an alternative to the above, I also wonder if it would be better (since my visible page is quite small) to move the javascript logic to the python code instead to reduce the html string accordingly.
  • But I would like to keep the use of js ajax to get the updated sensor readings (so I don't get a flickering web page). Is this polling a likely source of the problem. My own testing is very inconclusive.
  • The relay itself gets updated periodically with a machine.timer. Is this something that can clash with the websocket handler?

Any advice or pointers would be much appreciated.

/Fredrik

viiveli
Posts: 5
Joined: Sat Jan 09, 2021 5:53 pm
Location: Finland
Contact:

Re: Memory problems while serving web pages

Post by viiveli » Mon Mar 08, 2021 11:51 am

Do you have garbage collection enabled? That could help with memory issues. If not, pop this into your boot.py:

Code: Select all

import gc
gc.enable()

janol
Posts: 14
Joined: Wed Oct 02, 2019 7:52 am

Re: Memory problems while serving web pages

Post by janol » Mon Mar 08, 2021 1:33 pm

Yes, I found this command but hadn't realized that I only need to invoke it once. Now I do a gc.collect() after each time I have sent the webpage. Thanks. (I also do "del htmlstring" after it has been sent.)

Post Reply