ram shortage

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

ram shortage

Post by KJM » Sun Mar 13, 2022 1:06 am

I use urequests to download code.mpy an ~12k file. It downloads OK & I write it to flash. But when I then try to use urequests again it crashes at s=ssl.wrap_socket(s, server_hostname=host) with

Code: Select all

line 195 OSError: [Errno 12] ENOMEM
not enough ram.
So I decided to see if I could free up the ~12k of ram, I tried my idea in the repl

Code: Select all

>>> import gc; gc.collect(); gc.mem_free()
110176
>>> with open('code.mpy', 'r') as f: tram=f.read()
... 
>>> import gc; gc.collect(); gc.mem_free()
98112
>>> tram=0
>>> import gc; gc.collect(); gc.mem_free()
109472
loaded the file into ram, re-assigned the ram variable to something smaller, got most of the 11k back.

Suitably emboldened I tried the idea in my user code, re-assigning the ram I'd used to hold code.mpy prior to writing it to flash.

Code: Select all

import gc; gc.collect(); print('free mem before', gc.mem_free())
    tram=0
    gc.collect(); print('free mem after', gc.mem_free()) 
sadly, a different result this time

Code: Select all

free mem before 76304
free mem after 76308
Can someone please enlighten me why an idea that worked in the repl doesn't work in user code? I'm also interested to know why 76k of ram is not enough for ssl to work?!

Post Reply