Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Memory limit and very big XML file

I need to parse a very big XML file, with a filesize of 750Mo !

I have meomy limit at 512M

ini_set('memory_limit', '512M');

I have no problem to open file under 30Mo, but with 750Mo, I obtain a fatal error

Fatal error: Allowed memory size of 1677721600 bytes exhausted (tried to allocate 2988843769 bytes)

I do that to open files :

$fichier = file_get_contents($inputfileName);
$xmlInput = simplexml_load_string(utf8_encode($fichier));

Have you an idea to open this file ?

like image 850
bahamut100 Avatar asked Aug 31 '25 10:08

bahamut100


1 Answers

Using the DOM based extensions will take up significantly more memory as the raw XML is because the XML will be parsed completely into a tree structure of nodes. Have a look at XMLReader instead

The XMLReader extension is an XML Pull parser. The reader acts as a cursor going forward on the document stream and stopping at each node on the way.

and make sure you parse with LIBXML_PARSEHUGE

An alternative would the event-based XMLParser

like image 175
Gordon Avatar answered Sep 03 '25 09:09

Gordon