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
- 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.
- 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.
- 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.
- 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.
- 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.
- 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