How to tell ram/rom usage?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

How to tell ram/rom usage?

Post by eradicatore » Fri Jun 23, 2017 5:45 am

Hi,
I must not be searching for the right wording, because I can't find a good way to determine the ram/rom left on a nodeMCU. I looked at the os and machine imports but don't see anything there that looks right. Anyone know a good way?

Justin

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: How to tell ram/rom usage?

Post by devnull » Fri Jun 23, 2017 8:15 am

df() (disk-free) shows free file space, free() shows available ram.

Code: Select all

import gc
import os

def df():
  s = os.statvfs('//')
  return ('{0} MB'.format((s[0]*s[3])/1048576))

def free(full=False):
  F = gc.mem_free()
  A = gc.mem_alloc()
  T = F+A
  P = '{0:.2f}%'.format(F/T*100)
  if not full: return P
  else : return ('Total:{0} Free:{1} ({2})'.format(T,F,P))

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How to tell ram/rom usage?

Post by pythoncoder » Fri Jun 23, 2017 9:03 am

Neat! Forcing a garbage collection might give a more accurate figure in cases where unreferenced objects remain on the heap:

Code: Select all

def free(full=False):
  gc.collect()
  F = gc.mem_free()
  A = gc.mem_alloc()
  T = F+A
  P = '{0:.2f}%'.format(F/T*100)
  if not full: return P
  else : return ('Total:{0} Free:{1} ({2})'.format(T,F,P))
Peter Hinch
Index to my micropython libraries.

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: How to tell ram/rom usage?

Post by eradicatore » Sun Jun 25, 2017 11:12 pm

devnull wrote:df() (disk-free) shows free file space, free() shows available ram.

Thanks so much @devnull This seems to work great, I'll start keeping an eye on things and see how they change over development of my project. At one point I did crash just outright when I was importing modules, so I started cleaning up my imports and calling "collect" when I thought they should be done being used.

Question, do you know if doing "from machine import UART" helps reduce the memory usage? Like not importing the whole module?

eradicatore
Posts: 52
Joined: Thu Apr 20, 2017 9:19 pm

Re: How to tell ram/rom usage?

Post by eradicatore » Sun Jun 25, 2017 11:36 pm

Ok, read up (https://bytes.com/topic/python/answers/ ... mory-usage and others) and I think it's better to just call the full import:

import machine

instead of:

from machine import UART

Post Reply