Need help freeing memory from unused modules

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
oserror
Posts: 33
Joined: Tue Feb 12, 2019 12:26 am

Re: Need help freeing memory from unused modules

Post by oserror » Thu Jun 20, 2019 1:02 am

I let my program run for a couple of weeks while some of the parts from China arrived in the mail. I seem to have enough memory to run the blinklib.py library and do a ssl request to Thingspeak. Most of the time.

My program runs for about 10 seconds and goes into deep sleep for about 10 minutes, then repeats. It seems like the memory is getting fragmented from this behaviour. I am unloading the modules from memory each time they are used and running gc.collect(). I guess the garbage collection isn't guaranteed, but I thought that the soft reset would take care of the rest. This does not seem to be the case.

I simply unplugged the board from the usb cable and plugged it back in and everything is fine. I also just upgraded to MicroPython 1.11. The board is a Wemos D1 Pro -- I have the firmware loaded to the 4MB boundary in the flash. I think this isn't relevant in my case since I have enough storage but I'm throwing that out there in case I misunderstand.

Is there something i can do to prevent this? Is this a known issue with the ESP8266?

Thanks!

Christian Walther
Posts: 56
Joined: Fri Aug 19, 2016 11:55 am

Re: Need help freeing memory from unused modules

Post by Christian Walther » Thu Jun 20, 2019 11:54 am

oserror wrote:
Thu Jun 20, 2019 1:02 am
My program runs for about 10 seconds and goes into deep sleep for about 10 minutes, then repeats. It seems like the memory is getting fragmented from this behaviour.
I would consider that unlikely. The wake from deep sleep is a reset, so you are starting with a fresh heap every time. Does micropython.mem_info() provide any evidence for progressive fragmentation?

User avatar
oserror
Posts: 33
Joined: Tue Feb 12, 2019 12:26 am

Re: Need help freeing memory from unused modules

Post by oserror » Thu Jun 20, 2019 1:11 pm

I can try that. I am in a little over my head. :) What sort of things would I be looking for there? When should I run that method? When the board is unstable?

I ran the code overnight on my breadboard and there was no error. It ran for about 12 hours. I do a small check by writing the time to a file and comparing it to the datafile produced at the beginning of the program. It matched every time.

What I saw yesterday was sluggishness, followed by a series of resets until I unplugged the board. It would run through the beginning of my program and then when it loaded the blynk object from blynklib, it would fail. I have a little less than 21 Kbytes of memory free before this step. I have considered freezing some of my modules and will look into that in the next few days. I have compiled esp8266-open-sdk -- I just have to do the rest of the steps.

I didn't see an error before until at least a few days had passed. The only thing I did to interrupt it was to hit the reset button on the board and use rshell. I think I changed a couple of things but all of these changes are now reflected in the current configuation as I wiped the board to install MicroPython 1.11.

I guess at this point I can let it run for a few days and wait for an error if and when one occurs. I have the board powered by a USB port on my computer so I think it's getting enough juice. There is a BME280 sensor connected and nothing else.

User avatar
oserror
Posts: 33
Joined: Tue Feb 12, 2019 12:26 am

Re: Need help freeing memory from unused modules

Post by oserror » Thu Jun 20, 2019 7:06 pm

Okay, one thing I'm going to try is to disconnect it from my USB3 hub on my monitor. I had an issue with a Wemos D1 mini with a PIR sensor sending the state of the sensor to my Home Assistant software. Periodically, it would crash. I solved the problem by connecting the device to a power supply, not a serial port, and the periodic crashes have gone away.

I am not sure it will work but maybe that's the reason? I turn this computer off and on -- the monitor sends power all the time -- but when this machine comes on the port is recognized as /dev/ttyUSB0. I'm just guessing at this point since I am not certain what is involved in the lines of communication when a serial port is discovered by Linux. Bear in mind that in a regular day I would turn this machine off and on at least 5 or 6 times.

So I put the Wemos on a Raspberry Pi, and I opened up Picocom in tmux and let the session run. I have the Pi connected to my UPS (we have electrical storms this time of year but not just yet fortunately) so the power should be okay. I also secured my breadboard in the keyboard drawer in case of feline interference. I don't have a control group but maybe this is a good baseline. Errors will be logged.

Anyway, if anyone cares, I'll report back in a few days. :)

Post Reply