How to deliberately call "Fatal error: allowed memory size xxx bytes exhausted"

Whenever I get this error, I just increased the memory to fix it. I have a case where, for testing purposes, I want the page to use all the memory, but as far as I configured memory_limit.

I do not know how to do that.

EDIT: I tried this:

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

But that did not work. In the end, he simply printed "startstart", which is strange that he was printed twice ...

I need a simple code example, "put a lot of material into memory." It’s good that I know so much.

+6
php memory overflow
source share
7 answers

Here's the problem:

 $test = "a" + $test; 

+ in PHP for arithmetic, not for string concatenation. Using:

 $test = "a" . $test; 
+8
source share

All memory should be used.

 $a = 'x'; while (true) { $a = $a.$a; } 
+17
source share
 str_pad("",PHP_INT_MAX); 
+4
source share

You can do an endless loop, although I would advise you to do it.

You can also open / read in large memory files that exceed the memory limits, you can also write a loop that will generate a string with the number of bytes that will exceed the memory limit.

Which is better, has no idea. But there are several options available to you.

+1
source share
  • Download all Google Maps images.
  • Then re-size them with GD on a scale of 1-1.
+1
source share

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

+1
source share
 <?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); 
+1
source share

All Articles