8000 [8.x] Increase reserved memory for error handling (#42646) · laravel/framework@6d2d78c · GitHub
[go: up one dir, main page]

Skip to content

Commit 6d2d78c

Browse files
[8.x] Increase reserved memory for error handling (#42646)
* Increase reserved memory for error handling Follow up to #42630 The error handler should be able to report exceptions arising from exceeding the PHP memory limit. Because of changes made to error handling, the previously configured value is no longer sufficiently large. A similar change was also done in Symfony a while ago, see symfony/symfony#44327. I used the following artisan command to determine the amount of memory required for error handling, using a reporter that simply writes to a file: ```php <?php declare(strict_types=1); namespace App\Console\Commands; use Illuminate\Console\Command; final class MeasureHandlerMemory extends Command { protected $signature = 'measure-handler-memory'; private int $peak; public function handle(): void { $this->peak = memory_get_peak_usage(); trigger_error('', E_USER_ERROR); } public function __destruct() { $used = memory_get_peak_usage() - $this->peak; echo "error handling used: " . $used . "\n"; $before = memory_get_usage(); $times = 10240; $reserve = str_repeat('x', $times); $after = memory_get_usage() - $before; echo 'repeat times ' . $times . ' reserves: ' . $after . "\n"; $ratio = $after / $times; echo 'ratio between bytes and repeat: ' . $ratio . "\n"; echo 'minimum times to repeat: ' . $used / $ratio . "\n"; } } ``` * Free memory in HandleExceptions::handleShutdown() While validating the effectiveness of #42630 in our application, I found that the call `$error = error_get_last()` causes a tiny bit of memory usage. Thus, I think it is better to clear memory as soon as entering the handler. * Update HandleExceptions.php Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 5094350 commit 6d2d78c

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/Illuminate/Foundation/Bootstrap/HandleExceptions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class HandleExceptions
3636
*/
3737
public function bootstrap(Application $app)
3838
{
39-
self::$reservedMemory = str_repeat('x', 10240);
39+
self::$reservedMemory = str_repeat('x', 32768);
4040

4141
$this->app = $app;
4242

@@ -203,6 +203,8 @@ protected function renderHttpResponse(Throwable $e)
203203
*/
204204
public function handleShutdown()
205205
{
206+
self::$reservedMemory = null;
207+
206208
if (! is_null($error = error_get_last()) && $this->isFatal($error['type'])) {
207209
$this->handleException($this->fatalErrorFromPhpError($error, 0));
208210
}
@@ -217,8 +219,6 @@ public function handleShutdown()
217219
*/
218220
protected function fatalErrorFromPhpError(array $error, $traceOffset = null)
219221
{
220-
self::$reservedMemory = null;
221-
222222
return new FatalError($error['message'], 0, $error, $traceOffset);
223223
}
224224

0 commit comments

Comments
 (0)
0