Guide · WordPress

WordPress "Allowed Memory Size Exhausted" Error: How to Raise the Limit

Fix the WordPress "Allowed memory size exhausted" fatal error by raising WP_MEMORY_LIMIT, php.ini, .user.ini or .htaccess limits and finding what uses the memory.

By SiteDownCheck Editorial · Updated October 5, 2026 · 8 min read

The message looks like this:

Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 8192 bytes) in /var/www/html/wp-includes/class-wp-query.php on line 3156

PHP limits how much memory each script may use. When a WordPress request needs more than that limit, PHP stops it with a fatal error. Visitors see a blank page, the "critical error" message, or an HTTP 500, and the dashboard may fail when you try to install a plugin, import content, resize images or run a backup.

The number in the message is the limit in bytes: 41943040 is 40 MB (40 x 1024 x 1024), 67108864 is 64 MB, 134217728 is 128 MB and 268435456 is 256 MB. The "tried to allocate" number is just the last small step that tipped it over. The file named in the message is usually not the cause either, only where the limit happened to be hit. Treat it as a clue rather than proof.

How WordPress and PHP memory limits fit together

Three settings are involved, and the lowest effective one wins:

SettingWhere it livesWhat it does
memory_limitphp.ini, .user.ini, .htaccess, FPM pool or host panelThe real PHP limit for each request.
WP_MEMORY_LIMITwp-config.phpThe amount WordPress asks PHP to allow for front-end requests. Default 40 MB (64 MB on multisite).
WP_MAX_MEMORY_LIMITwp-config.phpThe amount WordPress asks for in admin tasks such as image processing. Default 256 MB.

WordPress raises the PHP limit at runtime with ini_set, but only up to what the server lets it do. If your host has a hard cap of 128 MB, setting WP_MEMORY_LIMIT to 512M has no effect. Confirm what is actually in effect before changing anything.

Step 1: Find the current limits

In the dashboard go to Tools, Site Health, Info, and open "Server" and "Constants". It shows the PHP memory limit and the WordPress constants. With WP-CLI:

wp eval 'echo ini_get("memory_limit") . PHP_EOL;'
wp config get WP_MEMORY_LIMIT
php -i | grep -i memory_limit

Be careful: the command-line PHP can use a different php.ini (and often has memory_limit = -1) than the web server's PHP. To see the web value, put a temporary phpinfo() page on the site, or use the Site Health screen. If you create a phpinfo() file, delete it afterwards.

Step 2: Raise the limit in wp-config.php

Add these lines above /* That's all, stop editing! Happy publishing. */:

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

This is the first thing to try, and on many hosts it is enough. For a typical business site 256M is a comfortable ceiling. WooCommerce and page-builder sites sometimes need more for specific tasks, but if a normal page view needs more than 256 MB, something is wrong and you should find it (see below) rather than keep raising the number.

Step 3: If that does nothing, raise the server limit

When wp-config.php changes have no effect, the server's memory_limit is capping you. Which method works depends on how PHP runs.

php.ini (VPS, dedicated, some hosts with a custom php.ini file):

memory_limit = 256M

Find the loaded file with php --ini. For PHP-FPM on Debian or Ubuntu the web file is typically /etc/php/8.3/fpm/php.ini, and you must reload the service:

sudo systemctl reload php8.3-fpm

.user.ini (PHP running as CGI/FPM on many shared hosts) in the site root:

memory_limit = 256M

PHP rescans .user.ini periodically (the default interval is 300 seconds, set by user_ini.cache_ttl), so changes may take a few minutes.

.htaccess (only for Apache with mod_php; it causes a 500 error under PHP-FPM or CGI):

php_value memory_limit 256M

If your site returns a 500 error right after adding that line, remove it. It means PHP is not running as an Apache module.

PHP-FPM pool override (for the admin of the server): in the pool file, /etc/php/8.3/fpm/pool.d/www.conf:

php_admin_value[memory_limit] = 256M

Hosting control panel. Many hosts expose "PHP settings" or "MultiPHP INI Editor" (cPanel) where you set memory_limit with a dropdown. Use it if it exists, since it writes the correct file for your setup. Plesk, DirectAdmin and managed WordPress hosts have equivalents. If the field is locked or the maximum is low, you need a plan upgrade or a ticket to the host. Check who hosts the site with the Website Hosting Checker.

Step 4: Check the limit that matters for this request

