|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2007-01-24 18:42 UTC] tdondich at groundworkopensource dot com
Description:
------------
Using:
- Apache 2.2.4
- PHP 5.2.0 w/ sessions
Description:
When running this simple script, Apache2 will consume all available memory and the script will not complete (with no memory limit and high time limit) until all memory is taken by Apache 2 process and system starts to fail (swap extensively until nothing is responsive).
Reproduce code:
---------------
<?php
session_start();
$myarray = array();
$numOfIterations = 100000; // Set to 10000 to see the script succeed
for($counter = 0; $counter < $numOfIterations; $counter++) {
$myarray[$counter] = 'jkdfjklsdfjklfsdhjklsdfhildfnifsdnsdfnlfsdnklfsdnkglgfnkl;nmklsdf';
}
// copy the array, just for kicks
$_SESSION['test'] = $myarray;
sleep(5); // See the memory usage in top for awhile
print("Completed.");
?>
Expected result:
----------------
Apache2 process (pre-fork) will consume all available memory and continue to climb until system swaps excessively and then becomes unresponsive.
Changing the $numOfIterations to 10000 causes it to succeed.
By my account, the memory needed approx for each array is:
100000 X 66 = 6600000bytes
6600000 / 1048576 = 6 megabytes
Even when copying it to $_SESSION, it should only take 12 megs, plus maybe a little more for overhead (array constructs, program execution). We're seeing consumption of over a gigabyte and growing with one script execution.
Actual result:
--------------
TAKEN FROM TOP:
Mem: 1035632k total, 1021568k used, 14064k free, 1208k buffers
Swap: 3028212k total, 432996k used, 2595216k free, 40336k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
16094 tdondich 16 0 344m 86m 5172 S 1 8.6 15:16.51 java
4205 root 15 0 197m 19m 3960 S 1 1.9 31:37.61 Xorg
28209 daemon 18 0 779m 704m 2148 D 1 69.7 0:04.34 httpd
No other pages being served on this box. The script was continuing to execute at this time.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 18:00:01 2025 UTC |
I get this output with fatal error: Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 18874411 bytes) in Unknown on line 0 With this script: <?php session_start(); $myarray = array(); $numOfIterations = 1000000; for($counter = 0; $counter < $numOfIterations; $counter++) { $myarray[$counter] = 'jkdfjklsdfjklfsdhjklsdfhildfnifsdnsdfnlfsdnklfsdnkglgfnkl;nmklsdf'; } $_SESSION['test'] = $myarray; print("SLEEPING<br />"); sleep(5); ?> Because you get the SLEEPING output, it appears the high memory consumption is occurring when the script has finished, possibly when the session is trying to be serialized. I'm running with PHP Version 5.2.1RC4-devUPDATE: Modified the script: <?php session_start(); $myarray = array(); $numOfIterations = 1000000; for($counter = 0; $counter < $numOfIterations; $counter++) { $myarray[$counter] = 'jkdfjklsdfjklfsdhjklsdfhildfnifsdnsdfnlfsdnklfsdnkglgfnkl;nmklsdf'; } $_SESSION['test'] = $myarray; print("SLEEPING<br />"); print("USAGE: " . memory_get_usage()); sleep(5); ?> I receive this output: SLEEPING USAGE: 160289476 Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 18874411 bytes) in Unknown on line 0I added some extra debug info to the script: <?php session_start(); $myarray = array(); $numOfIterations = 1000000; // Set to 10000 to see the script succeed for($counter = 0; $counter < $numOfIterations; $counter++) { $myarray[$counter] = 'jkdfjklsdfjklfsdhjklsdfhildfnifsdnsdfnlfsdnklfsdnkglgfnkl;nmklsdf'; if ( ($counter % 100000) == 0) { echo("Iterations: $counter, Memory: " . memory_get_usage() . "<br>\n"); } } // copy the array, just for kicks $_SESSION['test'] = $myarray; echo("USAGE: " . memory_get_usage() . "<br>\n"); echo("Writing Session Data."); session_write_close(); echo("Completed."); ?> And the output is: Iterations: 0, Memory: 42336 Iterations: 100000, Memory: 20291200 Iterations: 200000, Memory: 40539976 Iterations: 300000, Memory: 61837196 Iterations: 400000, Memory: 81037264 Iterations: 500000, Memory: 100237332 Iterations: 600000, Memory: 123631704 Iterations: 700000, Memory: 142831772 Iterations: 800000, Memory: 162031840 Iterations: 900000, Memory: 181231908 USAGE: 200431716 Writing Session Data. Fatal error: Out of memory (allocated 532152320) (tried to allocate 1834294 bytes) in /usr/local/www/apache22/data/foo.php on line 16 Completed. My environment is FreeBSD 6.2R, Apache 2.2.3, PHP 5.2.0, and memory_limit = 512M.