note 128070 added to class.arrayiterator
| From: | Mario at main dot php dot net | Date: | Fri, 30 Dec 2022 14:44:06 +0000 |
| Subject: | note 128070 added to class.arrayiterator | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220025@lists.php.net to get a copy of this message | ||
"mail at markuszeller dot com" claimed memory increase without use of IteratorAggregate,
but his provided code examples are not equal.
His first example returns a new ArrayIterator from a method and moment the loop is finished, the
memory used is freed. His second example however keeps references to these ArrayIterators in
variables. If you were to modify the second example to not write variables, the memory usage would
not increase, as expected.
$agg = [];
for ($i = 0; $i < 1e5; $i++) $agg[] = true;
echo memory_get_usage(), PHP_EOL;
// 4607624
foreach(new ArrayIterator($agg) as $data){};
echo memory_get_usage(), PHP_EOL;
// 4607624
foreach(new ArrayIterator($agg) as $data){};
echo memory_get_usage(), PHP_EOL;
// 4607624
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 2001:7d0:87f2:1300:e046:935e:8701:fde)
----
Manual Page -- https://php.net/manual/en/class.arrayiterator.php
Edit -- https://main.php.net/note/edit/128070
Del: integrated -- https://main.php.net/note/delete/128070/integrated
Del: useless -- https://main.php.net/note/delete/128070/useless
Del: bad code -- https://main.php.net/note/delete/128070/bad+code
Del: spam -- https://main.php.net/note/delete/128070/spam
Del: non-english -- https://main.php.net/note/delete/128070/non-english
Del: in docs -- https://main.php.net/note/delete/128070/in+docs
Del: other reasons-- https://main.php.net/note/delete/128070
Reject -- https://main.php.net/note/reject/128070
Search -- https://main.php.net/manage/user-notes.php