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
- Process large files in chunks instead of loading all at once: for chunk in pd.read_csv(f, chunksize=10000):
- Use memory-efficient data types: numpy float32 instead of float64, or categories in pandas
- Check memory usage: import tracemalloc; tracemalloc.start() to identify the source
- 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')
MemoryErrorLoading 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.
