stdin memory leak
-
- Posts: 7
- Joined: Tue Apr 26, 2022 8:36 pm
Re: stdin memory leak
Thank you very much for all your advice and insight, I agree. I need to be very aware of memory usage in the functionality I choose to use, especially with strings. Circuitpython must have been releasing memory without me realizing it. And when I introduced a second thread in micropython, the increasing usage of available memory eventually caused the second thread to crash, so the problem became more obvious.
Re: stdin memory leak
The intended memory handling of MicroPyhton and CircuitPython are identical. MicroPython will also automatically release memory when needed. So no action is required unless you want to optimize you timing.
-
- Posts: 7
- Joined: Tue Apr 26, 2022 8:36 pm
Re: stdin memory leak
Do you have any suggestions on good ways to pre-allocate memory for strings or release memory used for string functions? Or is the best method to gc.collect() after I'm done decoding into a string, similar to below?
Code: Select all
import sys
import gc
b = bytearray(1)
def read():
sys.stdin.readinto(b)
return b.decode('ascii')
while True:
read()
#gc.collect()
print (gc.mem_free())
Re: stdin memory leak
Please do not panic about memory handling. Unless you run into a real problem, like allocation exceptions, let the memory manager do it's work.
Re: stdin memory leak
As an aside:
decodes to UTF-8. That's all that MicroPython understands. The argument string is currently ignored
Code: Select all
return b.decode('ascii')