adafruit / Adafruit_CircuitPython_PortalBase

Base Library for the Portal-style libraries.
MIT License
18 stars 17 forks source link

MemoryError on second call to PyPortal.fetch() #85

Closed eloekset closed 1 year ago

eloekset commented 1 year ago

https://github.com/adafruit/Adafruit_CircuitPython_PortalBase/blob/ef951d5b5117d20c45eeb593f9ef6bc936148ae8/adafruit_portalbase/network.py#L684

For some reason json_transform(json_data) gets called on the second call to PyPortal.fetch() and it fails with MemoryError every time that happens. The first call to PyPortal.fetch() never fails, and all calls should get about the same response.

My PyPortal project is published on GitHub and this branch is the version running when reporting this issue: https://github.com/blog-eivindgl-com/netatmo-pyportal-display/tree/report_issue_to_adafruit

The response doesn't contain much json data, and I don't specify any JSON path to be processed when initializing the PyPortal object. image

image image

eloekset commented 1 year ago

I've tried commenting out the part that calls json_transform from the adafruit_portalbase/network library, but then realized that this library is included in CircuitPython 7.X and the network.mpy file in the lib folder is ignored. So I don't know how to modify that module. image

Since it's a MemoryError, I added calls to gc.mem_free() on every iteration to monitor if there's a memory leak. But the log shows that free memory goes both up and down and it's much higher than the number of bytes being allocated when the application crashes. image

eloekset commented 1 year ago

I still haven’t solved the MemoryError, but I’ve updated the issue I linked to from my project with several tips from the documentation about memory allocation failed. It’s most likely the use of fonts and bitmaps that causes the error, and it can most likely be fixed by calling gc.collect() on several places among other things.

makermelissa commented 1 year ago

Hi, thanks for finding this. If you want to override a frozen library, you can place it in the root of your CIRCUITPY drive.

eloekset commented 1 year ago

Thanks @makermelissa! I didn't try to skip json_transform anyway, but I followed a YouTube video where @FoamyGuy is investigating a similar issue. I took two of the tips from that video and now my issue is solved. I documented the process in a pull request in my project: https://github.com/blog-eivindgl-com/netatmo-pyportal-display/pull/8

You can close this issue if you want. I don't think it's relevant for the MemoryError at least.