Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intentionally cause a "Fatal error: Allowed memory size of xxx bytes exhausted"

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.

like image 254
phazei Avatar asked Sep 01 '10 23:09

phazei


1 Answers

Should eat all memory.

$a = 'x';
while (true) {
    $a = $a.$a;
}
like image 97
andrem Avatar answered Sep 20 '22 04:09

andrem