← Back to NumPy Course | Chapter 3: Array Attributes | Lesson 6 of 6

nbytes

nbytes is the total memory the array's data occupies.

In this page:

  1. nbytes
Syntax
python
arr.nbytes    # size * itemsize

nbytes

nbytes equals size multiplied by itemsize. It counts only the element data, not the small array object overhead. It is a quick way to estimate how much RAM a large array will use.

Note: A million float64 values take about 8 MB.

Example: nbytes

python
import numpy as np

a = np.zeros(1000, dtype=np.float64)
b = np.zeros(1000, dtype=np.float32)
print(a.nbytes, b.nbytes)
print(a.size * a.itemsize == a.nbytes)

# Output:
# 8000 4000
# True
Related Topics
Common Mistakes
  1. Expecting nbytes to include Python object overhead
  2. Forgetting dtype affects the total
  3. Comparing with sys.getsizeof which reports differently
Chapter Summary
  • nbytes = size * itemsize
  • Only counts element data
  • Halving precision halves memory
  • Useful for capacity planning
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.