Error message

PHP: Allowed memory size of … bytes exhausted

A PHP script asked for more memory than memory_limit allows. PHP stops it with a fatal error, not an exception: no catch block runs. The Sentry SDK still reports it, because it keeps a little memory in reserve for exactly this case, but the event holds less than you would expect: a single frame, the line where memory ran out.

Updated

Examples run on 21 November 2026 with PHP 8.6.0, sentry/sentry 4.17.1, 4.27.0 and 4.38.0, and Laravel 13.43.0 with sentry/sentry-laravel 4.22.0, 4.26.0 and 4.31.0. Errilo is in private beta.

What the message says

A loop that keeps adding 1 MiB strings to an array, with a 32 MiB limit:

oom.php
<?php
// oom.php, run with: php -d memory_limit=32M oom.php
function load(): array {
    $rows = [];
    while (true) { $rows[] = str_repeat('x', 1024 * 1024); }   // line 7
}
load();

PHP 8.6.0 prints the message, then the call stack:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2097152 bytes) in /app/oom.php on line 7
Stack trace:
#0 /app/oom.php(7): str_repeat('x', 1048576)
#1 {main}

Common causes and fixes

The same 1.5 million lines, with a 64 MiB limit: building the list ends with the fatal error above, reading them with a generator peaked at 2.0 MiB.

import.php
<?php
// 1.5 million lines, php -d memory_limit=64M
function allLines(int $n): array {            // builds the whole list: Fatal error at 64 MiB
    $r = [];
    for ($i = 0; $i < $n; $i++) { $r[] = "order-$i,19.90,paid"; }
    return $r;
}
function eachLine(int $n): Generator {        // one line at a time: peak memory 2.0 MiB
    for ($i = 0; $i < $n; $i++) { yield "order-$i,19.90,paid"; }
}
foreach (eachLine(1_500_000) as $line) { /* process $line */ }

What the PHP SDK captures

The SDK registers a shutdown function and keeps 16 KiB in reserve, which it frees when the script dies so there is room to build the event (read in ErrorHandler.php; the same in the three versions below). For a memory error, it also raises memory_limit by 5 MiB, once, so it can send. With plain sentry/sentry 4.17.1, 4.27.0 and 4.38.0, one event arrived in each case:

index.php
\Sentry\init(['dsn' => 'https://<key>@ingest.errilo.com/<project>']);

With Laravel 13.43.0 and sentry/sentry-laravel 4.22.0, 4.26.0 and 4.31.0 (Integration::handles() in bootstrap/app.php), the error goes through Laravel’s own handler and the event differs:

bootstrap/app.php (excerpt)
->withExceptions(function (Exceptions $exceptions) {
    Integration::handles($exceptions);
})

The setup is in the Laravel guide. We did not run this error on Symfony; the Symfony guide covers the setup.

When the process is killed from outside, nothing is sent: PHP never reaches its shutdown function. That is what happens when a container’s memory limit is lower than memory_limit: the kernel stops the process first. We checked that a SIGKILL sends nothing (see the execution time page); we did not run a container with a memory limit.

How Errilo shows it

The issue is titled with the type and the whole message, numbers included (Sentry\Exception\FatalErrorException: Error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 2097152 bytes)), with an Unhandled badge, level Fatal with the plain SDK and Error with Laravel, where the event has no level. Its location is the file of the single frame.

Errilo groups on the exception type and the frames, not on the message or the line. We ran Errilo’s grouping code on the events: two runs with a 32 MiB and a 64 MiB limit, in the same file, gave the same fingerprint, and so did a loop and a single 200 MiB allocation in another file. Two different files gave two issues. So one issue gathers every memory error that ends in the same file, whatever the limit and the size, and it points to where memory ran out. To find where it went, look at the breadcrumbs and the request, then profile.

Early access

The private beta is open, by invitation, to a handful of teams running a JavaScript, TypeScript, PHP or Python app in production. Join the waitlist and we’ll write to invite you.