From 20c22444baf8d627215b66e767aa0180e545931c Mon Sep 17 00:00:00 2001 From: adam1010 Date: Wed, 12 Aug 2015 22:56:25 -0500 Subject: [PATCH] Increase memory during shutdown for Fatal Error handling --- app/Config/core.php | 2 ++ lib/Cake/Core/App.php | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/app/Config/core.php b/app/Config/core.php index d2e163418..1f5aa5a1c 100644 --- a/app/Config/core.php +++ b/app/Config/core.php @@ -68,6 +68,8 @@ * - `renderer` - string - The class responsible for rendering uncaught exceptions. If you choose a custom class you * should place the file for that class in app/Lib/Error. This class needs to implement a render method. * - `log` - boolean - Should Exceptions be logged? + * - `extraFatalErrorMemory` - integer - Increases memory limit at shutdown so fatal errors are logged. Specify + * amount in megabytes or use 0 to disable (default: 4 MB) * - `skipLog` - array - list of exceptions to skip for logging. Exceptions that * extend one of the listed exceptions will also be skipped for logging. * Example: `'skipLog' => array('NotFoundException', 'UnauthorizedException')` diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index fa64f64f4..45ef3ff5c 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -888,6 +888,35 @@ class App { return static::$_packageFormat; } +/** + * 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"); + } + } + /** * Object destructor. * @@ -897,6 +926,14 @@ class App { * @return void */ public static function shutdown() { + $megabytes = Configure::read('Error.extraFatalErrorMemory'); + if ($megabytes === null) { + $megabytes = 4; + } + if ($megabytes !== false && $megabytes > 0) { + static::increaseMemoryLimit($megabytes * 1024); + } + if (static::$_cacheChange) { Cache::write('file_map', array_filter(static::$_map), '_cake_core_'); }