php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40222 Apache2 with simple PHP Script consumes all available memory
Submitted: 2007-01-24 18:42 UTC Modified: 2007-01-24 22:00 UTC
From: tdondich at groundworkopensource dot com Assigned:
Status: Not a bug Package: Apache2 related
PHP Version: 5.2.0 OS: Ubuntu Edgy
Private report: No CVE-ID: None
 [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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-24 18:43 UTC] tony2001@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.2-win32-latest.zip


 [2007-01-24 18:58 UTC] tdondich at groundworkopensource dot com
PHP Version 5.2.1RC4-dev,  php5.2-200701241730

Corrects the issue.

2 things:

1) Can we know what was the cause of the issue in case we need to do a work around?

2) When is PHP 5.2.1 scheduled to be released?
 [2007-01-24 19:02 UTC] tony2001@php.net
>1) Can we know what was the cause of the issue in case we
> need to do a work around?
I can't tell that having just the information you've provided.

>2) When is PHP 5.2.1 scheduled to be released?
Soon. RC3 is expected to be the last release candidate.
 [2007-01-24 20:58 UTC] tdondich at groundworkopensource dot com
Update.  I am still running 5.2-latest, however, I changed the script to have numOfIterations be equal to 1000000.  The script began the old behavior of ballooning and trying to consume as much memory as possible and the system started swapping and becoming unresponsive.

With 1000000 elements, the memory requirements should still be around:

1000000 X 66 = 66000000bytes = 62megabytes

The array is copied twice, and that should be 124megs.  We're continuing to see memory consumption skyrocket and consume all memory.

This does not occur when the number of elements is 100000.
 [2007-01-24 21:18 UTC] tony2001@php.net
Cannot reproduce.

100000 - 25.6Mb
1000000 - 256Mb

Use memory_get_usage() to get the memory usage.
 [2007-01-24 21:26 UTC] tdondich at groundworkopensource dot com
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-dev
 [2007-01-24 21:29 UTC] tdondich at groundworkopensource dot com
UPDATE:

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 0
 [2007-01-24 21:32 UTC] tony2001@php.net
Right, serializing is expected to consume even more memory, because it has to run malloc/memcpy/free a lot.
Storing 256Mb in session is noway a real task and quite a bad test, because it's not really clear what you're testing and what result you want to get.
 [2007-01-24 21:40 UTC] tdondich at groundworkopensource dot com
If it was performing malloc/memcpy/free, in that order, then it would be freeing the memory back via the free() system call.  Even if it's a bogus test, it's a test to fit your requirements.  Can you please clarify how the session saving is taking more than 3 times the memory usage the script says it's using?
 [2007-01-24 21:51 UTC] haroldp at sierraweb dot com
I 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.
 [2007-01-24 22:00 UTC] tony2001@php.net
>Can you please clarify how the session saving is taking
>more than 3 times the memory usage the script says it's >using?

You have that array in memory and a buffer which constantly grows. To grow the buffer you need to allocate another one, bigger then the previous, to copy data into it and free the small buffer. So in edge case, there will be array + small buf + big buf in the memory in one moment.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 02:01:32 2024 UTC