[python] Checking the memory used by an object
library python
Published : 2020-05-30   Lastmod : 2021-11-15

sys.getsizeof(a)

You may not be aware of memory usage in python, but here is a way to check memory usage per object.

github

  • You can find the file in jupyter notebook format on github here .

google colaboratory

  • To run it in google colaboratory here 002/002_nb.ipynb)

Author’s environment

sw_vers
ProductName: Mac OS X
ProductName: Mac OS X
ProductVersion: 10.14.6
BuildVersion: 18G95
Python -V
Python 3.7.3
import sys

a = [i ** 2 for i in range(10000)].

print('(Print the first 10) a :'',a[0:10])
print('Memory used for a :', sys.getsizeof(a))
(Print the first 10) a : [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].
Memory used for a : 87624

Related Articles