MemoryError 🐍 Python

MemoryError (Unable to allocate X GiB for an array)

Python couldn’t allocate memory — the data is larger than available RAM or address space.

Seen on: Python

Meaning

NumPy/pandas raise “Unable to allocate X GiB for an array with shape …”. 32-bit Python is limited to ~2–4 GB regardless of RAM.

Common causes

  • Loading a huge file fully into memory
  • Creating giant arrays/matrices
  • 32-bit Python
  • Memory leak in long loops

⚡ Quick fix

  1. Process in chunks (pd.read_csv(chunksize=...))
  2. Use smaller dtypes (float32, category)
  3. Install 64-bit Python
  4. Use generators instead of lists

Detailed fix by platform

Python

  1. for chunk in pd.read_csv("big.csv", chunksize=100_000): process(chunk)

How to diagnose

  1. Size — How large is the data vs RAM?
  2. Build — 32- or 64-bit Python?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.