PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) means a script tried to use more memory than PHP's memory_limit allows. The number is the limit in bytes (134217728 = 128 MB). This guide shows how to fix the allowed memory size exhausted error properly, not just by raising the limit forever.
Before you start
- Find the full error in the site's
error_logor PHP log: it names the file and line. - Know where your site runs: cPanel hosting or your own VPS.
Read the error
| Bytes in message | Limit |
|---|---|
| 67108864 | 64 MB |
| 134217728 | 128 MB |
| 268435456 | 256 MB |
| 536870912 | 512 MB |
The file and line show where memory ran out, which is often a plugin, an image library or a large database query, but not always the code that used most of it.
Raise memory_limit on cPanel hosting
- Open cPanel → Software → Select PHP Version (or MultiPHP INI Editor).
- Go to the Options tab.
- Set
memory_limitto 256M (or 512M for heavy stores) and save.
Your plan's CloudLinux limits still apply on top of this: a script cannot use more RAM than the account's physical memory limit.
Raise memory_limit on a VPS
Find the php.ini that the web server uses (via a phpinfo() page, Loaded Configuration File), e.g. /etc/php/8.3/fpm/php.ini, and set:
memory_limit = 256MRestart PHP-FPM: systemctl restart php8.3-fpm. For one site only, set it in the pool file: php_admin_value[memory_limit] = 256M. The CLI uses a separate file (/etc/php/8.3/cli/php.ini), relevant for Composer and cron scripts.
Per-folder overrides
.user.iniin the site root (PHP-FPM, LSAPI):memory_limit = 256M.htaccess(Apache with mod_php only):php_value memory_limit 256M. On other handlers this line causes a 500 error.- In code:
ini_set('memory_limit', '256M');
WordPress-specific limits
WordPress has its own constants in wp-config.php, above the "stop editing" line:
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');These cannot exceed the server's memory_limit. Check the effective value in Tools → Site Health → Info → Server.
Find the real cause
If the error returns at 256 MB or 512 MB, something is wrong in the code:
- A plugin or module: disable plugins one by one (or rename the plugins folder) and retest.
- Huge queries: code loading thousands of rows or posts at once. Process in batches.
- Image processing: very large uploaded images need lots of RAM to resize. Resize before upload.
- Infinite loops or recursion: the same file and line appear every time.
- Imports/exports: run them via WP-CLI or the command line, where you can raise the limit for that one run:
php -d memory_limit=1G script.php.
Check the fix
- Reload the page or rerun the task that failed.
- Confirm the new value with
phpinfo()(delete the file after) orphp -i | grep memory_limitfor the CLI.
Troubleshooting
- Value does not change: you edited the wrong php.ini, or a later file overrides it.
phpinfo()shows the active value and loaded ini files. - 500 error after editing .htaccess: remove the
php_valueline and use.user.iniinstead. - memory_limit = -1: unlimited; avoid on production, a bad script can then take the whole server down.
PHP memory FAQ
What memory_limit should I use?
128M suits simple sites, 256M most WordPress sites, 512M large WooCommerce or Magento stores.
Does a higher limit slow the site?
No. It is a ceiling, not a reservation. But each PHP worker may use up to that much, which matters for total server RAM.
Is this the same as the server running out of RAM?
No. This error is PHP's own per-script limit. Server-wide out-of-memory errors kill processes instead.
