Loading...

PHP simple_xml import dom memory problem

posted on Fri, October 13th, 2006

I was/am in the need to parse a big (6GB) xml file and thought I am going to use XMLReader and the expand function to separate the pieces within the XML file and then use simplexml to parse the pieces. This works quite well, but I ran into a stupid memory leak, which only appeared as I needed to extract some infomation within a string with a regular expression. Below is the piece of code, which I extracted to showcase the memory leak. I am using XMLReader to run through the top elements and expand them to a dom node, which is then imported into a simple xml dom object.

$xml = new XMLReader();
$xml->XML('<page><title>test</title></page>');
$xml->next('page');
$dom = new DomDocument();
$dom->appendChild($dom->importNode($xml->expand(), true));
$simple = simplexml_import_dom($dom);

while (true) {
preg_match_all('/a/', $simple->title, $links);
echo $simple->title . ": " . memory_get_usage() ."\n";
}


It seems that the simple xml object dom import has some casting problems, as it seems that the $simple->title doesn't return a string. If you cast the return value like shown below the memory usage is just normal.

$xml = new XMLReader();
$xml->XML('<page><title>test</title></page>');
$xml->next('page');
$dom = new DomDocument();
$dom->appendChild($dom->importNode($xml->expand(), true));
$simple = simplexml_import_dom($dom);

while (true) {
preg_match_all('/a/', (string)$simple->title, $links);
echo $simple->title . ": " . memory_get_usage() ."\n";
}


 
 

Comments

 
 

Leave a Comment

Commenting is not available in this weblog entry.