Back to Troubleshooting

Fix WordPress Memory Exhausted Error | NOC.org Support

The "Allowed memory size of 134217728 bytes exhausted" error (also known as the PHP memory limit error) means a PHP process running your WordPress site has tried to use more than 128 MB of memory (134217728 bytes = 128 MB). PHP enforces a per-process memory limit to prevent runaway scripts from consuming all server RAM. When a script exceeds this limit, PHP terminates it with this fatal error.

What the Error Looks Like

The full error message typically reads:

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

The number 134217728 is 128 MB in bytes. Common variants include:

  • 67108864 = 64 MB
  • 134217728 = 128 MB
  • 268435456 = 256 MB

The "tried to allocate X bytes" part tells you how much additional memory the script was requesting when it hit the limit. The file and line number point to where the allocation failed, but the root cause is usually accumulated memory usage across the entire request, not that specific line.

Method 1: Increase WP_MEMORY_LIMIT in wp-config.php

WordPress has its own memory limit setting that can override PHP's default (up to a point). Add or modify this line in wp-config.php, before the line that says "That's all, stop editing!":

/* WordPress memory limits */
define('WP_MEMORY_LIMIT', '256M');
define('WP_MAX_MEMORY_LIMIT', '512M');

/* That's all, stop editing! Happy publishing. */
  • WP_MEMORY_LIMIT — The maximum memory for front-end operations (default: 40M, or 64M for multisite).
  • WP_MAX_MEMORY_LIMIT — The maximum memory for admin/back-end operations (default: 256M).

Important: WP_MEMORY_LIMIT can only increase the PHP memory limit, not exceed it. If PHP's memory_limit in php.ini is set to 128M, WordPress cannot raise it to 256M via WP_MEMORY_LIMIT alone. You must also update php.ini.

Method 2: Increase memory_limit in php.ini

The authoritative memory limit is set in PHP's configuration:

# Find the php.ini file PHP is using:
php -i | grep "Loaded Configuration File"
# or check phpinfo() in a browser

# Common locations:
# /etc/php/8.2/fpm/php.ini    (PHP-FPM)
# /etc/php/8.2/cli/php.ini    (command line)
# /etc/php/8.2/apache2/php.ini (Apache mod_php)

# Edit the correct php.ini:
sudo nano /etc/php/8.2/fpm/php.ini

Find the memory_limit directive and increase it:

; Before:
memory_limit = 128M

; After:
memory_limit = 256M

Restart PHP-FPM (or Apache if using mod_php) for the change to take effect:

sudo systemctl restart php8.2-fpm
# or
sudo systemctl restart apache2

Method 3: Per-Pool Memory in PHP-FPM

If you run multiple sites with different memory needs, you can set memory limits per PHP-FPM pool:

# In /etc/php/8.2/fpm/pool.d/wordpress.conf:
[wordpress]
user = www-data
group = www-data
listen = /run/php/php8.2-fpm-wordpress.sock

; Set memory limit for this pool only:
php_admin_value[memory_limit] = 256M

; Other PHP settings per pool:
php_admin_value[upload_max_filesize] = 64M
php_admin_value[post_max_size] = 64M

The php_admin_value prefix means the setting cannot be overridden by ini_set() in PHP scripts. Use php_value (without admin) if you want to allow runtime overrides. For more on pool configuration, see PHP-FPM Pool Setup.

Method 4: Use .htaccess (Apache mod_php Only)

If you are using Apache with mod_php (not PHP-FPM), you can set the memory limit in .htaccess:

php_value memory_limit 256M

This method does not work with PHP-FPM, which is the recommended setup for modern WordPress installations.

Finding the Root Cause

Increasing the memory limit is a quick fix, but you should investigate why the site needs so much memory. A well-configured WordPress site typically needs 64-128 MB per request. If you are hitting 256 MB or more, something is likely inefficient.

Enable WP_DEBUG to Identify the Cause

// In wp-config.php:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

// Debug log is written to wp-content/debug.log

Check the debug log for warnings and deprecated function notices that might indicate which plugin or theme is consuming excessive memory.

Common Memory-Hungry Culprits

  • Page builders: Elementor, Divi, WPBakery — these load extensive frameworks and can use 100+ MB per request, especially on complex pages.
  • WooCommerce with large catalogs: Generating product listings, cart calculations, and order processing can be memory-intensive.
  • Image processing plugins: Plugins that resize, optimize, or watermark images can consume large amounts of memory, especially for high-resolution images.
  • Backup plugins: Running a backup (especially full-site backups with the database) in PHP can require significant memory.
  • Import/Export operations: Importing large XML files (WXR) or CSV product catalogs consumes memory proportional to the file size.
  • Too many active plugins: Each plugin adds overhead. Sites with 30+ active plugins can consume excessive memory even if no single plugin is the problem.

Identify the Specific Plugin

To find which plugin causes the issue:

  1. Deactivate all plugins.
  2. Switch to a default theme (Twenty Twenty-Four).
  3. If the error stops, reactivate plugins one by one until the error returns.
  4. The last activated plugin is the cause (or a combination of plugins triggering the issue).

Typical Memory Requirements

WordPress Setup Recommended memory_limit
Simple blog, few plugins 128M
Business site with page builder 256M
WooCommerce store 256M - 512M
Multisite network 256M - 512M
Large import/export operations 512M+ (temporary)

When to Increase vs When to Optimize

Increase the limit when:

  • You are running a legitimate heavy operation (WooCommerce, page builder, backup).
  • The current limit is very low (64M) for a complex site.
  • You have sufficient server RAM to support the higher limit across all concurrent PHP processes.

Optimize instead when:

  • A simple page (blog post, basic page) is hitting the memory limit — this indicates a problem.
  • You are already at 512M+ and still hitting the limit.
  • The server has limited RAM and you have many concurrent visitors (each PHP-FPM worker uses the full memory limit).

Server-Level Considerations

Remember that PHP-FPM runs multiple worker processes, each of which can use up to memory_limit. If you set memory_limit = 256M and have pm.max_children = 20, your PHP processes could theoretically use up to 5 GB of RAM (256M x 20). Make sure your server has enough RAM:

# Check available memory:
free -h

# Check PHP-FPM max_children setting:
grep 'pm.max_children' /etc/php/8.2/fpm/pool.d/*.conf

# Calculate maximum PHP memory usage:
# memory_limit x pm.max_children = maximum PHP RAM usage

Improve Your Websites Speed and Security

14 days free trial. No credit card required.