Python memory usage of numpy arrays
You can use array.nbytes
for numpy arrays, for example:
>>> import numpy as np>>> from sys import getsizeof>>> a = [0] * 1024>>> b = np.array(a)>>> getsizeof(a)8264>>> b.nbytes8192
In python notebooks I often want to filter out 'dangling' numpy.ndarray
's, in particular the ones that are stored in _1
, _2
, etc that were never really meant to stay alive.
I use this code to get a listing of all of them and their size.
Not sure if locals()
or globals()
is better here.
import sysimport numpyfrom humanize import naturalsizefor size, name in sorted( (value.nbytes, name) for name, value in locals().items() if isinstance(value, numpy.ndarray)): print("{:>30}: {:>8}".format(name, naturalsize(size)))