Skip to content
Advertisement

memory exhausted by using around 2MB of memory

I am struggling with a php that has memory problem.

The script dies with the message: PHP Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 3027408 bytes) in test.php on line 9

I created a small test script:

<?php
function memstat($file, $line) 
{
    $memlog = fopen('mem.log', "a+");
    fwrite($memlog, sprintf('mem @ %s %d: %d (%d; peek: %d)'.PHP_EOL, $file, $line, memory_get_usage(), memory_get_usage(true), memory_get_peak_usage()));
    fclose($memlog);
}
memstat(__FILE__, __LINE__);
$str = str_pad('', 1523687, ' ');
memstat(__FILE__, __LINE__);
print 'end '.strlen($str);
?>

This script runs on php 7.4 without crash, but when I increase the str_pad length parameter to 1523688 then it dies with the message above. And this script runs on php7.2 when the length parameter is 1515495 but crash when it is 1515496. And I tested it with php5.6 on the same server and it runs without any problems.

The script crash no matter how I run it, apache or cli. The apache has been restarted after the ini modifications, that is not the problem.

I tested it with 2GB memory_limit and I got the same error message. It only works if I change the memory_limit to -1, but I do not think this is the correct solution, I mean there is something wrong if 512MB is not enough for this little script. It looks to me that php allocates 2097152 memory when it starts running and the first time it tries to allocate more system memory it crashes.

On my own computer with the same apache and php 7 versions, there is no problem.

Environment:

Proc: Intel Xeon E5-2620

Mem: 8GB

Op: Win10 Pro 64bit

Web Server: Apache/2.4.29 (Win64) Apache Lounge VC11 Server built: Nov 6 2017 11:17:28

Php: 7.4.8 and 7.2.32

Do somebody has an idea what can cause an error like this?

edit

Pilan wrote a slightly different script. I executed the script on the problematic computer and this was the result:

string(3) "2GB"
mem @ C:webtest.php 15: 0.37 MB (2.00 MB; peak: 0.41 MB)

Fatal error: Allowed memory size of 2097152 bytes exhausted (tried to allocate 10000024 bytes) in C:webtest.php on line 16

As you can see the memory_limit is 2GB and it still dies when it tries to allocate more than 2MB.

Advertisement

Answer

Have a look at your memory_limit. The error says Allowed memory size of 2097152 bytes exhausted. Which is not 2GB

console.log(2097152 / 2**20 + "MB");
var_dump(ini_get('memory_limit'));

function memstat($file, $line) {
    echo sprintf(
        'mem @ %s %d: %.2f MB (%.2f MB; peak: %.2f MB)' . PHP_EOL,
        $file,
        $line,
        memory_get_usage() /2**20,
        memory_get_usage(true) /2**20,
        memory_get_peak_usage() /2**20
    );
}

memstat(__FILE__, __LINE__);
$str = str_pad('', 9999999, ' ');
memstat(__FILE__, __LINE__);
print 'end '.strlen($str);

Working example.

output

string(3) "64M"
mem @ /in/BHqWd 16: 0.37 MB (2.00 MB; peak: 0.41 MB)
mem @ /in/BHqWd 18: 9.91 MB (11.54 MB; peak: 9.91 MB)
end 9999999
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement