ProjectStack
python

MemoryError

Python couldn't allocate enough memory to complete the operation. Your program is trying to hold more data in RAM than the system has available.

Common causes

  • Loading a very large file entirely into memory instead of processing it in chunks
  • Creating a large numpy array, pandas DataFrame, or list that exceeds available RAM
  • A memory leak — objects are accumulating and never being garbage collected
  • Running on a system or container with a small memory limit

How to fix it

  1. Process large files in chunks instead of loading all at once: for chunk in pd.read_csv(f, chunksize=10000):
  2. Use memory-efficient data types: numpy float32 instead of float64, or categories in pandas
  3. Check memory usage: import tracemalloc; tracemalloc.start() to identify the source
  4. Increase available memory or upgrade to a machine/instance with more RAM

Example

Traceback (most recent call last): File "app.py", line 4, in <module> data = pd.read_csv('10gb_file.csv') MemoryError

Loading a 10 GB CSV file entirely into a pandas DataFrame on a machine with 8 GB of RAM

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.