Fugas de memoria al analizar XML en r
Las pérdidas de memoria cuando se utiliza el paquete XML en r no es algo nuevo. Este tema ya ha sido discutido:
Grave pérdida de memoria al analizar iterativamente archivos XMLhttp://www.omegahat.org/RSXML/MemoryManagement.htmlhttp://r.789695.n4.nabble.com/memory-leak-using-XML-readHTMLTable-td4643332.htmlSin embargo, después de leer todos estos documentos, todavía no conozco una solución para mi caso particular. Considere el siguiente código:
library(XML)
GetHref = function(x)
{
subDoc = xmlChildren(x)
hrefs = ifelse(is.null(subDoc$a), NA, xmlGetAttr(subDoc$a, 'href'))
rm(subDoc)
return(hrefs)
}
url = 'http://www.atpworldtour.com/Share/Event-Draws.aspx?e=338&y=2013'
parse = htmlParse(url)
print(.Call("R_getXMLRefCount", parse)) #prints 1
NodeList = xpathSApply(parse, "//td[@class='col_1']/div/div/div[@class='player']")
print(.Call("R_getXMLRefCount", parse)) #prints 33
PlNames = sapply(NodeList, xmlValue, trim = T)
print(.Call("R_getXMLRefCount", parse)) #prints 33
hrefs = sapply(NodeList, GetHref)
print(.Call("R_getXMLRefCount", parse)) #prints 157
rm(NodeList)
gc()
print(.Call("R_getXMLRefCount", parse)) #prints 157
Parece que los nodos XML internos creados durante el procesamiento posterior no se eliminan. ¿Cuál sería una solución en este caso?
Session Info:
R version 3.0.2 (2013-09-25)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] XML_3.98-1.1
loaded via a namespace (and not attached):
[1] tools_3.0.2