Hi,

Does anyone now how to free up the memory in a XMLElement object in 
LightXML.jl?

LightXML.jl has a free(xdoc::XMLDocument) but no corresponding 
free(xelem::XMLElement). There are functions related to free in clib.jl in 
the package but no docs that I can find on how to use it. I tried to create 
an XMLDocument from the element and then free the document but mem still 
leaks. Help appreciated.

Background/context/details:

In another thread we found out that our problems with mem-leaks in a 
long-running optimization is probably due to the use of LightXML.jl. The 
latter seem to require manual free'ing of resources as of the current 
version. Basically our main loop is something like this:

# Create a large number of XMLElement's (possibly deeply nested) with
construct_element(name::String, content::Array{Any}) = begin
  xmlelement = new_element(name)
  for item in content
    if typeof(item) <: Main.XMLElement
      add_child(xmlelement, item)
    elseif typeof(item) <: (String,String)
      set_attribute(xmlelement, item[1], item[2])
    elseif typeof(item) <: String
      add_text(xmlelement, item)
    else
      @assert false
    end
  end
  xmlelement
end

# What we are optimizing is the length of the xml when printed as string so 
we evaluated this by looping over array of xml elements generated
# by construct_element above and calling on each one (the replace is for 
taking away at least some of the whitespace which is not important):
fitness(xml) = length(replace(string(xml), r">\s+<", "><"))

Since the optimization algorithm calls fitness on 100K to several million 
xml elements per run when we compare several optimization algorithms to 
each other the mem expansion gets up to many gigs and eventually there is 
problems. It seems there is no free on XMLElement in LightXML.jl but there 
is a free for XMLDocument. We thus tried to change to this:

free_xmlelement(xmlelem::XMLElement) = begin
  tempdoc = XMLDocument()
  set_root(tempdoc, xmlelem)
  free(tempdoc)
end
fitness_with_free(xml) = begin
  qv = length(replace(string(xml), r">\s+<", "><"))
  free_xmlelement(xml)
  qv
end

but it does not seem to help. We would appreciate any help/advice on this 
or how we can further debug it. The documentation for xmlFreeDoc in libxml2 
(which is what is  what LightXML.free(xdoc::XMLDocument) calls) says that 
it will recursively free the document. But maybe we are missing something.

Thanks for any advice,

Robert Feldt

Reply via email to