views:

20

answers:

1

I have a model that contains one-to-one fields to other models. I over rid the save method to automatically assign these one-to-one fields. The problem is whenever I save this model, memory usage goes up by about 450k and is never released. The save method is as follows:

class Link(models.model):
   id = models.CharField(max_length=11, primary_key=True)
   fieldOne = models.OneToOneField(One, null=True, editable=False)
   fieldTwo = models.OneToOneField(Two, null=True,, editable=False)
   fieldThree = models.OneToOneField(Three, null=True,, editable=False)

   def save(self, *args, **kwargs):
       self.fieldOne = One.objects.get(id=self.id)
       self.fieldTwo = Two.objects.get(id=self.id)
       self.fieldThree = Three.objects.get(id=self.id)

       super(Link, self)save(*args, **kwargs)

I believe the memory leak occurs at the line when objects.get() is called, since when I comment tem out, I noticed no increase in mem usage.

A: 

Are you running with DEBUG on? DEBUG has nasty repercussions on memory usage.

Check out the docs on on memory leaks:

http://docs.djangoproject.com/en/dev/faq/models/#why-is-django-leaking-memory

Jack M.
Changed DEBUG to false and now memory usage only goes up by 10k. Much more manageable. Thanks for the hint. I didn't know DEBUG had this side effect.
Luiz C.
It keeps all of your queries in memory so that you can debug them. Kind of a hog, but when you need it, you're glad it is there.
Jack M.