PHP "Allowed Memory Size Exhausted": How to Fix It Properly

Where to raise memory_limit for your setup, and how to find the code that's actually eating the memory.

Fix PHP allowed memory size exhausted: code editor with a fatal error and a memory gauge moving to green

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) means one PHP request tried to use more memory than memory_limit allows, so PHP stopped it.

Short answer: raising memory_limit (to 256M or 512M) usually makes the error go away. But raise it in the right place for your setup, and then find out why the script needs so much. A limit that keeps going up is a symptom, not a fix.

Why this problem exists

memory_limit is a per-request cap. The number in the error is the limit in bytes:

Bytes in the error Limit
33554432 32M
67108864 64M
134217728 128M (PHP default)
268435456 256M
536870912 512M

The usual causes:

  • Loading too much data at once: fetchAll() on a big table, reading a whole CSV or JSON file into an array, or WooCommerce exporting every order in one go.
  • Image processing: GD and Imagick decode images into raw pixels. A 6000×4000 photo needs roughly 100 MB as a bitmap, whatever its file size.
  • Infinite loops or recursion: a loop that keeps appending to an array, or a function calling itself.
  • A heavy plugin or theme, especially page builders and backup plugins.

If the "tried to allocate" number is small but the limit is huge (say 1G), it's almost always a loop or a leak, not a genuinely heavy job.

The solution — the Hitori memory-limit path

Step 1: Find where it fails

The full error tells you the file and line:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
(tried to allocate 4096 bytes) in /var/www/site/wp-content/plugins/xyz/export.php on line 212

That path is your first suspect. If you only see a white page, check the PHP-FPM log, the web server error log, or wp-content/debug.log.

Step 2: Check the current limit

<?php echo ini_get('memory_limit');

Load it in the browser, then delete the file. The CLI value from php -i | grep memory_limit can differ from what the website uses.

Step 3: Raise it in the right place

Setup Where to set it
PHP-FPM (Nginx or Apache) Pool file, e.g. /etc/php/8.3/fpm/pool.d/www.conf: php_admin_value[memory_limit] = 256M, then restart FPM
Apache with mod_php php.ini, or .htaccess: php_value memory_limit 256M
cPanel / shared hosting MultiPHP INI Editor, or a .user.ini file in the site root
WordPress wp-config.php: define('WP_MEMORY_LIMIT', '256M'); (it can't go above the server limit)
One heavy script ini_set('memory_limit', '512M'); at the top of that script only

php_value in .htaccess breaks sites running PHP-FPM with a 500 error. Only use it with mod_php.

Step 4: Find the real cause

Log memory as the script runs:

error_log('Memory: ' . round(memory_get_usage(true) / 1048576) . ' MB at ' . __LINE__);

For a proper picture, profile with Xdebug or Blackfire on staging.

Step 5: Fix the code, not just the limit

  • Stream instead of loading everything: use fgetcsv() in a loop, and fetch database rows one at a time or in batches of 500–1000.
  • Free large variables you no longer need with unset().
  • Resize images before processing, and cap upload dimensions.
  • Move big exports and imports to a queue or cron job, not a web request.
  • Replace or update the plugin that keeps appearing in the error path.

Fix options compared

Fix Effort When to use
Raise limit to 256M Minutes A normal CMS site hitting 128M
Raise limit for one script Minutes One known heavy job (import, export, report)
Batch or stream the data Hours Errors on large tables, files or exports
Replace a plugin Hours The same plugin appears in every error
Move work to a queue A day or more Regular heavy jobs that time out too

Common mistakes and how to avoid them

  • Setting memory_limit = -1 permanently. One runaway request can then use all the server's RAM and take every site down. (It was suggested as a short-term workaround for PHP CVE-2026-109618. Undo it once you've patched.)
  • Editing the wrong php.ini. Run php --ini for the CLI, and check phpinfo() on the web for "Loaded Configuration File".
  • Forgetting to restart PHP-FPM after changing a pool or php.ini file.
  • Raising the limit above the server's real RAM. With 20 FPM workers at 512M each, a 4 GB server will start swapping.

Real-world example

A typical case: a WooCommerce store that crashes every time someone exports orders to CSV. The error points at the export plugin, and memory climbs steadily as it builds one huge array of every order.

Raising the limit to 1G makes the export work until the store grows again. The lasting fix is exporting in batches of 500 orders and writing each batch straight to the file, which keeps memory flat at around 60 MB however many orders there are.

Troubleshooting checklist

  • ☐ Read the file and line in the full error message
  • ☐ Confirmed the web-facing memory_limit (not just the CLI)
  • ☐ Raised it in the right place for FPM, mod_php or shared hosting
  • ☐ Restarted PHP-FPM if you edited server config
  • ☐ Logged or profiled memory to find the heavy section
  • ☐ Batched, streamed or queued the heavy work
  • ☐ Checked total RAM: workers × memory_limit fits on the server

Frequently asked questions

What's a good memory_limit for WordPress? 256M covers most sites. WooCommerce and page builders sometimes need 512M for admin tasks.

Why does the error say it tried to allocate only a few bytes? That's the last small allocation that crossed the limit. The memory was used up by everything before it.

Is it the same as "Out of memory"? No. "Out of memory" means the server itself ran out of RAM, which is a hosting problem rather than a PHP setting.

Will upgrading PHP help? Not by itself. Newer PHP is faster, but the same code still needs roughly the same memory, so fix the heavy part first. If you're upgrading anyway, read our guide to PHP 8 upgrade errors before you switch.

Get a fixed-price quote

Tell us what's broken or what you need. We reply within one working day with a price and timeline. No obligation.

Protected by reCAPTCHA. We only use your details to reply to this request.

Still crashing after raising the limit? Send us the full error message in the form above, and we'll tell you what's causing it and quote a fixed price for the fix.

Written by

Jyotirmay Gaur