ram usage

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

ram usage

Post by smhodge » Thu Apr 02, 2020 9:16 pm

I have been using mem_info() to look at memory usage, but that prints to the USB connection. Is there a way to access the same info from within my code?

pagano.paganino
Posts: 89
Joined: Fri Sep 11, 2015 10:47 pm
Location: Italy

Re: ram usage

Post by pagano.paganino » Thu Apr 02, 2020 11:06 pm

You can try this:

Code: Select all

import io
import os

class DUP(io.IOBase):

    def __init__(self, s):
        self.s = s

    def write(self, data):
        self.s += data
        return len(data)

    def readinto(self, data):
        return 0

s = bytearray()
os.dupterm(DUP(s))
machine.info(1)
os.dupterm(None)

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ram usage

Post by jimmo » Fri Apr 03, 2020 1:06 am

The main useful stats from micropython.mem_info() is the "GC: total: 2072832, used: 3264, free: 2069568" line.
You can get these three numbers using "gc.mem_alloc()" and "gc.mem_free()"

But if you want the other stats, then yeah the dupterm idea works nicely :)

smhodge
Posts: 86
Joined: Tue Jan 22, 2019 2:16 am
Location: Kirkland, WA, USA

Re: ram usage

Post by smhodge » Fri Apr 03, 2020 2:32 am

Those 3 numbers are all I'm looking for. Thanks

Post Reply