Bug #68392 [Com]: foreach memory usage increase
| From: | xczgamer at gmail dot com | Date: | Wed, 12 Nov 2014 09:19:39 +0000 |
| Subject: | Bug #68392 [Com]: foreach memory usage increase | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-188561@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68392&edit=1
ID: 68392
Comment by: xczgamer at gmail dot com
Reported by: ryan dot brothers at gmail dot com
Summary: foreach memory usage increase
Status: Open
Type: Bug
Package: Performance problem
Operating System: Linux
PHP Version: 5.6.2
Block user comment: N
Private report: N
New Comment:
PHP copies arrays when send to a function.
Hence yes, the memory almost doubles as the entire array gets copied.
If you change the following line:
function test_function($rows)
to:
function test_function(&$rows)
The memory usage will remain the same.
Previous Comments:
------------------------------------------------------------------------
[2014-11-10 17:39:29] ryan dot brothers at gmail dot com
Description:
------------
In the below example, the foreach in the global scope doesn't use any extra memory, but the
memory usage almost doubles from doing the foreach inside test_function. Is there a reason that so
much extra memory is used? I was expecting it not to use any extra memory as the foreach is done
using a reference for each array element.
Test script:
---------------
<?php
function test_function($rows)
{
echo memory_get_usage(true)."\n";
foreach ($rows as &$row)
{
}
unset($row);
echo memory_get_usage(true)."\n";
};
$rows = range(1, 10000);
echo memory_get_usage(true)."\n";
foreach ($rows as &$row)
{
}
unset($row);
echo memory_get_usage(true)."\n";
test_function($rows);
Expected result:
----------------
1835008
1835008
1835008
1835008
Actual result:
--------------
1835008
1835008
1835008
3407872
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68392&edit=1