1

i have a PHP CLI Application which needs a lot of memory. I set the memory_limit to -1 in the php.ini. Unfortunately i still get this issue:

Fatal error: Out of memory (allocated 870318080) (tried to allocate 138412032 bytes)

At this time my System had more than 4 GB of free memory (I have 12 GB installed). So this is not a hardware limit.

I also tried to set it in the code:

<?php
     ini_set('memory_limit','-1');
?>

What i also did was to set it to 2048M but the error was still there. It's always around 1GB so is there any hard coded 1GB limit?

What my System looks like:

  • Windows 7
  • PHP 5.6.20 (Thread safe 32bit without Suhosin and without safe_mode)

UPDATE:

Here is as Script to reproduce it:

<?php
ini_set("memory_limit", -1);
echo formatSizeUnits(memory_get_usage()).PHP_EOL;
for($i=0;$i<=10; $i++) {
    $content[$i] = file_get_contents("http://mirror.softaculous.com/apache/lucene/solr/6.0.0/solr-6.0.0.zip");
    echo formatSizeUnits(memory_get_usage()).PHP_EOL;
}

function formatSizeUnits($bytes)
{
    if ($bytes >= 1073741824)
    {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    }
    elseif ($bytes >= 1048576)
    {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    }
    elseif ($bytes >= 1024)
    {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    }
    elseif ($bytes > 1)
    {
        $bytes = $bytes . ' bytes';
    }
    elseif ($bytes == 1)
    {
        $bytes = $bytes . ' byte';
    }
    else
    {
        $bytes = '0 bytes';
    }

    return $bytes;
}

?>

My Output:

php test.php
123.37 KB
164.25 MB
328.37 MB
492.49 MB
PHP Fatal error:  Out of memory (allocated 688914432) (tried to allocate 171966464 bytes) in test.php on line 12
Fatal error: Out of memory (allocated 688914432) (tried to allocate 171966464 bytes) in test.php on line 12

UPDATE 2:

I installed a x64 PHP 5.6.20. Now I'm able to get over this limit. Is this an official limit in x86 PHP?

1 Answers1

0

I believe the theoretical limit a 32-bit process can handle is 3GB (there're of course tricks) but PHP in particular can hardly reach 1GB before crashing, at least in my experience.

You should at least attempt to optimise your code because to begin with you're loading a collection of ZIP archives in memory and there's probably not a good reason to.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thanks for your reply. The code above is just to reproduce the memory problem. So this is not how my code looks like in real live ;) – Tobias Tschech Apr 21 '16 at 06:24