Management Guides Troubleshooting
WordPress Memory Limit: Fix "Allowed Memory Size Exhausted"
"Allowed memory size of N bytes exhausted" means a PHP request hit memory_limit. Where the limit is set in PHP-FPM and WordPress, how to raise it safely, and how to find the cause.
"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)" means one PHP request tried to use more memory than PHP's memory_limit allows. The number is the limit in bytes: 134217728 is 128 MB, 268435456 is 256 MB. Visitors usually see a blank page or "There has been a critical error on this website".
The fix has two parts. Raise the limit in PHP-FPM, where the real ceiling is set, to a value the server's RAM can carry. Then find out what used the memory, because a plugin that needs more every month will reach any limit you set.
Paths and directives below follow the PHP and WordPress documentation; the commands were not run on a test server for this article.
Which error do you have?
| What you see | Likely cause | Where to look |
|---|---|---|
Allowed memory size of N bytes exhausted | The request reached PHP's memory_limit | PHP-FPM php.ini or pool file |
Same error, and N stays the same after editing wp-config.php | The limit is fixed in the FPM pool, or the wrong file was edited | php_admin_value[memory_limit] in the pool file |
Fatal error: Out of memory (allocated N) | The server could not supply the memory, although the limit was not reached | free -m, swap, the kernel log |
| 502 errors, PHP-FPM or MariaDB restarting | The kernel's OOM killer stopped a process | sudo dmesg -T | grep -i "out of memory" |
The last two are a shortage of RAM on the server. Raising memory_limit makes them worse. Go to the Linux OOM killer on a VPS instead.
The error is written to the PHP or nginx error log with the file and line where the last allocation failed:
sudo grep -h "Allowed memory size" /var/log/nginx/error.log | tail -5
Two layers: PHP and WordPress
PHP memory_limit is the ceiling for each request. PHP's built-in default is 128M. It is set in php.ini or in the PHP-FPM pool file. The PHP manual notes that since PHP 8.5.12, 8.4.27, 8.3.36 and 8.2.35 a request may briefly go over this value before the fatal error is raised, within a bound set by the separate memory_hard_limit_percent directive. A pool is a group of PHP worker processes with its own settings.
WordPress has two constants that ask PHP for more at run time. According to the wp-config.php documentation and the core source:
WP_MEMORY_LIMITdefaults to 40M on a single site and 64M on multisite. WordPress applies it only if it is higher than the current PHP limit. With PHP at 128M it changes nothing.WP_MAX_MEMORY_LIMITdefaults to 256M. WordPress raises the limit to this value in the admin area and for heavy tasks such as image processing, again only if it is higher than the current PHP limit.
WordPress does this with PHP's ini_set() function, so it works only when PHP allows the value to be changed. The PHP-FPM documentation states that settings defined with php_admin_value in a pool file cannot be overridden with ini_set(). If your pool contains php_admin_value[memory_limit], that number is final and the WordPress constants have no effect.
See the effective values
In the dashboard, open Tools, Site Health, Info. The "Server" section shows "PHP memory limit" as PHP-FPM reports it. The "WordPress Constants" section shows WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT.
From the shell, WP-CLI prints the constants:
cd /var/www/example.com
wp eval 'echo WP_MEMORY_LIMIT, " / ", WP_MAX_MEMORY_LIMIT, PHP_EOL;'
WP-CLI runs under command-line PHP. On Ubuntu and Debian that has its own php.ini in /etc/php/8.5/cli/, so wp eval 'echo ini_get("memory_limit");' shows the command-line limit, not the one your visitors get. Trust Site Health for the web value. Installing wp is covered in the WP-CLI guide.
Find where FPM sets it:
# Ubuntu, Debian (replace 8.5 with your PHP version: 8.4 on Debian 13, 8.3 on Ubuntu 24.04)
grep -rn "memory_limit" /etc/php/8.5/fpm/
# AlmaLinux, Rocky Linux
grep -rn "memory_limit" /etc/php.ini /etc/php.d/ /etc/php-fpm.d/
Raise the PHP limit
Edit the memory_limit line in the FPM php.ini:
# Ubuntu, Debian
sudo nano /etc/php/8.5/fpm/php.ini
# AlmaLinux, Rocky Linux
sudo nano /etc/php.ini
memory_limit = 256M
256M is a rule of thumb that covers most sites with a page builder or a shop; it is not a documented requirement. If you prefer one fixed value per site that WordPress and plugins cannot change, set it in the pool file instead (/etc/php/8.5/fpm/pool.d/www.conf, or /etc/php-fpm.d/www.conf on AlmaLinux and Rocky Linux):
php_admin_value[memory_limit] = 256M
Test the configuration and reload:
# Ubuntu, Debian
sudo php-fpm8.5 -t && sudo systemctl reload php8.5-fpm
# AlmaLinux, Rocky Linux
sudo php-fpm -t && sudo systemctl reload php-fpm
Verify in Site Health that "PHP memory limit" shows the new value, then repeat the action that failed.
Set the WordPress constants
These matter only when the PHP limit is lower than the value you want and the pool does not use php_admin_value. A common case: PHP stays at 128M for visitors, and the admin area is allowed more.
wp config set WP_MEMORY_LIMIT 128M
wp config set WP_MAX_MEMORY_LIMIT 384M
wp config get WP_MAX_MEMORY_LIMIT
wp config set writes the lines above the point where wp-settings.php is loaded, which is where they must be. If you edit wp-config.php by hand, put them above the "That's all, stop editing" comment.
Why a higher limit can take the server down
memory_limit applies to each request separately. PHP-FPM runs up to pm.max_children requests at once. The worst case is therefore:
memory_limit x pm.max_children + MariaDB + everything else = must fit in RAM
With 10 workers and a 512M limit, PHP alone is allowed 5 GB. On a 2 GB VPS a burst of heavy requests then exhausts the RAM, and the kernel's OOM killer stops a process, often the database. Most requests use far less than the limit, so this is a worst case and not a forecast, but the limit is what protects the server from one bad request multiplied by every worker.
Check both numbers together:
grep -n "^pm.max_children" /etc/php/8.5/fpm/pool.d/www.conf # /etc/php-fpm.d/www.conf on AlmaLinux, Rocky
free -m
PHP-FPM tuning for WordPress shows how to size pm.max_children from measured worker memory, and How much RAM does WordPress need? covers the plan size.
Find what is using the memory
If the limit is already 256M and the error returns, look for the cause before raising it again.
- Read the log line. The path in the error often contains
wp-content/plugins/NAME. It shows where the last allocation failed, which is a strong hint and not proof. - Note which action fails. Uploading a very large image, an import or export, a backup plugin building an archive inside PHP, and report pages in a shop are typical. One failing admin action is a different problem from every page failing.
- Measure. The Query Monitor plugin shows peak memory for each page load. Compare a page with a plugin active and inactive.
- Deactivate and retest.
wp plugin deactivate PLUGIN-SLUG, repeat the action, reactivate. Start with the plugin named in the log. - Update. Memory problems are often fixed in a later plugin release.
Heavy jobs such as imports usually run better from the shell with WP-CLI or from a real cron job, outside the web workers. See WordPress cron jobs. If the site shows the generic critical error page with no message, the critical error guide shows how to get the real error text.