Skip to content Skip to sidebar Skip to footer

Why Does Not Django Release Fetched Db Objects?

I have very simple django app: models.py: class Product(models.Model): name = models.CharField(max_length=1000, default='') desc = models.TextField(default='') vie

Solution 1:

This is not really anything to do with Django. Generally, Python does not return memory to the operating system until it needs to.

See the effbot's explanation for more detail.

Solution 2:

This line is the culprit

for p in Product.objects.all()[:300000]:

The slicing forces the QuerySet to evaluate, ie. hit the database, and it then returns a list() containing the slice of objects, which you then can iterate over. And Django's QuerySet cache keeps it in memory since you might want to iterate over the same "QuerySet" again.

You could optimize this by using an iterator.

It's in the docs here

Post a Comment for "Why Does Not Django Release Fetched Db Objects?"