Memory errors can come from different contexts, each with its own limit:

  • Front end: limited by WP_MEMORY_LIMIT and PHP memory_limit.
  • Admin and image resizing: uses WP_MAX_MEMORY_LIMIT.
  • WP-Cron and background tasks: share the web limits, since they run in a web request unless you use system cron (see WP-Cron explained and fixed).
  • WP-CLI: uses CLI PHP's memory_limit. To raise it for one command: php -d memory_limit=512M $(which wp) search-replace ..., or set WP_CLI_PHP_ARGS.

Uploads failing with a different message ("exceeds the maximum upload size") relate to upload_max_filesize and post_max_size, not memory.

Step 5: Find what is actually using the memory

Raising the limit is a fix for a symptom. If memory use climbs for no clear reason, find the cause.

  1. Check the log. Turn on WP_DEBUG_LOG (see white screen of death) and read wp-content/debug.log. The path in the fatal error names the plugin or theme running at the time.
  2. Test by elimination. Deactivate all plugins and switch to a default theme (a safe method is in the plugin conflict guide). If the error goes away, reactivate one at a time. The plugin that brings it back is responsible, or interacts badly with another one.
  3. Measure memory per request. Add a temporary snippet in a must-use plugin (wp-content/mu-plugins/mem.php):
<?php
add_action( 'shutdown', function () {
    error_log( sprintf( '%s used %.1f MB (peak %.1f MB)',
        $_SERVER['REQUEST_URI'] ?? 'cli',
        memory_get_usage() / 1048576,
        memory_get_peak_usage() / 1048576 ) );
} );

Remove it when done. A profiler plugin such as Query Monitor shows peak memory per request in the admin bar.

  1. Look at the usual suspects.
  2. Page builders and mega-themes with many widgets on a page.
  3. Backup, import and export plugins that load whole tables or archives into memory.
  4. Image processing: very large uploads (a 6000 x 4000 pixel JPEG can need hundreds of megabytes to resize, because the memory needed relates to pixel dimensions, not file size).
  5. Queries that load every post or user at once (posts_per_page => -1) in custom code, and big wp_options autoload data.
  6. A runaway loop or recursion in custom code.
  7. Check object cache and plugin settings. An object cache can lower database load, but a misconfigured one can increase memory use.

Step 6: Reduce usage instead of just raising the cap

  • Update or replace the plugin that spikes.
  • Process heavy tasks (imports, exports, regenerate thumbnails) in batches: wp media regenerate --yes accepts ranges, and many import plugins let you lower the batch size.
  • Resize images before upload, and set a maximum dimension.
  • Add page caching so most requests do not run WordPress at all.
  • Use a recent PHP version, which generally uses memory more efficiently than older ones.
  • Remove unused plugins.

Step 7: Watch for memory pressure at the server level

If you manage the server, remember that each PHP-FPM worker can use up to memory_limit. A pm.max_children of 50 with a 256 MB limit could in the worst case need 12.8 GB. In practice average usage is far lower, so measure the real figure with ps --no-headers -o rss -C php-fpm8.3 | awk '{s+=$1} END {print s/NR/1024 " MB avg"}' and size the pool from that. If memory runs out at the OS level, you will see processes killed and 502 errors instead of PHP's own message; see fix 502 Bad Gateway.

An uptime monitor checking a page that exercises WordPress (not only a cached static page) will alert you when memory errors begin to break requests.

Next steps

FAQ

What memory limit should I set for WordPress?

256M is a sensible limit for most sites, including WooCommerce stores. Small brochure sites run fine on 128M. If you need much more for ordinary page views, look for a plugin or code that is using too much.

Why did WP_MEMORY_LIMIT not change anything?

The server's PHP memory_limit is probably lower and acts as a hard cap, or a more specific setting overrides it. Check Site Health, then raise memory_limit in php.ini, .user.ini, the PHP-FPM pool or your host's control panel.

Is it safe to set memory_limit to -1?

Not on a production web server. It removes the cap, so one runaway script can use all the RAM and take the whole server down. Use a high but finite number.

Does a bigger memory limit make my site faster?

No. The limit is a ceiling, not an allocation, and does not improve speed unless your site was hitting it. Speed comes from caching, hosting and lighter code.

Why do I only see the error when I upload images or run updates?

Those tasks need far more memory than a normal page view, particularly image resizing and plugin updates. WordPress uses WP_MAX_MEMORY_LIMIT in the admin, so make sure the server's PHP limit allows it, and resize very large images before uploading.

Get notified when this guide updates

Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.

This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.