Skip to content

Commit

Permalink
[8.x] Increase reserved memory for error handling (laravel#42646)
Browse files Browse the repository at this point in the history
* Increase reserved memory for error handling

Follow up to laravel#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 laravel#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
2 people authored and chu121su12 committed Jun 6, 2022
1 parent 8c84308 commit 3fd4de5
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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);

static::$app = $app;

Expand Down Expand Up @@ -220,6 +220,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 Down

0 comments on commit 3fd4de5

Please sign in to comment.