Django - bulk_create() lead to memory error

If your are using Django in debug mode it will keep track on all your sql statements for debugging purposes. For many objects this may cause memory problems. You can reset that with:

from django import db
db.reset_queries()

see why-is-django-leaking-memory


You can specify the batch_size in the bulk_create method.

Syntax: bulk_create(objs, batch_size=None)
Feature.objects.bulk_create(instances, batch_size=1000)

Django 2.2: https://docs.djangoproject.com/en/2.2/ref/models/querysets/#bulk-create

Django 3.1: https://docs.djangoproject.com/en/3.1/ref/models/querysets/#bulk-create