Safely catch a 'Allowed memory size exhausted' error in PHP
As this answer suggests, you can use register_shutdown_function()
to register a callback that'll check error_get_last()
.
You'll still have to manage the output generated from the offending code, whether by the @
(shut up) operator, or ini_set('display_errors', false)
ini_set('display_errors', false);
error_reporting(-1);
set_error_handler(function($code, $string, $file, $line){
throw new ErrorException($string, null, $code, $file, $line);
});
register_shutdown_function(function(){
$error = error_get_last();
if(null !== $error)
{
echo 'Caught at shutdown';
}
});
try
{
while(true)
{
$data .= str_repeat('#', PHP_INT_MAX);
}
}
catch(\Exception $exception)
{
echo 'Caught in try/catch';
}
When run, this outputs Caught at shutdown
. Unfortunately, the ErrorException
exception object isn't thrown because the fatal error triggers script termination, subsequently caught only in the shutdown function.
You can check the $error
array in the shutdown function for details on the cause, and respond accordingly. One suggestion could be reissuing the request back against your web application (at a different address, or with different parameters of course) and return the captured response.
I recommend keeping error_reporting()
high (a value of -1
) though, and using (as others have suggested) error handling for everything else with set_error_handler()
and ErrorException
.
How to recover from a fatal error Allowed memory size exhausted
This error is a fatal error - that means you cannot recover from it. If PHP has hit it's memory limit, it won't be able to allocate any more memory to create your exception and any other memory it needs to carry on its execution.
There is another type of error - "catchable fatal error" which as the name suggests, can be caught in a try/catch, but unfortunately the memory size allocation is not one of them.
How to handle Allowed memory size of X bytes exhausted before the fatal error occurs?
I ended up catching the fatal error with register_shutdown_function()
and display a nice error page with layout.
Related Topics
PHP Glob - Scan in Subfolders For a File
How to Get JavaScript Function Data into a PHP Variable
Best Way to Completely Destroy a Session - Even If the Browser Is Not Closed
Preserve Key Order (Stable Sort) When Sorting With PHP'S Uasort
Import CSV File Directly into MySQL
Using Default Arguments in a Function
Passing an Array as Arguments, Not an Array, in PHP
Checking to See If One Array'S Elements Are in Another Array in PHP
Rebase Array Keys After Unsetting Elements
How to Locate the PHP.Ini File (Xampp)
Access Controller Method from Another Controller in Laravel 5
PHP Curl Not Working - Wamp on Windows 7 64 Bit
How to Read Last Lines (I.E. "Tail") from a File Using PHP
Read Each Line of Txt File to New Array Element
Convert Timestamp to Readable Date/Time PHP
Super Fast Getimagesize in PHP
How to Detect a Create, Update, Delete Query Is Successful in Codeigniter