Ffile2fix
Sign in Get started

Fix the WordPress memory limit error

Fatal error: Allowed memory size of 41943040 bytes exhausted (tried to allocate 2348617 bytes) in /var/www/html/wp-includes/class-wpdb.php

PHP stopped WordPress because a request needed more memory than allowed. 41943040 bytes is 40 MB, WordPress's default WP_MEMORY_LIMIT for single sites. Raising WP_MEMORY_LIMIT in wp-config.php fixes most cases, but if a site already has 256M and still runs out, a plugin or theme is usually using memory badly.

Also appears as: PHP Fatal error: Allowed memory size of 268435456 bytes exhausted in /wp-content/plugins/woocommerce/ · The PHP memory limit is below the recommended 64M (Site Health) · Fatal error: Out of memory (allocated 41943040) (tried to allocate 1048576 bytes) in /wp-admin/includes/image.php · Allowed memory size of 67108864 bytes exhausted (tried to allocate 32768 bytes) in wp-includes/plugin.php

Common causes

  • WP_MEMORY_LIMIT left at the 40M default (64M on multisite) on a site with heavy plugins
  • The host's PHP memory_limit is low and cannot be overridden by WordPress
  • Page builders, WooCommerce and backup plugins needing more memory on admin screens
  • Large image uploads processed by GD or Imagick to create thumbnails
  • A plugin loading huge queries, such as all posts or orders at once
  • Bloated autoloaded options in wp_options loaded on every request

How to fix it

  1. Raise WP_MEMORY_LIMIT. Add define('WP_MEMORY_LIMIT', '256M'); to wp-config.php above the line /* That's all, stop editing! */. WordPress raises PHP's memory_limit to this value for front-end requests if the server allows it.
  2. Raise WP_MAX_MEMORY_LIMIT for admin. Admin screens and image processing use WP_MAX_MEMORY_LIMIT, which defaults to 256M. Add define('WP_MAX_MEMORY_LIMIT', '512M'); only if admin tasks still fail.
  3. Raise PHP's memory_limit if WordPress cannot. Check Tools > Site Health > Info > Server for the PHP memory limit. If it does not change, set memory_limit = 256M in php.ini, .user.ini or the hosting panel, and restart PHP-FPM, because some hosts block runtime changes.
  4. Find the plugin using the memory. The path in the error often points to the plugin folder. Use Query Monitor to see peak memory per page, and disable plugins one at a time on staging to confirm.
  5. Shrink autoloaded options. Run wp option list --autoload=on --format=total_bytes, or the SQL query in the code box, to measure autoloaded data. Remove leftovers from deleted plugins and large expired transients with wp transient delete --expired.
  6. Reduce image sizes before upload. Very large photos need a lot of memory to resize. Resize them to a sensible width before uploading, or use a host with Imagick, which is usually more memory efficient than GD.

wp-config.php (plus an SQL size check)

define( 'WP_MEMORY_LIMIT', '256M' );     // front end
define( 'WP_MAX_MEMORY_LIMIT', '512M' ); // wp-admin, image processing

-- autoloaded options size (MySQL)
-- SELECT SUM(LENGTH(option_value)) FROM wp_options WHERE autoload IN ('yes','on','auto-on','auto');

How to stop it happening again

  • Remove unused plugins and their leftover options
  • Monitor memory per request with Query Monitor during development
  • Resize images before uploading
  • Ask your host for a memory_limit that matches your plugin stack

Frequently asked questions

What is the difference between WP_MEMORY_LIMIT and memory_limit?

memory_limit is PHP's setting. WP_MEMORY_LIMIT is the value WordPress tries to raise memory_limit to at runtime; it never lowers a higher PHP limit, and it fails if the host prevents runtime changes.

How much memory does WordPress need?

WordPress itself runs in 64M, but sites with WooCommerce, page builders or many plugins typically need 256M. Needing more than 512M for normal pages usually points to a plugin problem.

I set 256M but the error still says 40 MB. Why?

The define must appear before wp-settings.php is loaded, above the 'stop editing' line. Also check for a second WP_MEMORY_LIMIT definition elsewhere in wp-config.php.