Tell me, is there any function that lets you know how much memory is occupied by a matrix or an array of numpy?
1 answer
You can use the nbytes method:
In [5]: a = np.random.rand(10**6) In [6]: a.nbytes Out[6]: 8000000 In [7]: a.dtype Out[7]: dtype('float64') this also works for multidimensional matrices:
In [11]: a = np.random.randint(0, 10, (10**3, 10**3, 10**3), dtype=np.int8) In [12]: a.shape Out[12]: (1000, 1000, 1000) In [13]: a.nbytes Out[13]: 1000000000 In [14]: a.dtype Out[14]: dtype('int8') You can also use the standard function: sys.getsizeof ()
In [33]: a = np.random.rand(10**6) In [34]: sys.getsizeof(a) Out[34]: 8000096 which will call __sizeof__ for the callee:
In [35]: a.__sizeof__() Out[35]: 8000096 From the documentation:
getsizeof () calls the sizeof __ sizeof __ method adds the additional garbage collector overhead.
- oneIn the simple case,
numpy.arraycan be considered as a piece of memory with the corresponding metadata (type, size, where / what lies)..nbytesreturns the size of a piece of memory (the space occupied by the elements of the array — in the idealized model: perhaps ignoring alignment, auxiliary structures), disregarding the metadata.sys.getsizeof()promises to return the size of the Python object (numpy.array()) in bytes, which may include space for metadata (sometimes garbage is returned). These numbers should be considered as estimates of the required memory, and the actual memory can be recognized by measurements. - jfs
|