Ben
Ben

Reputation: 2077

How did my script run out of memory and execution time at the same time?

I knew I was going to run out of something, since I was running a very long task. That's not the problem.

I am just curious, how did I get both errors at the same time?

Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 71 bytes) in...

--

Fatal error: Maximum execution time of 300 seconds exceeded in Unknown on line 0

Shouldn't the first "fatal" error halt execution? I am running APC and php 5.2.6.

EDIT: Here is a screenshot

Upvotes: 5

Views: 721

Answers (2)

hakre
hakre

Reputation: 197757

It might be that you're running into a race-condition here.

It basically works for example triggering a FATAL in the shutdown function for the memory and a FATAL for the timeout earlier triggering the shutdown function:

register_shutdown_function(function() {
    # consume all memory
    echo "break my pain\n";
    $a = ['hello'];
    while($a[] = $a);
});

set_time_limit(1);
while(1) usleep(100);

It also works the other way round, first triggering the FATAL for the memory, then the FATAL for the timeout in the shutdown function:

register_shutdown_function(function() {
    set_time_limit(1);
    while(1) usleep(100);
});

ini_set('memory_limit', '1');
$a = array_fill(0, 1024, 'hello');
while($a[] = $a);

This does not yet 100% explain what happens inside Codeigniter, however I'd say the query takes too long therefore after returning triggering the execution time limit as well. This is somewhat hard to reproduce because of your older PHP version. If I now eval PHP code, I get the message that the code was eval'ed and in which line I find that eval command (PHP 5.4). Try to reproduce with PHP 5.4 to gain more information. You will also greatly benefit from the speed improvements we've seen since PHP 5.2. You're missing a lot.

Earlier:

Fatal error: Maximum execution time of 300 seconds exceeded in Unknown on line 0

This smells like a startup error.

What has triggered your first error is hard to say, you cutted it off. Either you have got eval'ed code here which might have been handled partly a-part via APC you could see two fatal errors in the same script.

However, try without APC if you can reproduce it. If you still can, please report it in the PHP bugtracker.

If you can't, please report the problem with the APC bugtracker.

Edit: Just seeing PHP 5.2.6, you might need to report and fix it your own though ;) - Try to reproduce with the current stable PHP version as well then.

Upvotes: 4

periklis
periklis

Reputation: 10192

One way that this can happen, is if you have a register_shutdown_function:

<?php
register_shutdown_function('shutdownFunction');
error_reporting(E_ALL);ini_set("display_errors", true);
ini_set("memory_limit", "5M");
ini_set("max_execution_time", 1);

for ($i=0;$i<1000000;$i++) {
    $val[] = rand();
}

function shutDownFunction() {
    echo "executed";
    for ($i=0;$i<100000000;$i++) {
        $val = rand();
    }
}

Outputs the same with what you got:

Fatal error: Allowed memory size of 5242880 bytes exhausted (tried to allocate 524288 bytes) in /home/periklis/workspace/build_script/test.php on line 8

executed

Fatal error: Maximum execution time of 1 second exceeded in /home/periklis/workspace/build_script/test.php on line 13

Upvotes: 0

Related Questions