#41053 [Fbk]: Filling Array with integers : memory usage too high
ID: 41053
Updated by: stas@php.net
Reported By: matthieu dot aubry at gmail dot com
Status: Feedback
Bug Type: Performance problem
Operating System: linux
PHP Version: 5.2.1
New Comment:
Each element requires a value structure (zval) which takes 16 bytes.
Also requires a hash bucket - which takes 36 bytes. That gives 52 bytes
per value. Memory allocation headers take another 8 bytes*2 - which
gives 68 bytes. Pretty close to what you have.
Previous Comments:
------------------------------------------------------------------------
[2007-04-11 14:12:11] tony2001@php.net
Please try using this CVS snapshot:
http://snaps.php.net/php5.2-latest.tar.gz
For Windows:
http://snaps.php.net/win32/php5.2-win32-latest.zip
------------------------------------------------------------------------
[2007-04-11 13:26:37] matthieu dot aubry at gmail dot com
Description:
------------
Hello,
I would like to cache big tables containing integers in PHP memory. But
I notice that PHP uses a LOT of memory for variables.
I simply fill an array indexed with 1,000,000 distinct integers. I
assign 0 to all elements.
Results
=======
PHP 5.1.2 result is 57.5 MB
PHP 5.2.1 result is 99.4 MB
Minimum used = 1E6 * (4bytes+4bytes) = 8MB.
I don't know much about PHP Array implementation. I don't expect it to
use only 8MB of course, but I think it actually uses too much memory.
Also, why this difference between the 2 versions?
(I've read bug reports stating memory problem in 5.2.0 but they were
said to be fixed before 5.2.1)
Reproduce code:
---------------
<?php
function getMemoryUsage()
{
return round(memory_get_usage() / (1024*1024), 1)." MB";
}
function printMemoryUsage()
{
print("Memory = ".getMemoryUsage()." <br>\n");
}
printMemoryUsage();
$end = 1000000;
$array = array();
printMemoryUsage();
for($i = 0; $i < $end; $i++)
{
$array[$i] = 0;
}
printMemoryUsage();
?>
Expected result:
----------------
Thank you for explanations/details ; is this behaviour expected?
Could it be improved in future releases?
Actual result:
--------------
=================
Test PHP 5.2.1-0.dotdeb.1 with Suhosin-Patch 0.9.6.2 (cli) (built: Feb
9 2007 04:46:32)
=================
Linux 2.4.33bipiv-ipv4-32 #1 SMP lun oct 23 18:38:55 CEST 2006 i686
GNU/Linux
# time php test27.php
Memory = 0.1 MB <br>
Memory = 0.1 MB <br>
Memory = 99.4 MB <br>
real 0m0.954s
=================
Test PHP 5.1.2 (cli) (built: Mar 7 2007 21:53:56)
=================
Linux 2.6.15-23-386 #1 PREEMPT Tue May 23 13:49:40 UTC 2006 i686
GNU/Linux
$ time php test27.php
Memory = 0 MB <br>
Memory = 0 MB <br>
Memory = 57.5 MB <br>
real 0m1.520s
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=41053&edit=1
Thread (6 messages)