Ben
Ben

Reputation: 62444

Tracing a PHP "out of memory" error

I'm building an application with the Yii framework and am trying to determine the cause of an out of memory error. Is it possible to get a stack trace? I've tried doing something like...

function handleShutdown() {
    debug_print_backtrace();
    $error = error_get_last();
    $info = "[SHUTDOWN] file:".$error['file']." | ln:".$error['line']." | msg:".$error['message'] .PHP_EOL;
    echo $info;
}
register_shutdown_function('handleShutdown');

But the debug_print_backtrace() doesn't show anything but

#0 handleShutdown()
[SHUTDOWN] file:C:\Users\bkuhl\htdocs\instaLabel\yii-1.1.12\base\CModule.php | ln:530 | msg:Allowed memory size of 67108864 bytes exhausted (tried to allocate 65488 bytes)

I've checked CModule.php and there's no line number 530 in that file. It only goes to 518.

Upvotes: 6

Views: 4695

Answers (5)

Roman
Roman

Reputation: 188

Actually, there is a way to intercept Out Of Memory error like it was done in Symfony.

Symfony reserves some memory(32k) for fatal errors handling and then release it in case of exception.

Unfortunately, still, there is no way to get correct stack trace via code but you can set breakpoint in xDebug on your handler specified in register_shutdown_function and see correct stack trace.

Upvotes: 1

igorsantos07
igorsantos07

Reputation: 4686

It's not possible to find the backtrace from a Fatal Error using the shutdown function since this function is called from the PHP lifecycle, instead of some other piece of code. Thus, your stacktrace is correct - that's the stacktrace of the shutdown function, which is empty :(

Upvotes: 0

Ben
Ben

Reputation: 62444

In my case, this was happening because I had infinite function recursion. When I added the xDebug extension to PHP it threw an accurate error due to the function call limit.

Upvotes: 3

Gung Foo
Gung Foo

Reputation: 13558

Profile your app with xdebug. It should tell you which function is called how often and how much memory it eats.

Upvotes: 1

Anthony Hatzopoulos
Anthony Hatzopoulos

Reputation: 10547

Keep in mind because of the fatal memory error it is difficult to debug simply because, well, you might not have enough memory left to run your debugger.

For example if I have 100 bytes left I won't be able to run much. So the results of your shutdown function will vary.

Now as for your line number that doesn't exist.. is it possible the file is running an eval(), pulling in another file or outputting something that may cause the erroneous line number? I would read that file and the calling files and see how it's ran. Sorry I can't help further, I'm not familiar with the Yii framework.

Upvotes: 0

Related Questions