stdin memory leak

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
aviator_11
Posts: 7
Joined: Tue Apr 26, 2022 8:36 pm

Re: stdin memory leak

Post by aviator_11 » Wed Apr 27, 2022 7:09 pm

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.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: stdin memory leak

Post by Roberthh » Wed Apr 27, 2022 7:30 pm

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.

aviator_11
Posts: 7
Joined: Tue Apr 26, 2022 8:36 pm

Re: stdin memory leak

Post by aviator_11 » Wed Apr 27, 2022 9:37 pm

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())

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: stdin memory leak

Post by Roberthh » Thu Apr 28, 2022 6:19 am

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.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: stdin memory leak

Post by scruss » Thu Apr 28, 2022 7:31 pm

As an aside:

Code: Select all

        return b.decode('ascii')
decodes to UTF-8. That's all that MicroPython understands. The argument string is currently ignored

Post Reply