How to Fix WordPress 'Allowed Memory Size Exhausted' Fatal Error

A technical guide to diagnosing and permanently fixing the PHP Fatal error: Allowed memory size of X bytes exhausted error in WordPress by editing php.ini, wp-config.php, and .htaccess.

How to Fix WordPress 'Allowed Memory Size Exhausted' Fatal Error

One of the most disruptive WordPress errors is the PHP fatal error that instantly breaks your entire site:

PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes)

This error means your WordPress installation, PHP plugins, or theme is trying to consume more RAM than your server’s PHP configuration allows. It typically appears as a white screen, a 500 Internal Server Error, or a direct error message in the WordPress admin panel.

Hosting your site on a Dedicated VPS gives you full control to permanently resolve this by editing server-level configuration files.

Understanding the Error

PHP runs each WordPress request within a memory sandbox. The memory_limit directive in php.ini sets the maximum RAM a single PHP process can consume. The WordPress default is 40MB, but complex sites with WooCommerce, page builders like Elementor, or heavy SEO plugins (like Rank Math or Yoast) routinely demand 256MB or more.

Solution 1: Edit wp-config.php (Easiest)

WordPress provides a dedicated constant to request more PHP memory. Add this line to your wp-config.php file, before the /* That's all, stop editing! */ line:

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

This instructs WordPress to request up to 512MB from the PHP engine. Note: This only works if the server’s php.ini allows it. If php.ini sets the limit to 256MB, this directive cannot exceed it.

Solution 2: Edit php.ini (Most Reliable)

This is the definitive fix. Locate your active php.ini file by creating a temporary phpinfo.php file in your webroot:

<?php phpinfo(); ?>

Access it via your browser and search for “Loaded Configuration File” to find the exact path (e.g., /etc/php/8.1/fpm/php.ini).

Open the file and find the memory_limit directive:

; Old value
memory_limit = 256M

; New value
memory_limit = 512M

After saving, restart PHP-FPM for the change to take effect:

sudo systemctl restart php8.1-fpm

(Remember to delete the phpinfo.php file afterwards for security.)

Solution 3: Edit .htaccess (Apache-specific)

If you are on Apache and cannot modify php.ini directly (e.g., on shared hosting), you can try adding this directive to your .htaccess file in the WordPress root:

php_value memory_limit 512M

Diagnosing Which Plugin is the Culprit

If the error only happens during specific actions (e.g., importing products, running a backup), a specific plugin is likely the cause.

  1. Temporarily deactivate all plugins via FTP (rename the /wp-content/plugins/ folder to /wp-content/plugins_disabled/).
  2. Re-enable plugins one by one, triggering the action each time.
  3. When the error returns, you’ve found the guilty plugin.

For a related issue where your site crashes during peak traffic, also see our guide on fixing WordPress Error 503 Service Unavailable.

Conclusion

The “Allowed Memory Size Exhausted” error is a symptom of either under-resourced hosting or a poorly optimized plugin. By increasing the PHP memory limit via wp-config.php or php.ini and profiling your plugins, you can resolve this permanently and prevent it from recurring.

Prevent fatal PHP memory errors with generous memory limits pre-allocated on LiteSpeed-accelerated shared hosting.