Skip to content

Commit

Permalink
Increase memory during shutdown for Fatal Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam authored and markstory committed Dec 28, 2015
1 parent dd659e2 commit a77d0c4
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/Error/BaseErrorHandler.php
Expand Up @@ -72,6 +72,13 @@ public function register()
if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg')) {
return;
}
$megabytes = Configure::read('Error.extraFatalErrorMemory');
if ($megabytes === null) {
$megabytes = 4;
}
if ($megabytes !== false && $megabytes > 0) {
static::increaseMemoryLimit($megabytes * 1024);
}
$error = error_get_last();
if (!is_array($error)) {
return;
Expand Down Expand Up @@ -212,6 +219,36 @@ public function handleFatalError($code, $description, $file, $line)
return true;
}

/**
* Increases the PHP "memory_limit" ini setting by the specified amount
* in kilobytes
*
* @param string $additionalKb Number in kilobytes
* @return void
*/
public static function increaseMemoryLimit($additionalKb)
{
$limit = ini_get("memory_limit");
if (!is_string($limit) || !strlen($limit)) {
return;
}
$limit = trim($limit);
$units = strtoupper(substr($limit, -1));
$current = substr($limit, 0, strlen($limit) - 1);
if ($units === "M") {
$current = $current * 1024;
$units = "K";
}
if ($units === "G") {
$current = $current * 1024 * 1024;
$units = "K";
}

if ($units === "K") {
ini_set("memory_limit", ceil($current + $additionalKb) . "K");
}
}

/**
* Log an error.
*
Expand Down

0 comments on commit a77d0c4

Please sign in to comment.