Ffile2fix
Sign in Get started

Fix "Allowed memory size exhausted" in PHP

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

PHP stopped the script because it tried to use more RAM than the memory_limit setting allows. The first number is the limit in bytes (134217728 = 128M); the second is just the last small allocation that tipped it over. Raise memory_limit if the job genuinely needs more, but if a small limit is hit by a normal page, look for a runaway loop or an oversized query instead.

Also appears as: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 4096 bytes) in /var/www/html/index.php on line 42 Β· PHP Fatal error: Out of memory (allocated 2097152) (tried to allocate 65536 bytes) Β· Allowed memory size of 1610612736 bytes exhausted (tried to allocate 4096 bytes) - composer

Common causes

  • memory_limit in php.ini or the pool config is too low for the workload (for example 64M or 128M)
  • Loading a whole database table or large file into an array instead of processing it in chunks
  • Infinite recursion or a loop that keeps appending to an array
  • Large image processing with GD or Imagick, where each pixel costs several bytes of RAM
  • Composer resolving dependencies, which can need well over 1 GB
  • A plugin, library or ORM that caches every loaded object for the life of the request

How to fix it

  1. Check the current limit. Run php -i | grep memory_limit for the CLI, or call phpinfo() in a temporary file for the web server. The CLI and PHP-FPM often read different php.ini files, so check the one that actually ran the failing script.
  2. Raise memory_limit in php.ini. Set memory_limit = 256M in the active php.ini (php --ini shows the path), then restart PHP-FPM with sudo systemctl restart php8.3-fpm or reload Apache. On shared hosting use the control panel's PHP settings or a .user.ini file.
  3. Raise it for one script only. For a single heavy job, call ini_set('memory_limit', '512M'); at the top of the script, or run php -d memory_limit=512M script.php from the CLI. This avoids raising the limit for every web request.
  4. Fix Composer memory errors. Run COMPOSER_MEMORY_LIMIT=-1 composer update, or php -d memory_limit=-1 $(which composer) update. Composer 2 already uses far less memory than Composer 1, so upgrade with composer self-update if you are still on 1.x.
  5. Find the code that uses the memory. Open the file and line in the error, and log memory_get_peak_usage(true) at checkpoints to see where usage jumps. Replace fetchAll() over large tables with a cursor or LIMIT/OFFSET batches, and unset() large arrays when finished.
  6. Profile if the cause is unclear. Use Xdebug's profiler or a tool like Blackfire on a staging copy to see which function allocates the most memory. A normal page that needs more than 256M almost always has a bug rather than a low limit.

php.ini or .user.ini

; php.ini (restart PHP-FPM after editing)
memory_limit = 256M

; one-off CLI run
; php -d memory_limit=512M import.php

; inside a single script
; ini_set('memory_limit', '512M');

How to stop it happening again

  • Process large datasets with generators, cursors or batches instead of loading everything at once
  • Set a realistic memory_limit per environment rather than -1 on production web requests
  • Free large variables with unset() inside long-running loops and workers
  • Monitor peak memory in logs for cron jobs and queue workers

Frequently asked questions

What does 134217728 bytes mean?

It is the memory_limit in bytes. 134217728 bytes is 128 MB, 268435456 is 256 MB and 536870912 is 512 MB.

Should I set memory_limit to -1?

Only for trusted one-off CLI tasks like Composer. On a web server, -1 lets a single buggy request consume all RAM and can take the whole server down.

Why does the error say it tried to allocate only a few bytes?

The allocation in the message is just the last one that crossed the limit. The memory was already used by everything earlier in the request, so look at what the script loaded before that line.