PHP Fatal error: Allowed memory size exhausted - Rocketeers app

  [ Rocketeers ](/)   

[Login](https://rocketeersapp.com/login) 

 On this page

 Knowledge
---------

PHP Fatal error: Allowed memory size exhausted
==============================================

### [\#PHP](https://rocketeersapp.com/knowledge/php)

This fatal error means a PHP script tried to use more memory than the configured limit. Raise the limit, but first ask why the script needs that much.

 Published by [Mark van Eijk](https://rocketeersapp.com/author/mark-van-eijk) on June 23, 2026 · 1 minute read

1. [About the error](#content-about-the-error)
2. [Why do I see this error](#content-why-do-i-see-this-error)
3. [Solution](#content-solution)

[\#](#content-about-the-error "Permalink")About the error
---------------------------------------------------------

The full message looks like this:

 ```
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

```

The number `134217728` is the limit in bytes, in this case 128 MB. PHP refused to give the script any more memory and stopped it. Because it is a fatal error, it usually surfaces as a blank page or a [500 error](/500-internal-server-error-laravel) in the browser.

[\#](#content-why-do-i-see-this-error "Permalink")Why do I see this error
-------------------------------------------------------------------------

PHP caps how much memory a single script may use through the `memory_limit` setting. A script hits that cap for one of two reasons:

- It genuinely needs more, for example exporting a large dataset or processing a big file.
- It has a problem: loading thousands of Eloquent models at once, an accidental infinite loop, or building a huge array in memory.

The amount it "tried to allocate" is a hint. A tiny allocation (like 20 KB above) failing means the script was already sitting right at the limit.

[\#](#content-solution "Permalink")Solution
-------------------------------------------

To raise the limit globally, edit `memory_limit` in your `php.ini`:

 ```
memory_limit = 256M

```

For a single CLI command without touching config:

 ```
php -d memory_limit=512M artisan some:command

```

And to set it from inside a script at runtime (use sparingly):

 ```
ini_set('memory_limit', '512M');

```

After editing `php.ini`, reload PHP-FPM so the change takes effect:

 ```
systemctl reload php8.3-fpm

```

Before you reach for a bigger number, check whether the code is the real culprit. In Laravel, the classic cause is loading everything at once:

 ```
// Loads every row into memory at once
$users = User::all();

// Streams rows in small batches instead
User::chunk(500, function ($users) {
    // ...
});

```

Use `chunk()`, `cursor()`, or `lazy()` for large result sets. Setting `memory_limit = -1` (unlimited) hides the problem and risks taking the whole server down, so avoid it in production.

### Subscribe to our newsletter

Do you want to receive regular updates with fresh and exclusive content to learn more about web development, hosting, security and performance? Subscribe now!

  Fill in your email address to receive updates  Subscribe 

#### More in [\#PHP](https://rocketeersapp.com/knowledge/php)

- [How to run PHP files](https://rocketeersapp.com/knowledge/run-php-files)
- [PHP file upload exceeds upload\_max\_filesize](https://rocketeersapp.com/knowledge/php-file-upload-exceeds-upload-max-filesize)
- [PHP Warning: Undefined array key](https://rocketeersapp.com/knowledge/undefined-array-key-php)
- [PHP: Call to a member function on null](https://rocketeersapp.com/knowledge/call-to-a-member-function-on-null)
- [PHP Fatal error: Class "X" not found](https://rocketeersapp.com/knowledge/php-class-not-found)
- [Composer out of memory (allowed memory size exhausted)](https://rocketeersapp.com/knowledge/composer-out-of-memory-allowed-memory-size-exhausted)

 [View all 11 articles →](https://rocketeersapp.com/knowledge/php)
