# DOMNodeList memory eating

- Thread: https://externals.io/message/64102
- Original archive: https://news-web.php.net/php.internals/64102
- Started by Nicolai Scheer on 2012-11-29T13:36:32+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64102 — Nicolai Scheer — 2012-11-29T13:36:32+00:00

URL: https://externals.io/message/64102#64102

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

---

## #64103 — Rob Richards — 2012-11-29T14:00:55+00:00

URL: https://externals.io/message/64102#64103 · In reply to: #64102

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


> 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
