|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-02-26 13:35 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 21:00:01 2025 UTC |
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