XPath returns a fully populated list of object based on the expression. getElementsByTagName accesses the nodes within the tree in real time as you iterate so you only have one object at a time unless you are persisting them. Different behavior due to who owns the data and its lifecycle. XPath data is strictly owned by libxml while the underlying data from getElementsByTagName can be managed by PHP.

Rob

On 11/29/12 8:36 AM, Nicolai Scheer wrote:
Hi!

Just stumbled upon a strange behaviour when iterating over DOMNodeLists.
Before I open a bug report I'd like to ask if anyone can comment on this
issue, maybe it's the excpected behaviour.

Consider the following small script:

<?php

fgets( STDIN );

$xml = <<<XML
<?xml version="1.0" ?>
<root>
     <node>x</node>
     <node>x</node>
     <node>x</node>
</root>
XML;

$dom = new DOMDocument();
$dom->loadXML( $xml );
$xpath = new DOMXPath( $dom );

while ( true )
{
     # 1. memory eater...
     $link_object_list = $xpath->query( '//node' );

     # 2. constant memory usage
     #$link_object_list = $dom->getElementsByTagName( 'node' );

     foreach ( $link_object_list as $value )
     {
         while ( true )
         {
             $link = $link_object_list->item( 0 );
         }
     }
}

?>


Strange thing is, if the DomNodeList is produced by the xpath query, the
script eats memory. If I use getElementsByTagName to get the list memory
usage is constant.

$value holds the object handle for the first NodeItem. If another list
element is accessed (e.g. the second one) the usage is constant as well
(both cases).

Seems as if there's something strange going on with object handles and / or
wrong garbage collection. Or maybe I'm just confusing things here ;)

Any comments?

Greetings

Nico



--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to