#41053 [NEW]: Filling Array with integers : memory usage too high

From: Date: Wed, 11 Apr 2007 13:26:37 +0000
Subject: #41053 [NEW]: Filling Array with integers : memory usage too high
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-111347@lists.php.net to get a copy of this message
From: matthieu dot aubry at gmail dot com Operating system: linux PHP version: 5.2.1 PHP Bug Type: Performance problem Bug description: Filling Array with integers : memory usage too high 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 bug report at http://bugs.php.net/?id=41053&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=41053&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=41053&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=41053&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=41053&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=41053&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=41053&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=41053&r=needscript Try newer version: http://bugs.php.net/fix.php?id=41053&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=41053&r=support Expected behavior: http://bugs.php.net/fix.php?id=41053&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=41053&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=41053&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=41053&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=41053&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=41053&r=dst IIS Stability: http://bugs.php.net/fix.php?id=41053&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=41053&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=41053&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=41053&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=41053&r=mysqlcfg

« previous php.bugs (#111347) next »