|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-04-11 14:12 UTC] tony2001@php.net
[2007-04-11 17:29 UTC] stas@php.net
[2007-04-11 19:03 UTC] matthieu dot aubry at gmail dot com
[2007-04-11 19:16 UTC] tony2001@php.net
[2007-04-11 20:24 UTC] stas@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 16:00:02 2025 UTC |
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