Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65536 bytes)

This error means a single PHP request tried to use more memory than allowed — here 128 MB. WordPress has its own memory settings layered on top of PHP’s, which is why fixes sometimes seem not to work. This guide explains both layers, how to raise them on a VPS, and when raising them is the wrong fix.

Two layers of limits

  1. PHP memory_limit — the hard maximum per PHP process, set in php.ini.
  2. WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT — WordPress requests these values at runtime (for the front end and admin respectively). Defaults: 40 MB (64 MB on multisite) and 256 MB.

WordPress only ever raises the limit towards its constants; if PHP’s memory_limit is already higher, it’s left alone. So the effective limit is the higher of the two — but a hosting environment may forbid runtime changes. On your own VPS, set both explicitly.

Check the current values

Tools → Site Health → Info → Server shows the PHP memory limit, and the WordPress Constants section shows WP_MEMORY_LIMIT and WP_MAX_MEMORY_LIMIT.

From the shell:

sudo -u www-data wp eval 'echo WP_MEMORY_LIMIT, " / ", WP_MAX_MEMORY_LIMIT, " / ", ini_get("memory_limit"), PHP_EOL;'

Raise the limits

PHP

Create an override file for PHP-FPM (adjust the version):

echo "memory_limit = 256M" | sudo tee /etc/php/8.3/fpm/conf.d/99-memory.ini
sudo systemctl restart php8.3-fpm

WordPress

In wp-config.php, above “That’s all, stop editing!”:

define( 'WP_MEMORY_LIMIT', '256M' );
define( 'WP_MAX_MEMORY_LIMIT', '512M' );

256 MB per request is generous for most sites; WooCommerce stores and page builders may need it. WP_MAX_MEMORY_LIMIT covers admin tasks like image processing and imports.

The limit is per request — multiply by workers

Raising memory_limit doesn’t consume memory by itself; it allows each PHP process to grow that large. The danger is the total:

max memory ≈ pm.max_children × memory actually used per request

With pm.max_children = 20 and requests using up to 256 MB, PHP alone could try to use 5 GB — on a 2 GB server, the kernel’s OOM killer would start terminating processes (often MySQL). Size PHP-FPM workers to your RAM:

# Average memory per PHP-FPM worker (MB)
ps -C php-fpm8.3 -o rss= | awk '{ sum += $1; n++ } END { printf "%.0f MB avg over %d workers\n", sum/n/1024, n }'

If workers average 80 MB and you can spare 1.2 GB for PHP, pm.max_children = 15 is about right. See PHP-FPM tuning.

Find what uses so much memory

A request that needs more than 256 MB is usually a symptom. Find the cause:

  • The error message names a file — look at the path: wp-content/plugins/<name>/… points to the plugin involved (though not always the root cause).
  • Query Monitor plugin — shows peak memory, database queries and slow components per page. Install it temporarily on staging or production.
  • Deactivate plugins one by one on a staging copy and watch peak memory.
  • Large autoload options — WordPress loads all autoloaded options on every request. Check the size:
sudo -u www-data wp db query "SELECT SUM(LENGTH(option_value))/1024/1024 AS MB FROM wp_options WHERE autoload IN ('yes','on','auto-on','auto');"

More than a few MB is a problem; find the biggest:

sudo -u www-data wp db query "SELECT option_name, LENGTH(option_value) AS size FROM wp_options WHERE autoload IN ('yes','on','auto-on','auto') ORDER BY size DESC LIMIT 10;"

Leftovers from uninstalled plugins can often be deleted or set to not autoload. See WordPress autoload options.

  • Imports, exports and backups — large operations inside a web request use lots of memory. Run them with WP-CLI instead, where the CLI memory_limit applies (often -1, unlimited).

Common situations

Situation Typical fix
Error in the admin only (media, editor) Raise WP_MAX_MEMORY_LIMIT
Error when uploading large images Raise memory; or resize images before upload
Error after installing a plugin That plugin; check its settings or replace it
Error in cron or scheduled tasks Run cron via WP-CLI system cron
Error appears randomly under load Server RAM exhausted — check free -h, OOM in journalctl -k

Server memory vs PHP memory

Don’t confuse the two. “Allowed memory size exhausted” is the PHP per-request limit. If the whole server runs out of RAM, you’ll see different symptoms: 502/504 errors, MySQL being killed, “Error establishing a database connection”. Check with:

free -h
sudo journalctl -k | grep -i "out of memory"

That calls for fewer PHP workers, caching, swap, or more RAM — see how much RAM your VPS needs.

On a WordPress VPS, you set PHP’s limits yourself and can see exactly how memory is used.

  • WordPress
  • PHP
  • Performance