top of page

How to deal with memory leakage caused by XML package in R ?


Hey all,

In this post, I'll give you a very short but efficient trick for solving memory leakage issue in XML package. Almost for two months, I was struggling with some memory leakage issues in one my projects.

But first, to give you some background, I have been working on a project that I have to build a MCMC for optimizing some parameters. Therefore I need to be able to run a specific task for hundreds of thousands of times. As, part of the task, I am supposed to modify a XML document and then run a model in each iteration. It didn't take a very long time that I realized there is a big memory leakage somewhere in my code.

Long story short, I was able to track that down and realized that it has to do with the way that XML package in R treats its objects. I have no idea, how this happened, but it seems that the developer has forgotten to release the memory taken by its functions. So in next couple of lines I'll show you a very simple trick for handling this issue:


library (XML)

for (i in 1:1000) {

pXML<-xmlParse(file)

### do some stuff here

if(exists("pXML")){

.Call("RS_XML_forceFreeDoc", pXML) # it's how you release that

rm(pXML)

gc()

}

}









Featured Posts
Recent Posts
Archive
Search By Tags
bottom of page