ID:               51155
 User updated by:  flavius dot as at gmail dot com
 Reported By:      flavius dot as at gmail dot com
 Status:           Open
 Bug Type:         *Programming Data Structures
 Operating System: ArchLinux x86_64
 PHP Version:      5.3.1
 New Comment:

Oh and I've forgot to mention, there's plenty of RAM and swap space
before running php -f:

free -m
             total       used       free     shared    buffers    
cached
Mem:          1975       1732        243          0        131      
1027
-/+ buffers/cache:        573       1402
Swap:         5718          0       5718


Previous Comments:
------------------------------------------------------------------------

[2010-02-26 13:38:28] flavius dot as at gmail dot com

Updated OS: ArchLinux x86_64

------------------------------------------------------------------------

[2010-02-26 13:35:13] flavius dot as at gmail dot com

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 this bug report at http://bugs.php.net/?id=51155&edit=1

Reply via email to