Python memory usage of numpy arrays

走远了吗. 提交于 2019-11-26 08:07:49

问题


I\'m using python to analyse some large files and I\'m running into memory issues, so I\'ve been using sys.getsizeof() to try and keep track of the usage, but it\'s behaviour with numpy arrays is bizarre. Here\'s an example involving a map of albedos that I\'m having to open:

>>> import numpy as np
>>> import struct
>>> from sys import getsizeof
>>> f = open(\'Albedo_map.assoc\', \'rb\')
>>> getsizeof(f)
144
>>> albedo = struct.unpack(\'%df\' % (7200*3600), f.read(7200*3600*4))
>>> getsizeof(albedo)
207360056
>>> albedo = np.array(albedo).reshape(3600,7200)
>>> getsizeof(albedo)
80

Well the data\'s still there, but the size of the object, a 3600x7200 pixel map, has gone from ~200 Mb to 80 bytes. I\'d like to hope that my memory issues are over and just convert everything to numpy arrays, but I feel that this behaviour, if true, would in some way violate some law of information theory or thermodynamics, or something, so I\'m inclined to believe that getsizeof() doesn\'t work with numpy arrays. Any ideas?


回答1:


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.nbytes
8192



回答2:


The field nbytes will give you the size in bytes of all the elements of the array in a numpy.array:

size_in_bytes = my_numpy_array.nbytes

Notice that this does not measures "non-element attributes of the array object" so the actual size in bytes can be a few bytes larger than this.




回答3:


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 sys
import numpy
from humanize import naturalsize

for 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)))


来源:https://stackoverflow.com/questions/11784329/python-memory-usage-of-numpy-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!