# Allowed memory size exhausted

A fatal error: the script asked for more memory than memory_limit allows. Read the data in smaller pieces first, and raise the limit only after that.

Message:

```text
Allowed memory size of 134217728 bytes exhausted (tried to allocate 2097152 bytes)
```

## What it means

PHP gives every request and every command a memory budget, set by `memory_limit`. Your code asked for one more block (the bytes in brackets), and that block would have taken the total past the budget, so PHP stopped the script on that line. 134217728 bytes is 128M, the PHP default. This is a fatal error, not an exception: a `try` with `catch (Throwable $e)` around the code never runs, only shutdown functions do.

## Why it happens

- Loading a whole table or file at once. `Model::all()`, `->get()` on a large query, or `file_get_contents()` on a big export keeps every row or byte in memory at the same time.
- A loop that keeps collecting. Appending to an array for every record of a source that grows, or a static cache that never empties in a long-running worker.
- A recursion that does not stop and carries data into every call.
- A lower limit on the web than on the command line. PHP-FPM can read another `php.ini` or set the limit per pool, so a command that works in your terminal can fail in a request: a CLI at 512M next to an FPM pool at 128M, for example.

## How to fix it

Read less at a time. A generator hands out one item at a time instead of building the whole array first:

```php
<?php

function readLines(string $path): Generator
{
    $handle = fopen($path, 'r');

    while (($line = fgets($handle)) !== false) {
        yield $line;
    }

    fclose($handle);
}

foreach (readLines('/var/data/import.csv') as $line) {
    // handle one line
}
```

In Laravel, Eloquent does the same for queries. `chunk` passes a batch of models to a closure, `lazy` gives you one stream of models that is fetched in chunks behind the scenes, and `cursor` runs one query and builds one model at a time:

```php
<?php

use App\Models\Order;

foreach (Order::where('status', 'open')->lazy() as $order) {
    // one model at a time
}
```

The Laravel docs add two limits: `cursor` cannot eager load relations, and it can still run out of memory on a very large result because PDO buffers the raw rows. Use `lazy` there. When you update the column you filter on, use `chunkById` or `lazyById`.

Then look at the limit. On the command line:

```bash
php -r 'echo ini_get("memory_limit"), PHP_EOL;'
php --ini
```

For the web, return `ini_get('memory_limit')` from a request, because the value there can differ. An FPM pool can set it for all its requests:

```ini
php_admin_value[memory_limit] = 256M
```

Code cannot raise a `php_admin_value` with `ini_set()`; it can raise a `php_value`.

Raise the limit last. A higher limit only postpones the error until the table or file grows again, and every FPM worker may use that much memory at the same moment, so a generous limit can run the whole server out of memory instead of stopping one request.

## Find it in production

A visitor gets a 500. Laravel with `APP_DEBUG=false` shows its plain "Server Error" page. Plain PHP with `display_errors` off sends an empty 500 and writes `PHP Fatal error: Allowed memory size ...` to the PHP error log; with `display_errors` on, the message lands in the page and the status is 200.

Laravel does report it. `Illuminate\Foundation\Bootstrap\HandleExceptions` keeps 32 KB in reserve, frees it in a shutdown function, reads `error_get_last()` and hands the fatal error to the exception handler as `Symfony\Component\ErrorHandler\Error\FatalError`. It appears in `storage/logs/laravel.log` with the file and line, but without a stack trace. Because Laravel reports it, [Ranetrace error monitoring](/features/error-monitoring) records it with the request it happened on.

Reproduced on PHP 8.5, Laravel 13.45, 5 December 2026.

Read more: https://www.php.net/manual/en/ini.core.php

## Related

### Terms

- [Lazy collections in Laravel](https://ranetrace.com/glossary/lazy-collections-in-laravel.md)
- [Eloquent ORM in Laravel](https://ranetrace.com/glossary/eloquent-orm-in-laravel.md)

### Other errors

- [Maximum execution time exceeded](https://ranetrace.com/error-guides/php-maximum-execution-time-exceeded.md)
- [The POST data is too large](https://ranetrace.com/error-guides/laravel-post-data-too-large.md)

Read this guide in a browser: https://ranetrace.com/error-guides/php-allowed-memory-size-exhausted
