Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64103 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 97581 invoked from network); 29 Nov 2012 14:01:00 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Nov 2012 14:01:00 -0000 Authentication-Results: pb1.pair.com header.from=rrichards@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rrichards@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 207.58.142.213 as permitted sender) X-PHP-List-Original-Sender: rrichards@php.net X-Host-Fingerprint: 207.58.142.213 smtp2go.com Linux 2.6 Received: from [207.58.142.213] ([207.58.142.213:42726] helo=smtp2go.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 57/33-08727-B9A67B05 for ; Thu, 29 Nov 2012 09:01:00 -0500 Message-ID: <50B76A97.8020302@php.net> Date: Thu, 29 Nov 2012 09:00:55 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: Nicolai Scheer CC: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] DOMNodeList memory eating From: rrichards@php.net (Rob Richards) 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: > > > fgets( STDIN ); > > $xml = << > > x > x > x > > 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 >