Skip to content

Commit

Permalink
[8.x] Increase reserved memory for error handling (#42646)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
spawnia and taylorotwell authored Jun 3, 2022
1 parent 5094350 commit 6d2d78c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Foundation/Bootstrap/HandleExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class HandleExceptions
*/
public function bootstrap(Application $app)
{
self::$reservedMemory = str_repeat('x', 10240);
self::$reservedMemory = str_repeat('x', 32768);

$this->app = $app;

Expand Down Expand Up @@ -203,6 +203,8 @@ protected function renderHttpResponse(Throwable $e)
*/
public function handleShutdown()
{
self::$reservedMemory = null;

if (! is_null($error = error_get_last()) && $this->isFatal($error['type'])) {
$this->handleException($this->fatalErrorFromPhpError($error, 0));
}
Expand All @@ -217,8 +219,6 @@ public function handleShutdown()
*/
protected function fatalErrorFromPhpError(array $error, $traceOffset = null)
{
self::$reservedMemory = null;

return new FatalError($error['message'], 0, $error, $traceOffset);
}

Expand Down

0 comments on commit 6d2d78c

Please sign in to comment.