From: flavius dot as at gmail dot com Operating system: Linux PHP version: 5.3.1 PHP Bug Type: *Programming Data Structures Bug description: serialize() crashes with unreasonable/unexplicable "out of memory" for objects
Description: ------------ When serializing a SplFixedArray with serialize(), the script dies with "Fatal error: Allowed memory size of 134217728 bytes exhausted" The "expected result" works and allocates at most 20.96 mb for $cnt = 8565 on line 15. The "actual result" crashes when serialize()'ing with $cnt only incremented by one, which is not understandable. The actual values may vary, but if you play enough with it you'll find at which amount of items serialize() has that spark. Then you can toggle to using plain arrays on line 17, and that problem will disappear, although arrays actually consume more memory (in my experiments around 1.2 mb more). Reproduce code: --------------- 1 <?php 2 function rand_str($length = 32, $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz1234567890') { 3 $chars_length = (strlen($chars) - 1); 4 $string = $chars{rand(0, $chars_length)}; 5 for ($i = 1; $i < $length; $i = strlen($string)) { 6 $r = $chars{rand(0, $chars_length)}; 7 if ($r != $string{$i - 1}) $string .= $r; 8 } 9 return $string; 10 } 11 function convert($size) { 12 $units=array('b','kb','mb','gb','tb','pb'); 13 return @round($size/pow(1024,($unit=floor(log($size,1024)))),2).' '.$units[$unit]; 14 } 15 $cnt = 8566; 16 $r = new SplFixedArray($cnt); 17 //$r = array(); 18 for($i=0;$i<$cnt;$i++) { 19 $r[$i] = rand_str(1024); 20 } 21 echo "\nGENERATING DONE\n"; 22 echo 'peak before serialize() ',convert(memory_get_peak_usage()),PHP_EOL; 23 echo "---\n"; 24 $r = serialize($r); 25 echo 'peak after dumping ',convert(memory_get_peak_usage()),PHP_EOL; 26 unset($r); Expected result: ---------------- GENERATING DONE peak before serialize() 10.19 mb --- peak after dumping 20.96 mb Actual result: -------------- GENERATING DONE peak before serialize() 10.19 mb --- Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 8913127 bytes) in /home/flav/softpedia/bigarray-bugreport.php on line 24 -- Edit bug report at http://bugs.php.net/?id=51155&edit=1 -- Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51155&r=trysnapshot52 Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51155&r=trysnapshot53 Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51155&r=trysnapshot60 Fixed in SVN: http://bugs.php.net/fix.php?id=51155&r=fixed Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51155&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=51155&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=51155&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=51155&r=needscript Try newer version: http://bugs.php.net/fix.php?id=51155&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=51155&r=support Expected behavior: http://bugs.php.net/fix.php?id=51155&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=51155&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=51155&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=51155&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51155&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=51155&r=dst IIS Stability: http://bugs.php.net/fix.php?id=51155&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=51155&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=51155&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=51155&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=51155&r=mysqlcfg