7

Possible Duplicate:
PHP Fatal error: Out of memory (allocated 80740352) (tried to allocate 12352 bytes) in

We all know this error:

Fatal error: Out of memory (allocated 32016932) (tried to allocate 25152 bytes)

Is there a way to handle this error? Maybe a function call that is executed? This isn't a true stack overflow, it's a limit imposed by the system. One would think there would be a little bit of flexibility here. I cache a lot of data locally in memory for processing, but I have a garbage collection function. I already have the function setup to check the memory usage and free memory if it's over a certain limit, but I miss sometimes. If I could call my function on this "fatal" error... Well, in an ideal world, the program should be able to recover.

My php.ini setting is at memory_limit = 500M.

Any thoughts?


Many thanks to everyone who offered a comment on this, but extra thanks to @Wrikken, for his excellent suggestion.

Here's what I ended up doing:

declare(ticks=1);
register_tick_function('TickFunction');
function TickFunction() {
    if(memory_get_usage()>220000000) // 220M
    {
        GlobalDataStore::CheckMemory(); // my garbage collection function   
    }
}

I have the php.ini memory limit set much higher than I would actually like it to run, and the tick function takes care of the memory monitoring and freeing memory on an as needed basis.

Community
  • 1
  • 1
Beachhouse
  • 4,972
  • 3
  • 25
  • 39
  • 2
    `memory_limit = XM` in your php.ini file? – Pedro del Sol Dec 11 '12 at 16:35
  • 1
    This might be of help: http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error – N.B. Dec 11 '12 at 16:37
  • 1
    As the link I posted suggest - you should be able to register a shutdown function, thus recovering from the fatal error. I haven't tried it so I can't guarantee it works. – N.B. Dec 11 '12 at 16:38
  • 2
    you can't recover from the FATAL error, and you can't be 100% certain anything you place inside the register shutdown function will execute properly since the memory might be too low. In other words doing an echo of too big a string or executing a `mail()` call will not always be possible. – Anthony Hatzopoulos Dec 11 '12 at 16:42
  • 2
    If you say your limit is a suggestion only... disable it, and let your function impose only your suggested limit. You will be responsible for calling your memory/garbagecontroller though. The amount of memory makes me think this is not taking place in a webserver, so `register_tick_handler()` could work. Make your callback _very_ lightweight though. – Wrikken Dec 11 '12 at 16:46
  • Thanks for the comments here, I found http://www.brettbrewer.com/content/view/123/1/. I'm trying to test that now. I was under the impression that register_shutdown_function never returned the execution cursor back to the original code file, and it just exited on completion of the shutdown function. – Beachhouse Dec 11 '12 at 16:46
  • so if I remove the memory limit, what would happen if I hit the server limit of 750M. Stack overflow and PHP would go down? -- this is for a cron job. -- I'm honestly about to resort to rewriting this is in C and running off the webserver totally. That may be a more sensible option. – Beachhouse Dec 11 '12 at 16:49
  • 2
    @Beachehouse: depens, but OOM-errors usually mean random-process-kill. So. that's what the limit is for. But you claim you don't want a hard one. Unless all this could be solved by setting the limit to the max available memory, ever, and having your callback enforce a (much) smaller one... and making sure you don't eat memory away in to big chunks (going from below soft limit to above hard limit). – Wrikken Dec 11 '12 at 16:53
  • @N.B. It looks like register_shutdown_function does not return the execution cursor to the code file. – Beachhouse Dec 11 '12 at 16:53
  • @Wrikken DUDE. you rock. Working like charm. Code below. – Beachhouse Dec 11 '12 at 17:09
  • Nice job, @Wrikken you should post your suggestions as an answer so that it can be accepted. – ajacian81 Dec 11 '12 at 19:47

0 Answers0