All posts

Fix MemoryError in Django When Deploying

Fix out-of-memory errors during Django deployment caused by collectstatic, migrations on large tables, and pip install in containers.

Django Deployment Memory Errors

Django deployments can fail with memory errors during build steps like collectstatic, migrate, or pip install. These are distinct from runtime memory issues.

collectstatic OOM

With many static files or heavy post-processing (like django-compressor), collectstatic can eat RAM:

# Run with increased memory and no input prompts
python manage.py collectstatic --noinput --clear

If it still fails, split the process:

# Dockerfile — use a bigger build stage
FROM python:3.12-slim AS builder
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN python manage.py collectstatic --noinput

# Slim runtime
FROM python:3.12-slim
COPY --from=builder /app/staticfiles /app/staticfiles

pip install Memory

Some packages (numpy, pandas) require lots of memory to build from source:

# Use pre-built wheels
RUN pip install --no-cache-dir --prefer-binary -r requirements.txt

Migrations on Large Tables

Adding a column to a table with millions of rows can require a table rewrite:

# Use RunSQL for zero-downtime migration
class Migration(migrations.Migration):
    operations = [
        migrations.RunSQL(
            "ALTER TABLE myapp_bigtable ADD COLUMN new_col INTEGER DEFAULT 0;",
            reverse_sql="ALTER TABLE myapp_bigtable DROP COLUMN new_col;"
        ),
    ]

CI Memory Limits

# GitHub Actions — use a larger runner
jobs:
  deploy:
    runs-on: ubuntu-latest-4-cores  # More memory

Bugsly tracks deployment events alongside application errors, so you can correlate deployment failures with subsequent runtime issues in a unified timeline.

Try Bugsly Free

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

Get Started Free