23

Does anybody know how much memory is used by a numpy ndarray? (with let's say 10,000,000 float elements).

mskfisher
  • 3,291
  • 4
  • 35
  • 48
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • Does this answer your question? [Python memory usage of numpy arrays](https://stackoverflow.com/questions/11784329/python-memory-usage-of-numpy-arrays) – YaOzI Jan 13 '23 at 04:36

3 Answers3

39

The array is simply stored in one consecutive block in memory. Assuming by "float" you mean standard double precision floating point numbers, then the array will need 8 bytes per element.

In general, you can simply query the nbytes attribute for the total memory requirement of an array, and itemsize for the size of a single element in bytes:

>>> a = numpy.arange(1000.0)
>>> a.nbytes
8000
>>> a.itemsize
8

In addtion to the actual array data, there will also be a small data structure containing the meta-information on the array. Especially for large arrays, the size of this data structure is negligible.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • Thanks especially the two properties help a lot. – Michel Keijzers Feb 22 '12 at 13:52
  • 1
    `nbytes` doesn't seem to work correctly with broadcasted arrays. For instance, `np.broadcast_to(np.arange(1000), (int(1e15), 1000)).nbytes == 8e18`, which probably requires more RAM than exists on Earth. – Mateen Ulhaq Feb 07 '20 at 11:49
3

To get the total memory footprint of the NumPy array in bytes, including the metadata, you can use Python's sys.getsizeof() function:

import sys
import numpy as np

a = np.arange(1000.0)

sys.getsizeof(a)

8104 bytes is the result

sys.getsizeof() works for any Python object. It reports the internal memory allocation, not necessarily the memory footprint of the object once it is written out to some file format. Sometimes it is wildly misleading. For example, with 2d arrays, it doesn't dig into the memory footprint of vector.

See the docs here. Ned Batcheldor shares caveats with using sys.getsizeof() here.

jeffhale
  • 3,759
  • 7
  • 40
  • 56
0

I gauss, easily, we can compute by print(a.size // 1024 // 1024, a.dtype) it is similar to how much MB is uesd, however with the param dtype, float=8B, int8=1B ...

insight
  • 19
  • 2