πŸ”§ Error Fixes
Β· 1 min read

Python MemoryError β€” How to Fix It


Python raises MemoryError when it can’t allocate more memory. This usually means your script is trying to load more data into RAM than your system has available.

What causes this error

  1. Loading a huge file into memory at once β€” reading a multi-GB CSV or JSON file with open().read() or pd.read_csv() without chunking
  2. Creating a massive list or dictionary β€” generating millions of objects in a loop
  3. Memory leak β€” objects that should be garbage collected are still referenced

Fix 1: Process data in chunks

Instead of loading everything at once:

# ❌ This loads the entire file into memory
data = pd.read_csv('huge_file.csv')

# βœ… Process in chunks
for chunk in pd.read_csv('huge_file.csv', chunksize=10000):
    process(chunk)

For plain files:

# ❌ Loads entire file
content = open('huge.txt').read()

# βœ… Read line by line
with open('huge.txt') as f:
    for line in f:
        process(line)

Fix 2: Use generators instead of lists

# ❌ Creates a list of 100M items in memory
squares = [x**2 for x in range(100_000_000)]

# βœ… Generator β€” computes one at a time
squares = (x**2 for x in range(100_000_000))

Fix 3: Find the memory leak

import tracemalloc
tracemalloc.start()

# ... your code ...

snapshot = tracemalloc.take_snapshot()
for stat in snapshot.statistics('lineno')[:10]:
    print(stat)

This shows which lines allocate the most memory.

Also related: Python cheat sheet for quick syntax reference.

If you genuinely need more RAM:

  • Use a machine with more memory
  • On Linux, add swap space: sudo fallocate -l 4G /swapfile
  • Use 64-bit Python (32-bit is limited to ~2GB)

How to prevent it

  • Always use chunked reading for files over 100MB
  • Prefer generators over list comprehensions for large datasets
  • Profile memory usage with tracemalloc or memory_profiler during development
  • Consider using numpy arrays instead of Python lists (10x more memory efficient for numbers)

Related fixes: Python AttributeError: NoneType Β· Python TypeError Β· Python ValueError

Related: Pip Install Error Fix

πŸ“˜