Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:39:28+00:00 2026-06-01T12:39:28+00:00

import os import xml.etree.ElementTree as et for ev, el in et.iterparse(os.sys.stdin): el.clear() Running the

  • 0
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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:39:29+00:00Added an answer on June 1, 2026 at 12:39 pm

    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:

    import xml.etree.ElementTree as ET
    
    # get iterator
    context = ET.iterparse(source, events=("start", "end"))
    
    # get the root element
    event, root = next(context)
    
    for event, elem in context:
        if event == "end" and elem.tag == "record":
            # process record elements here...
            root.clear()
    

    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 top or ps to ever decrease, even if that heap memory is unused.

    update :

    Code changed in order to work in Python 3+.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import urllib import xml.etree.ElementTree as ET def getWeather(city): #create google weather api url url
please consider this code: import xml.etree.ElementTree as ET import urllib XML_response = urllib.urlopen('http://www.navlost.eu/aero/metar/?icao=LWSK&dt0=2011-05-03+12%3A00%3A00&c=1&rt=metar').read() tree
I have tho following code fragment: from xml.etree.ElementTree import fromstring,tostring mathml = fromstring(input) for
import xml.etree.ElementTree as ET xmldata = file('my_xml_file.xml') tree = ET.parse(xmldata) root = tree.getroot() root_iter
I have been trying to parse a file with xml.etree.ElementTree : import xml.etree.ElementTree as
Here's the code I have: from cStringIO import StringIO from lxml import etree xml
I have some code to retrieve XML data: import cStringIO import pycurl from xml.etree
I am trying to use xml.etree.ElementTree to parse responses from eBay finding API, findItemsByProduct.
I'm using python 2.6.2's xml.etree.cElementTree to create an xml document: import xml.etree.cElementTree as etree
I found a nice script to import xml using powershell http://slash4.de/tutorials/Automatic_mediawiki_page_import_powershell_script Currently I don't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.