Why does this code eat up memory? When I run it it slowly consumes more memory with every loop, and I have something like 300000 loops. I'm using Windows, and Python 2.6.
def LoadVotes(self):
old_votes=Votes.objects.all()
amount=old_votes.count()
print 'Amount of votes is: ' + str(amount)
c=0
for row in old_votes:
try:
new_id_user=LegacyUserId.objects.get(legacy_id=row._login)
except LegacyUserId.DoesNotExist:
string=" user with old id "+str(row._login)+" does not match new user id /n"
log=open('log_add_old_votes.txt','a')
log.write(string)
continue
try:
new_id_media=LegacyMedia.objects.get(legacy_id=row.media_file_id)
except new_id_media.DoesNotExist:
log_text='old media with ID:'+str(row.media_file_id)+' is not found in relation with new media \n'
log=open('log_add_old_votes.txt','a')
log.write(log_text)
continue
mo=MediaObject.objects.get(pk=new_id_media.object_id)
new_votes_item, created=Mark.objects.get_or_create(user=new_id_user.user, media_object=mo, defaults={'mark':int(row.rate)*2})
c=c+1
i=amount-c
print '\rRemain:',
stdout.write("%d" % i)
stdout.flush()