Browse Source

Increase memory during shutdown for Fatal Error handling

Adam 10 years ago
parent
commit
a77d0c4213
1 changed files with 37 additions and 0 deletions
  1. 37 0
      src/Error/BaseErrorHandler.php

+ 37 - 0
src/Error/BaseErrorHandler.php

@@ -72,6 +72,13 @@ abstract class BaseErrorHandler
             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;
@@ -213,6 +220,36 @@ abstract class BaseErrorHandler
     }
 
     /**
+     * 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.
      *
      * @param string $level The level name of the log.