All posts

Fix MemoryError in Django in Production

Resolve production MemoryError in Django caused by large querysets, file handling, and Celery task memory accumulation.

Django MemoryError in Production

A MemoryError in production Django means your worker process has exhausted available RAM. This is different from a slow memory leak — it's usually a single request or task that tries to load too much data at once.

Large QuerySets

The most common cause — loading an entire table into memory:

# CRASH: loads all rows into a list
all_users = list(User.objects.all())  # 10M users = boom

# FIX: paginate or stream
from django.core.paginator import Paginator

paginator = Paginator(User.objects.all(), 1000)
for page_num in paginator.page_range:
    page = paginator.page(page_num)
    for user in page.object_list:
        process(user)

Or use iterator() for read-only processing:

for user in User.objects.all().iterator(chunk_size=2000):
    process(user)

File Uploads

Reading uploaded files entirely into memory:

# BAD
def upload(request):
    content = request.FILES['data'].read()  # 2GB file = crash

# GOOD — stream to disk
def upload(request):
    uploaded = request.FILES['data']
    with open('/tmp/upload.csv', 'wb') as f:
        for chunk in uploaded.chunks(chunk_size=8192):
            f.write(chunk)

Celery Task Memory

Celery workers accumulate memory across tasks. Configure worker recycling:

# celery.py
app.conf.worker_max_memory_per_child = 200_000  # 200MB, then restart

Bugsly captures MemoryError exceptions with the full stack trace and request context, showing exactly which view or task triggered the crash.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free