Search code examples
xmlpython-2.7memorycelementtree

python 2.7 xml.etree.cElementTree memory use


I created a small tool for my job, it parses xml files to find the text of an element; example code:

import xml.etree.cElementTree as Etree


def open_xml():
    """
    Retrieves info from the xml file
    """
    try:
        tree = Etree.parse("xml_file.xml")
        text_to_find = tree.findtext(
            path=".//ns:some_element",
            namespaces={"ns": "http://something.com/something"})
        print text_to_find

    except IOError:
        print "No xml file found."

This is part of a gui; we never close the app, it's always running on a dedicated computer.

If I understand correctly, python creates an object in memory representing the xml tree. My question is: do I need to clear the memory of that object after I'm done printing the info? I'm worried that we're using more and more memory each time we parse an xml file...


Solution

  • No, you don't have to worry about that, as far as I know. Given that the only variable referencing the cElementTree instance is the variable tree, then the instance will be deleted soon after tree goes out of scope. To experiment, you can define a class that will print something when it is deleted so you know when the deletion happen, like the one posted here :

    class C:
        def __del__(self):
            print "delete object"
    

    and then reference instance of that class using a variable declared in the same scope as the tree variable :

    def open_xml():
        ....
        try:
            tree = Etree.parse("xml_file.xml")
            c = C()
            ....
    

    This way you can predict deletion of instance referenced by tree by deletion of instance referenced by c, as they are declared in the same scope. Now watch & see if "delete object" get printed to your console.