views:

78

answers:

7

Whenever I've received this error, I just increased the memory to fix it. I have a case where, for testing purposes, I want to cause a page to use up all the memory however big I set the memory_limit.

I have no idea how to go about doing that.

EDIT: I tried this:

<?php
echo "start";
@ini_set('memory_limit', '1M');
$test = "a";
while (1) {
    $test = "a" + $test;    
}
echo "done";
?>

But it didn't crash. At the end it just printed "startstart" which is strange that it was printed twice...

I'd like a simple code example, the "put a lot of stuff in memory".. well I know that much.

+1  A: 

You can do an infinite loop, although I would advise against that.

You can also open / read into memory big files that would exceed the memory limit, you could also write a loop that would generate a string with the amount of bytes which would exceed the memory limit.

Which is best, no clue. But there are a couple options available to you.

Brad F Jacobs
+4  A: 

Should eat all memory.

$a = 'x';
while (true) {
    $a = $a.$a;
}
andrem
Takes < 1 sec to exhaust my memory
andrem
I basically tried that, though it's slightly more efficient, it didn't provide any better results.
phazei
@phazei: See my answer. You had `$test = "a" + $test;` which is very different from `$test = "a" . $test;`
Josh
+2  A: 
  1. Download all Google Maps images.
  2. Then re-size them using GD into a 1-1 scale.
jakenoble
+1  A: 

Write a PHP function that tries to find a pattern within /dev/random

Rob Olmos
+3  A: 

Here's the problem:

$test = "a" + $test;

+ in PHP is for arithmetic, not string concatination. Use:

$test = "a" . $test;
Josh
*smacks self in head with keyboard multiple times*(I knew that... too much js lately)
phazei
@phazei: No problem, I have done that many times too, and I have been coding PHP since 2000!
Josh
+1  A: 
<?php
$limit = ini_get('memory_limit');
$last = strtolower($limit[strlen($limit)-1]);
switch($last) {
    case 'g':
        $limit *= 1024;
    case 'm':
        $limit *= 1024;
    case 'k':
        $limit *= 1024;
}
$limit = $limit + 1;//not needed actually, I assume the script has consumed 1 byte of memory by now...
$foo = `dd if=/dev/zero bs=$limit count=1`;
//or, if you don't like the command line:
$bar = str_repeat($a,$limit);
Wrikken
Actually, that +1 on limit is needed. Didn't crash otherwise.
phazei
Here there's more overhead then 0 bytes, as for instance settings are loaded etc. I'm rather surprised you have a 0 byte overhead on PHP scripts, but of course I cannot argue with your results.
Wrikken
With your script I get the "Allowed memory size exhausted" error. But I just realized if doing a while(1) { $byte=$byte.$byte} I get an "Out of memory" error long before hitting the allowed limit that the former error hits. Any ideas as to why?
phazei
Nope, no idea what the difference between those 2 is. You might want to grep in the PHP source to check under which conditions they occur. It might have something to do with getting the memory from an outside program, or being able to predict that doubling a string length will result in to much memory usage. Although here both errors are the same 'exhausted' one (PHP 5.3.2)
Wrikken
Did turn out to be memory issue with children processes on Apache. $foo caused an "out of mem" while $bar caused "allowed mem exhausted". Also while (1) { $holder[] = str_repeat($byte,1024*1024); } caused an "out of mem".Turns out it was all because of RLimitMEM and RLimitCPU in httpd.conf, was setting a hard memory limit on child processes.It seems the $bar should have caused an "out of mem" error as well, but since it tried to allocate it in a single step, it must be some bug where it's possible to get around the RLimitMEM setting.
phazei
+2  A: 
str_pad("",PHP_INT_MAX);
danorton