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
How to tell ram/rom usage?
Re: How to tell ram/rom usage?
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))
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to tell ram/rom usage?
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.
Index to my micropython libraries.
-
- Posts: 52
- Joined: Thu Apr 20, 2017 9:19 pm
Re: How to tell ram/rom usage?
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?
-
- Posts: 52
- Joined: Thu Apr 20, 2017 9:19 pm
Re: How to tell ram/rom usage?
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
import machine
instead of:
from machine import UART