Reputation: 152
I think this question is not good for understand, but i have to guess about it.
Status:
I expect, memory usage will be bigger then usual and if i use orm as on, query count will be over then 20,000 count. so i planed this work like this.
dict()
with only using data. that dict()
call queryset
dataset
.so code will be like this.
for user in every_user:
filter = user.get_filter()
dataset = {k: v for k, v in queryset.iteritems() if v['status'] == filter}
message_html = render_to_string('a.html', dataset)
send_mail(user.email, message_html)
so, i am worry about dataset
and message_html
are use memory and, it will make memory leaks (i think..).
so this is question. i have to del dataset
and del message_html
on that for
loop?
Upvotes: 0
Views: 63
Reputation: 148890
Python uses garbage collection. That means that when a variable has no references on it, it can be freed by the garbage collector. del
does not free memory: it just remove the indentifier pointing to the actual variable.
Example:
a = [1,2,3]
b = a
del a
b still points to the list [1,2,3], so it cannot be freed
That means that as dataset
and message_html
and defined inside the loop they are automatically undefined at the end of each iteration, so del dataset
and del message_html
is essentially no-op. It just causes an explicit destruction of the identifiers that will be implicitely destroyed on next instruction
TL/DR: in your code del dataset
and del message_html
on end of loop would be no-op.
For the actual question, in your code, Python will only use memory for one single user and free it before next user iteration.
Upvotes: 2
Reputation: 70
Given the example code, those variables will be garbage collected. There is no need to free them.
If you're nervous, you could simply split the contents of the loop into a function taking the user as a parameter. It is easy to reason that a local variable within a function that is never passed out of the function necessarily is available for garbage collection as soon as the function returns.
Upvotes: 1