For the below code, I am not able to clear the memory allocated for 'root' variable evan after 'del root' and gc.collect(). I understand that in python, garbage collection releases automatically. Is there any way I can clear it more?
from __future__ import with_statement
import os
import sys
from memory_profiler import profile
import gc
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
def f():
with open('file.xml') as f:
output_xml_str = f.read()
root = ET.fromstring(output_xml_str)
del root
@profile
def main():
x = {}
for i in xrange(10000):
x[i] = i+1
del x
f()
for i in range(12):
gc.collect()
if __name__ == "__main__":
sys.exit(main())
and here's profiling output
Unfortunately you can't do much except calling gc.collect()
plus you can adopt good practice style like you used del
to tell the GC
that you don't want it anymore so it will be deleted when GC
will come around and do it's job.
You should use numpy
for container like array which is more efficient in terms of memory management
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.