PHP Fatal error: Allowed memory size of X bytes exhausted
The script tried to use more memory than PHP’s memory_limit allows.
Meaning
PHP caps memory per request. The number in the message is the limit (134217728 bytes = 128 MB). The trigger is either legitimately heavy work (big imports, image processing) or a bug that keeps allocating (infinite loop, loading a whole table into an array).
Common causes
- Loading large result sets or files fully into memory
- Infinite loop/recursion building arrays
- Image processing on large images (GD/Imagick)
- Composer install/update on low-memory servers
- WordPress plugins with heavy queries
⚡ Quick fix
- Raise
memory_limit(e.g. 256M) if the workload is legitimate - Process data in chunks/generators instead of loading everything
- For Composer:
COMPOSER_MEMORY_LIMIT=-1 composer install - Profile with
memory_get_peak_usage()to find the hotspot
Detailed fix by platform
PHP
- Stream rows instead of fetchAll():php
$stmt = $pdo->query('SELECT * FROM big_table'); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { // one row at a time process($row); } // For MySQL, also consider: $pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); - Set
memory_limit = 256Min php.ini,.user.ini, orini_set('memory_limit', '256M').
WordPress
define('WP_MEMORY_LIMIT', '256M');anddefine('WP_MAX_MEMORY_LIMIT', '512M');in wp-config.php.
How to diagnose
- Limit — What is memory_limit (from the message)?
- Location — Which file/line allocates?
- Data size — How much data is loaded at once?
- Loops — Any unbounded growth?
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026