Knowledge
PHP Fatal error: Allowed memory size exhausted
#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 on June 23, 2026 · 1 minute read
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 in the browser.
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.
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!
Related articles
How to run PHP files
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.
PHP Fatal error: Class "X" not found
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.