|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2006-12-04 21:24 UTC] cnww at hfx dot andara dot com
Description: ------------ When called on a tree implemented as a multi-dimensional array occupying something like 60MB of RAM serialize fails with an error like: Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 6282420 bytes) in /var/www/lib/metacache.php on line 105 So either the memory allocation system is busted or the serialization of ~60MB of data (actually the full text version is only 9.1MB) requires over 1GB of memory. Reproduce code: --------------- The line that is actually failing is: $meta_ser = serialize( &$metacache_buf ); For obvious reasons I have not posted the whole array here. I created an example program that reproduces the problem using an array of 242,919 strings made with var_export() from the directory listing of a Windows machine's D: drive. You can get the code from: http://arachne.k-pcltee.com/~cnww/serialbug.bz2 The example program seems to require around 52MB of RAM to create the array, so if you're using the default 8MB limit it will fail before it gets to the serialize() call. Any limit of 64MB or greater seems to result in it failing during serialize(). Expected result: ---------------- It should print "Exiting normally" Actual result: -------------- Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 6010844 bytes) in /tmp/serialbug.php on line 313030 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 07:00:01 2025 UTC |
For the benefit of future viewers, the following function serializes the test array in about 1.4 seconds on my machine (vs. about 2 minutes for PHP's serialize(), but I expect that's mainly due to my function not needing swap space) and it does not appear to significantly affect the size of the PHP process in RAM (i.e. it serializes with "memory_limit = 64MB" on the same array that serialize() cannot handle with "memory_limit = 1024MB"). The output is identical (same MD5 checksum) as the output from serialize() on the same test array (I have not yet proven that the output is always the same as serialize()). function my_serialize( &$data, $buf = "" ) { if( is_array( &$data )) { $buf .= "a:" . count( &$data ) . ":{"; foreach( $data as $key => &$value ) { $buf .= serialize( $key ) . my_serialize( &$value ); } $buf .= "}"; return $buf; } else { return $buf . serialize( &$data ); } } The only serious problem I see with it is that if it's called on a non-array object that contains a complex array as a property then the array member will end up being handled by serialize().