import os
import xml.etree.ElementTree as et
for ev, el in et.iterparse(os.sys.stdin):
el.clear()
Running the above on the ODP structure RDF dump results in always increasing memory. Why is that? I understand ElementTree still builds a parse tree, albeit with the child nodes clear()ed. If that is the cause of this memory usage pattern, is there a way around it?
You are
clearing each element but references to them remain in the root document. So the individual elements still cannot be garbage collected.The solution is to clear references in the root, like so:
Another thing to remember about memory usage, which may not be affecting your situation, is that once the VM allocates memory for heap storage from the system, it generally never gives that memory back. Most Java VMs work this way too. So you should not expect the size of the interpreter in
toporpsto ever decrease, even if that heap memory is unused.update :
Code changed in order to work in Python 3+.