Also, a search for [python garbage collection] might give you more understanding about your problem.
I recently had a problem with python memory usage, which came down more or less to this (semi-pseudo-code):
Code:
for file in directory:
text = open(file).read()
do something with text
Now, if you're not familiar with lower level languages and the python garbage collector (like I was), you might think: well, the text variable is overwritten each time, so this can't possibly take that much more space than my largest file. Except: it's not really overwritten. So if this if loops continues, it will take the memory of the sum of all the files in the directory.
This can be avoided by placing the inner part of the for loop in a function and only passing that function the values it needs.
Code:
def read_and_do_something(file):
text = open(file).read()
do something with text
for file in directory:
read_and_do_something(file)
this way the text variable (and all others, except file) are deleted when the function ends because they "run out of scope" which means that there's no way they could be accessed.
Hope this helps.
Bookmarks