PHP XML memory leak?

We have a serious memory leak in one of our regular scripts that quickly destroy free memory on the server. Despite many hours of research and experimentation, I could not even go into it.

Here is the code:

    echo '1:'.memory_get_usage()."\n";
ini_set('memory_limit', '1G');
    echo '2:'.memory_get_usage()."\n";

$oXML = new DOMDocument();
    echo '3:'.memory_get_usage()."\n";
$oXML->load('feed.xml'); # 556 MB file
    echo '4:'.memory_get_usage()."\n";

$xpath = new DOMXPath($oXML);
    echo '5:'.memory_get_usage()."\n";
$oNodes = $xpath->query('//feed/item'); # 270,401 items
    echo '6:'.memory_get_usage()."\n";

unset($xpath);
    echo '7:'.memory_get_usage()."\n";
unset($oNodes);
    echo '8:'.memory_get_usage()."\n";
unset($oXML);
    echo '9:'.memory_get_usage()."\n";

And here is the conclusion:

1:679016
2:679320
3:680128
4:680568
5:681304
6:150852408
7:150851840
8:34169968
9:34169448

As you can see, when we use xpath to load nodes into an object, memory usage jumps from 681 304 to 150 852 408. I am not very worried about this.

My problem is that even after the destruction of the $ oNodes object, we are still stuck using memory 34,169,968.

, , PHP, , script. free -m , 3,295 , 5,226 , . 2 , script, , .

SimpleXML , . , , :

XML xpath php,

DOMDocument/Xpath -

DOMDocument PHP

, - , .

UPDATE 11/10: , . , 30 . , , , .

, PHP 5.3.15 Apache 2.2.3 Red Hat 5.11. , - , . .

+4
2

, . XML 3gb, , . :

  • xpath, () file_get_contents. , .
  • xml . xml , ( , )

, 30 - . 500 xml 30 - . , , 3gb xml ( 200). script ( 700 . ) 5 .

0

PHPDocxPro ( DomDocument) , , , . , get_memory_usage(), , PHP . , top ps - , .

// ps reports X memory usage
var $foo = (new DomDocument())->loadXML(getSomeXML());
// ps reports X + Y memory usage
var $foo = (new DomDocument())->loadXML(getSomeXML());
// ps reports X + ~2Y memory usage
var $foo = (new DomDocument())->loadXML(getSomeXML());
// ps reports X + ~3Y memory usage

unset() ...

// ps reports X memory usage
var $foo = (new DomDocument())->loadXML(getSomeXML());
// ps reports X + Y memory usage
unset($foo);
var $foo = (new DomDocument())->loadXML(getSomeXML());
// ps reports X + ~Y memory usage
unset($foo);
var $foo = (new DomDocument())->loadXML(getSomeXML());
// ps reports X + ~Y memory usage

, , , , PHP, , get_memory_usage(). , , , , . unset($foo) , . .

0

Source: https://habr.com/ru/post/1615075/


All Articles